• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How do i use the collection returned by a Maps .values method?

Ive never really understood how to use this properly. I have a HashMap full of Employee objects and im looking to extract the individual Employee objects and put them into a TreeSet.

Code:
TreeSet<Employee> aSortedSet = new TreeSet<Employee>();

Iterator<Employee> aCollectionIterator = anUnsortedMap.values().iterator();

        while (aCollectionIterator.hasNext())
        {
            aSortedSet.add(aCollectionIterator.next());
        }

I just seem to end up with a aSortedSet that has another collection inside it rather than the individual values from anUnsortedMap. What am i doing wrong? 😕
 
I'm not quite sure I understand your problem. Your aSortedSet will contain the exact same Employee objects as the anUnsortMap, just wrapped in a different data structure.

An easier way to achieve the same result would be the following:
Code:
TreeSet<Employee> aSortedSet = new TreeSet<Employee>(anUnsortedMap.values());
 
Last edited:
Does the OP mean that his TreeSet only contains one item, which is the Collection object itself returned from HashMap.values()?

In any case, I'm not sure why OP's code wouldn't work. Here's a quick test:
Code:
        HashMap anUnsortedMap = new HashMap();
        anUnsortedMap.put("1", "GKJH");
        anUnsortedMap.put("2", "OIUT");
        anUnsortedMap.put("3", "CVBN");
        anUnsortedMap.put("4", "ABC");
        anUnsortedMap.put("5", "POIU");
        anUnsortedMap.put("6", "ERHK");
        
        TreeSet<String> aSortedSet = new TreeSet<String>();

        Iterator<String> aCollectionIterator = anUnsortedMap.values().iterator();

        while (aCollectionIterator.hasNext())
        {
            aSortedSet.add(aCollectionIterator.next());
        }
        
        //print the TreeSet
        for(String value : aSortedSet){
            System.out.println(value);
        }


OUTPUT:
ABC
CVBN
ERHK
GKJH
OIUT
POIU

Works for me. As mentioned, for simplicity sake, you can just include the whole values Collection in the TreeSet's constructor. There's also an addAll() method.
 
Last edited:
Yes my collection appeared to contain just 1 object. Well it only showed 1 thing when i did a System.out.println(object); check to see what was in there, whereas the collection returned from the map showed several things. They look like the memory addresses of the objects but im not sure im still relatively new to this.

In the end i gave up and used a set instead of a map 🙂
 
When you print something out, you're calling the toString() method on that object. A lot of objects don't override the default toString() method (the default toString() just prints out the address of the object).

When you printed your tree, you saw the memory address of the tree since it was using the default toString() method. When you printed each element in the tree, you saw the memory address of each element (if you had overriden toString() in your Employee class, you would have seen something better).
 
Last edited:
Yeah i saw 1 single address with the way i was doing it, i saw several when i printed out the Collection directly from the map.

This works perfectly:
Code:
Collection aCollection = aMap.values();
        
        ArrayList<Employee> aList = new ArrayList<Employee>(aCollection);

        System.out.println(aList);

This dosent:
Code:
Collection aCollection = aMap.values();
        
        TreeSet<Employee> aTreeSet = new TreeSet<Employee>(aCollection);

        System.out.println(aTreeSet);

HashSet works fine too, but for some reason a TreeSet only gets 1 Employee object.

Oooohh i know what the problem was! I didn't finish my compareTo method for Employee... i just found it there and it looks like i got distracted and trailed off then forgot about it lol. All works great now :thumbsup:
 
I don't know anything about Java other than it looks similar to C++ in syntax (I could see everything you have written as a C++ line).

Where on earth is Goober actually SORTING the data? The iterators some how automatically put them in order?
 
I don't know anything about Java other than it looks similar to C++ in syntax (I could see everything you have written as a C++ line).

Where on earth is Goober actually SORTING the data? The iterators some how automatically put them in order?

TreeSet's are ordered, and at insertion time it orders them accordingly. Since it's full of Strings, it does the "normal" ordering of a String.
 
TreeSet's are ordered, and at insertion time it orders them accordingly. Since it's full of Strings, it does the "normal" ordering of a String.

Ah I see. Thanks. I am up for a possible Java position and have no clue about it, lol. I can however learn really quickly and the project is interesting enough that I'll figure it out.
 
Ah I see. Thanks. I am up for a possible Java position and have no clue about it, lol. I can however learn really quickly and the project is interesting enough that I'll figure it out.

If you know how to program OOP, then Java is cake to learn. The documentation helps wonders as well. I transitioned to Java after doing C++ for 5.5 years, and I feel more competent in Java than I ever did in C++, after only doing it for a little over 2 years.
 
Back
Top