vikaspatel1
asked on
java - collection
i am doing student grade problem here i have question
import java.util.*;
public class StudentComparator implements Comparator
{
public int compare(Object o1, Object o2)
{
Student s1 = (Student)o1;
Student s2 = (Student)o2;
if (s1.getLastName().compareT o(s2.getLa stName()) < 0)
return -1;
if (s1.getLastName().compareT o(s2.getLa stName()) == 0)
{
if (s1.getFirstName().compare To(s2.getF irstName() ) < 0)
return -1;
if (s1.getFirstName().compare To(s2.getF irstName() ) == 0)
{
if (s1.getId() < s2.getId())
return -1;
if (s1.getId() == s2.getId())
return 0;
return 1;
}
return 1;
}
return 1;
}
}
i want to convert this program to comparable interface. i don't konw how can i do this.
rest of program as follows.
//------------------------ ---------- ---------- ---------- ---
// Name : Vikas Patel
// Subject : Internet Application With Java
// Description : Student Grade Report
//------------------------ ---------- ---------- ---------- ---
import iopack.Io;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
//------------------------ ---------- ---main--- ---------- ----
public class ExP20_5
{
public static void main(String[] args)
{
Map m = new HashMap();
while(true)
{
System.out.println ("");
System.out.println (" Enter A for ADD");
System.out.println (" Enter M for Modify");
System.out.println (" Enter R for Remove");
System.out.println (" Enter P for Print");
String input = Io.readString("Please Enter :");
if (input.equalsIgnoreCase("A "))
{
int more;
do
{
String fName = Io.readString(
"Enter the student's first name:");
String lName = Io.readString(
"Enter the student's last name:");
String ident = Io.readString(
"Enter the student's ID:");
String grade = Io.readString(
"Enter the student's grade:");
if (fName == null || lName == null|| ident == null || grade == null)
System.exit(0);
else
{
int id = Integer.parseInt(ident);
Student s = new Student(fName, lName, id);
m.put(s, grade);
}
System.out.println ("");
more = Io.readInt ("Enter 1 for continue or 0 for Quit:");
}while (more == 1);
}
else if (input.equalsIgnoreCase("R "))
{
int more2 ;
do
{
System.out.println("------ ---------- ---------- --");
String ident = Io.readString(
"Enter the student's ID for remove:");
if (ident == null)
System.exit(0);
else
{
int id = Integer.parseInt(ident);
remove(id, m);
}
System.out.println ("");
more2 = Io.readInt( "Enter 1 for continue or 0 for Quit:");
}while (more2==1);
}
else if (input.equalsIgnoreCase("M "))
{
int more3 ;
do
{
String ident = Io.readString(
"Enter the student's ID:");
String grade = Io.readString(
"Enter the student's grade:");
if (ident == null || grade == null)
System.exit(0);
else
{
int id = Integer.parseInt(ident);
modify(id, grade, m);
}
System.out.println ("");
more3 = Io.readInt( "Enter 1 for continue or 0 for Quit:");
}while (more3 == 1);
}
else if (input.equalsIgnoreCase("P "))
{
print(m);
}
else
System.exit(0);
}
}
//------------------------ ---------r emove----- ---------- ---------- ---------- ---------
private static void remove(int id, Map m)
{
int studentId = 0;
Set keySet = m.keySet();
Iterator iter = keySet.iterator();
while(iter.hasNext())
{
Object key = iter.next();
Student s = (Student)key;
studentId = s.getId();
if (studentId == id)
m.remove(s);
}
}
//------------------------ --------mo dify------ ---------- ---------- ---------- ----------
private static void modify(int id, String grade, Map m)
{
int studentId = 0;
Set keySet = m.keySet();
Iterator iter = keySet.iterator();
while(iter.hasNext())
{
Object key = iter.next();
Object value = m.get(key);
Student s = (Student)key;
studentId = s.getId();
if (studentId == id)
m.put(s, grade);
}
}
//------------------------ ---------- print----- ---------- ---------- ---------- ---------- -
private static void print(Map m)
{
List keys = new ArrayList(m.keySet());
Comparator comp = new StudentComparator();
Collections.sort(keys, comp);
Iterator iter = keys.iterator();
System.out.println ("======================== ========== ======= ");
while(iter.hasNext())
{
Object key = iter.next();
Object value = m.get(key);
Student s = (Student)key;
System.out.println(s.getLa stName() + ", " + s.getFirstName() + " " + s.getId()
+ ": " + value);
}
System.out.println("====== ========== ========== ========== =====");
}
}
import java.util.*;
public class StudentComparator implements Comparator
{
public int compare(Object o1, Object o2)
{
Student s1 = (Student)o1;
Student s2 = (Student)o2;
if (s1.getLastName().compareT
return -1;
if (s1.getLastName().compareT
{
if (s1.getFirstName().compare
return -1;
if (s1.getFirstName().compare
{
if (s1.getId() < s2.getId())
return -1;
if (s1.getId() == s2.getId())
return 0;
return 1;
}
return 1;
}
return 1;
}
}
i want to convert this program to comparable interface. i don't konw how can i do this.
rest of program as follows.
//------------------------
// Name : Vikas Patel
// Subject : Internet Application With Java
// Description : Student Grade Report
//------------------------
import iopack.Io;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
//------------------------
public class ExP20_5
{
public static void main(String[] args)
{
Map m = new HashMap();
while(true)
{
System.out.println ("");
System.out.println (" Enter A for ADD");
System.out.println (" Enter M for Modify");
System.out.println (" Enter R for Remove");
System.out.println (" Enter P for Print");
String input = Io.readString("Please Enter :");
if (input.equalsIgnoreCase("A
{
int more;
do
{
String fName = Io.readString(
"Enter the student's first name:");
String lName = Io.readString(
"Enter the student's last name:");
String ident = Io.readString(
"Enter the student's ID:");
String grade = Io.readString(
"Enter the student's grade:");
if (fName == null || lName == null|| ident == null || grade == null)
System.exit(0);
else
{
int id = Integer.parseInt(ident);
Student s = new Student(fName, lName, id);
m.put(s, grade);
}
System.out.println ("");
more = Io.readInt ("Enter 1 for continue or 0 for Quit:");
}while (more == 1);
}
else if (input.equalsIgnoreCase("R
{
int more2 ;
do
{
System.out.println("------
String ident = Io.readString(
"Enter the student's ID for remove:");
if (ident == null)
System.exit(0);
else
{
int id = Integer.parseInt(ident);
remove(id, m);
}
System.out.println ("");
more2 = Io.readInt( "Enter 1 for continue or 0 for Quit:");
}while (more2==1);
}
else if (input.equalsIgnoreCase("M
{
int more3 ;
do
{
String ident = Io.readString(
"Enter the student's ID:");
String grade = Io.readString(
"Enter the student's grade:");
if (ident == null || grade == null)
System.exit(0);
else
{
int id = Integer.parseInt(ident);
modify(id, grade, m);
}
System.out.println ("");
more3 = Io.readInt( "Enter 1 for continue or 0 for Quit:");
}while (more3 == 1);
}
else if (input.equalsIgnoreCase("P
{
print(m);
}
else
System.exit(0);
}
}
//------------------------
private static void remove(int id, Map m)
{
int studentId = 0;
Set keySet = m.keySet();
Iterator iter = keySet.iterator();
while(iter.hasNext())
{
Object key = iter.next();
Student s = (Student)key;
studentId = s.getId();
if (studentId == id)
m.remove(s);
}
}
//------------------------
private static void modify(int id, String grade, Map m)
{
int studentId = 0;
Set keySet = m.keySet();
Iterator iter = keySet.iterator();
while(iter.hasNext())
{
Object key = iter.next();
Object value = m.get(key);
Student s = (Student)key;
studentId = s.getId();
if (studentId == id)
m.put(s, grade);
}
}
//------------------------
private static void print(Map m)
{
List keys = new ArrayList(m.keySet());
Comparator comp = new StudentComparator();
Collections.sort(keys, comp);
Iterator iter = keys.iterator();
System.out.println ("========================
while(iter.hasNext())
{
Object key = iter.next();
Object value = m.get(key);
Student s = (Student)key;
System.out.println(s.getLa
+ ": " + value);
}
System.out.println("======
}
}
Another note.
TreeMap expects you to give it a Comparator. If
you do not give it a Comparator, it assumes that
everything you put in it implements the Comparable interface.
So an alternate approach would be to have Student
implement the "Comparable" interface.
In the declaration of Student,
public class Student implements Comparable{
public int compare(Object o2){
Student s1 = this;
Student s2 = (Student)o2;
... same code as you used in your comparator
}
...whatever else Student has
}
Either way, solves the problem so use what you are most comfortable with.
- T
TreeMap expects you to give it a Comparator. If
you do not give it a Comparator, it assumes that
everything you put in it implements the Comparable interface.
So an alternate approach would be to have Student
implement the "Comparable" interface.
In the declaration of Student,
public class Student implements Comparable{
public int compare(Object o2){
Student s1 = this;
Student s2 = (Student)o2;
... same code as you used in your comparator
}
...whatever else Student has
}
Either way, solves the problem so use what you are most comfortable with.
- T
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
D'oh, thanks CEHJ.
-T
-T
8-)
Thanks vikaspatel1, but it was a little unfair to give me all the points when Tim had answered the main part of the question. A points split would have been better.
I think what you are asking is, "How do I use the given Comparator to sort the
Student objects in the Map"? Am I correct?
If so, you need 2 things.
1. Change the Map implementation from a HashMap to a TreeMap.
HashMaps don't store things sorted.
2. Pass the TreeMap the Comparator as its constructor argument.
Comparator c = new StudentComparator();
Map m = new TreeMap( c );
and you are set.
This keeps the Student objects (the Map's keys) in sorted order.
So when you iterate them, they iterate in sorted order.
Regards,
- Tim