0

i have a Vector of Hashmap and the HashMap contains different data types:

Vector<HashMap<String, Object>> theVector= new Vector<HashMap<String, Object>>();

theResults contains this HashMap:

HashMap<String, Object> theHashMap= new HashMap<String, Object>();

theHashMap has these data: (pretend this is a for-loop)

//1st set

theHashMap.put("BLDG_ID", 111); //int
theHashMap.put("EMP_NAME", "AAA"); //String
theHashMap.put("FLAG", true); //boolean

theVector.add(theHashMap);

//2nd set

theHashMap.put("BLDG_ID", 222); //int
theHashMap.put("EMP_NAME", "BBB"); //String
theHashMap.put("FLAG", false); //boolean

theVector.add(theHashMap);

//2nd set<br>
theHashMap.put("BLDG_ID", 111); //int
theHashMap.put("EMP_NAME", "CCC"); //String
theHashMap.put("FLAG", false); //boolean

theVector.add(theHashMap);

I want to sort the contents of my vector of HashMap according to BLDG_ID so that when I display the data it would look like

BLDG_ID || EMP_NAME
111     ||    AAA
111     ||    CCC
222     ||    BBB

How do I do that?

Confluence
  • 1,331
  • 1
  • 10
  • 26
ayeen c
  • 71
  • 1
  • 5
  • my > and < have been stripped here's the declaration for theVector again'' Vector <HashMapvString, Object>> theVector= new Vector<HashMap<String, Object>>(); – ayeen c Oct 20 '11 at 17:41
  • Please learn how to use Markdown: http://stackoverflow.com/editing-help – NullUserException Oct 20 '11 at 17:42
  • argh. my < and > have been stripped here's the declaration for theVector again'' Vector > theVector= new Vector>(); HashMap theHashMap= new HashMap(); – ayeen c Oct 20 '11 at 17:47

3 Answers3

2

I think you'd be much better off doing something like this: Instead of using a hashmap for your values, just make a class. Then you'll get compile time checking on your operations, which will help prevent errors down the road.

class Employee implements Comparable<Employee> {
    int buildingId;
    String name;
    boolean flag;

    Employee(int b, String n, boolean f) {
        buildingId = b;
        name = n;
        flag = f;
    }

    public int compareTo(Employee other) {
        if(other.buildingId == this.buildingId) 
            return name.compareTo(other.name);
        return buildingId - other.buildingId; // potential for overflow, be careful
    }

}

Then you can just sort the vector using whatever sort you want. If you use ArrayList (the modern form of Vector) you can use Collections.sort(myList);

List<Employee> emps = new ArrayList<Employee>();
emps.add(new Employee(111,"AAA",true));
emps.add(new Employee(111,"CCC",false));
emps.add(new Employee(111,"BBB",false));

Collections.sort(emps);
System.out.println("Building Id,Employee Name");
for(Employee emp : emps) System.out.println(emp.getCSV()); // or however you want to format it
corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • +1: Another case of [object denial](http://stackoverflow.com/questions/3725703/how-to-store-more-than-one-string-in-a-map). – Joachim Sauer Oct 20 '11 at 17:45
  • unfortunately I can't change the hashmap because some other process needs that object – ayeen c Oct 20 '11 at 17:48
  • @Joachim that is a gem of a term! – corsiKa Oct 20 '11 at 17:49
  • @ayeenc That should be addressed. If nothing else, I would use it internally for your own sorting purposes. It will simplify things in the long run. But really, you should be talking to your consumer of the hashmap and get them to transition to the object. – corsiKa Oct 20 '11 at 17:51
  • i also cannot change the Vector to List... i would break a lot of other functionalities/dependencies if i change any data type – ayeen c Oct 20 '11 at 18:13
  • I looked and you don't have to change Vector to List. It the only *major* difference between them is that Vector is synchronized and ArrayList isn't. (There are other differences, but that's the key one.) So you can still use `Collections.sort()` with your vector. If you pass in a custom comparator, you can use `sort(theVector,comp)` - the comparator would be equivalent to the `compareTo` method (basically). – corsiKa Oct 20 '11 at 20:11
1

Implement a custom Comparator<Map<String, Object>>, then call Collections.sort

Note: You might want to use ArrayList instead of Vector.

Puce
  • 37,247
  • 13
  • 80
  • 152
1
List<Map<String, Object>> vector = new Vector<Map<String, Object>>();

Collections.sort(vector, new Comparator<Map<String, Object>>() {
    @Override
    public int compare(Map<String, Object> map1, Map<String, Object> map2) {
        return ((Integer) map1.get("BLDG_ID")).compareTo((Integer) map2.get("BLDG_ID")));
    }            
});

Update: For your code:

After the "last"

theVector.add(theHashMap);

add the following

Collections.sort(theVector, new Comparator<HashMap<String, Object>>() {
        @Override
        public int compare(HashMap<String, Object> o1, HashMap<String, Object> o2) {
            return ((Integer) o1.get("BLDG_ID")).compareTo((Integer) o2.get("BLDG_ID"));
        }            
    });
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • Use the interface (Map) instead of the implementation (HashMap). Besides I think the second generic parameter should be Object. – Puce Oct 20 '11 at 17:54
  • thanks Bhesh Gurung & puce, pardon my ignorance but how do i use the above code given my data sample?do i add that custom Comparator inside the same method where i have the theVector.add(theHashMap); and theHashMap.put("BLDG_ID", 222);//int
    , etc? thanks for your patience
    – ayeen c Oct 20 '11 at 18:45
  • @ayeenc: Yes, after you are done adding all the `HashMap`s into the vector, that's when you need to sort your vector right? – Bhesh Gurung Oct 20 '11 at 19:16
  • @ayeenc: Also, since this is your first question here in SO, you can accept the answer if your problem is solved. See http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work. – Bhesh Gurung Oct 20 '11 at 19:35