How to sort hashMap by keys?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1261 Views - Last Post: 10 October 2013 - 03:43 PM Rate Topic: -----

#16 pineapplePie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 23-January 12

Re: How to sort hashMap by keys?

Posted 08 October 2013 - 08:20 AM

View Postg00se, on 08 October 2013 - 02:18 AM, said:

Can you please post your complete, runnable, code?

These are runnable but the outputs are not right.

import java.util.Set;

public class Tester {
    public static void main(String[] args){
        StudentSet map = new StudentSet();
         // Student(id, name, age) , post code
        map.intput(new Student(1, "C", 22), "1048");
        map.intput(new Student(2, "A", 20), "2913");
        map.intput(new Student(6, "D", 20), "2013");
        map.intput(new Student(9, "B", 21), "2012");
 
        // Name : alphabetical order
        for (Student s : map.getNameMap().keySet()){
            System.out.println(s.getID()+":"+s.getName()+":"+s.getAge()+":"+map.getNameMap().get(s));
        }
        
        System.out.println("---------ID");
        Set<Student> sks = map.getIDMap().keySet();
        for (Student student : sks){
        	System.out.printf("Id is %d%n", student.getID());
        }
        
        // ID order
        for (Student s : map.getIDMap().keySet()){
            System.out.println(s.getID()+":"+s.getName()+":"+s.getAge()+":"+map.getIDMap().get(s));
        }
        
        System.out.println("---------AGE");
        // Age order
        for (Student s : map.getAgeMap().keySet()){
            System.out.println(s.getID()+":"+s.getName()+":"+s.getAge()+":"+map.getAgeMap().get(s));
        }
    }
}


import java.util.Comparator;
 
public class Student implements Comparable<Student> , Comparator<Student>{
    private static int id;
    private String name;
    private static int age;
    public Student(int i, String n, int a){
        id=i;
        name=n;
        age=a;
    }
     
    public int getID(){
        return id;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
     
    // this is for sorting by name
    @Override
    public int compareTo(Student o) {
        return name.compareTo(o.getName());
    }

    // this is for sorting by name : Comparator
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getName().compareTo(o2.getName());
    }
     
    // this is for sorting by id
    public static Comparator<Student> idOrder = new Comparator<Student>() {
        @Override
        public int compare(Student o1, Student o2) {
            return ((Integer)o1.getID()).compareTo(o2.getID());
        }
    };
    // this is for sorting by age
    public static Comparator<Student> ageOrder = new Comparator<Student>() {
        @Override
        public int compare(Student o1, Student o2) {
            return ((Integer)o1.getAge()).compareTo(o2.getAge());
        }
    };
}


import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
 
 
public class StudentSet {
    SortedMap<Student, String> studentMap;
 
    public StudentSet() {
    	studentMap = new TreeMap<>();
    }
    public void intput(Student s, String str){
    	studentMap.put(s, str);
    }
 
    //------------------------------------------
    // sort the set on the basis of Name.
    public Map<Student, String> getNameMap() {
        return studentMap;
    }
    // sort the set on the basis of ID.
    public Map<Student, String> getIDMap() {
    	SortedMap<Student, String> sortedMap = new TreeMap<>(Student.idOrder);
    	sortedMap.putAll(studentMap);
        return sortedMap;
    }
    
    // sort the set on the basis of AGE.
    public SortedMap<Student, String> getAgeMap() {
    	SortedMap<Student, String> sortedMap = new TreeMap<>(Student.ageOrder);
    	sortedMap.putAll(studentMap);
        return sortedMap;
    }
}


Was This Post Helpful? 0
  • +
  • -

#17 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,217
  • Joined: 20-September 08

Re: How to sort hashMap by keys?

Posted 08 October 2013 - 08:38 AM

You've declared two of what should be instance variables in Student as static. They should not be static
Was This Post Helpful? 0
  • +
  • -

#18 pineapplePie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 23-January 12

Re: How to sort hashMap by keys?

Posted 10 October 2013 - 01:27 AM

I thought it should be "static" since it was done like this;

Quote

You can use a SortedMap with a Comparator:

public class StudSort {
public static void main(String[] args) {
SortedMap<Student, Integer> map = new TreeMap<Student, Integer>(new StudentComparator());
// Student(id, name, age) , postcode
map.put(new Student(1, "C", 22), 1048);
map.put(new Student(2, "A", 20), 2913);
map.put(new Student(6, "D", 20), 2013);
map.put(new Student(9, "B", 21), 2012);
System.out.println(map);
}
private static class StudentComparator implements Comparator<Student> {
public int compare(Student s1, Student s2) {
return s1.getName().compareTo(s2.getName());
}
}
}

How should it be done if "idOrder" is not static? I'm kinda lost about the usage of it...
Was This Post Helpful? 0
  • +
  • -

#19 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,217
  • Joined: 20-September 08

Re: How to sort hashMap by keys?

Posted 10 October 2013 - 01:31 AM

Quote

You've declared two of what should be instance variables in Student as static.
You're not looking at the right code. The code you just posted is not that of class Student
Was This Post Helpful? 1
  • +
  • -

#20 pineapplePie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 23-January 12

Re: How to sort hashMap by keys?

Posted 10 October 2013 - 03:43 PM

View Postg00se, on 10 October 2013 - 01:31 AM, said:

Quote

You've declared two of what should be instance variables in Student as static.
You're not looking at the right code. The code you just posted is not that of class Student

I see. Thank you very much. I'll try to solve this somehow.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2