3

Possible Duplicate:
How to sort a Map<Key, Value> on the values in Java?

Suppose am having a map like

Map<String, Student> studDetails = new HashMap<String, Student>();

And the map contains entries like

studDetails.put("1",student1);
studDetails.put("2",student2);
studDetails.put("3",student3);
studDetails.put("4",student4);

Student entity is Something like

Class Student{
           private String studName;
           private List<Group> groups;
}

Group entity will be like

Class Group{
             private String groupName;
             private Date creationDate;
}

OK, so what I need is when am displaying student details, it will be in the order of group creation date. So if student is mapped to more than one group, we can take the creationDate of first group.

How can I given the soring on my HashMap studDetails using this scenario.?

Can anyone help..please..

Community
  • 1
  • 1
Tijo
  • 41
  • 2
  • 4
  • 2
    Sorting a map based on the values is achieved here: [How to sort a Map on the values in Java?](http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java). Moreover, `Date` is `Comparable`, so you shouldn't have a hard time figuring out how to utilize this ;) – Baz Oct 12 '12 at 07:28

4 Answers4

4

HashMap is not sorted you should use a SortedMap implementation instead for example TreeMap.

Then you could create your own Comparator<String> which will sort by the groups attribute of the actual Student instance but you'll need the actual map for it because TreeMap sorts by the keys, so this is a possible but not nice solution.

So with TreeMap:

public class StudentGroupComparator implements Comparator<String> {
  private Map<String, Student> sourceMap;

  public StudentGroupComparator(Map<String, Student> sourceMap) {
    this.sourceMap = sourceMap;
  }

  @Override
  public int compare(String key1, String key2) {
    // TODO: null checks
    Student student1 = sourceMap.get(key1);
    Student student2 = sourceMap.get(key2);

    Date o1CreationDate = student1.groups.get().creationDate;
    Date o2CreationDate = student2.groups.get().creationDate;
    return o1CreationDate.compareTo(o2.creationDate);
  }
}


SortedMap<String, Student> sortedMap = new TreeMap<String, Student>(new StudentGroupComparator(sourceMap));
sortedMap.putAll(sourceMap);
KARASZI István
  • 28,974
  • 8
  • 95
  • 116
3

How can I given the soring on my HashMap studDetails using this scenario?

You can't, because HashMap is fundamentally unordered (or at least, the ordering is unstable and unhelpful).

Even for sorted maps like TreeMap, the sort order is based on the key, not the value.

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
0

Add Students Objects to List and Use Collections.sort(list, custom_comparetor).

Prepare one Custom comparator to sort Students Objects.

Try this code it might helpful

StudentComparator.java

class StudentComparator implements Comparator {

    public int compare(Object stud1, Object stud2) {
        List<Group> list1Grp = ((Student) stud1).getGroups();
        List<Group> list2Grp = ((Student) stud2).getGroups();

        Collections.sort(list1Grp, new GroupComparator());
        Collections.sort(list2Grp, new GroupComparator());
        return list1Grp.get(0).getCreationDate().compareTo(list2Grp.get(0).getCreationDate());
    }

}

GroupComparator.java

public class GroupComparator implements Comparator {

    public int compare(Object grp1, Object grp2) {
        return ((Group) grp1).getCreationDate().compareTo(
                ((Group) grp2).getCreationDate());
    }

}

main method

add student object to one new List

then use

Collections.sort(new_stud_list, new StudentComparator());
NPKR
  • 5,134
  • 4
  • 28
  • 46
0

Add Comparable to Group

class Group implements Comparable {
    private String groupName;
    private Date creationDate;

    public Date getCreationDate() {
        return creationDate;
    }

    @Override
    public int compareTo(Object t) {
        Group g = (Group) t;
        return getCreationDate().compareTo(g.getCreationDate());
    }
}

Use TreeSet instead of List in Student for Groups

public class Student implements Comparable {
    private String studName;
    private TreeSet<Group> groups;

    public TreeSet<Group> getGroups() {
        return groups;
    }

    @Override
    public int compareTo(Object t) {
        Student t1 = (Student) t;
        return groups.first().getCreationDate()
                .compareTo(t1.getGroups().first().getCreationDate());
    }

}

Now use

TreeSet<Student> studDetails = new TreeSet();

then add students. It will be ordered one. Hope you can take care of null pointer exceptions

Shashi
  • 11,342
  • 16
  • 60
  • 108
vels4j
  • 10,808
  • 4
  • 35
  • 54