Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

collection output issue

Posted on 2017-03-02
9
Medium Priority
?
145 Views
Last Modified: 2017-03-09
 

 
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
 
public class MyHashMapEntrySet3 {
 
    public static void main(String arg[]){
      HashMap hm = new HashMap<Integer, String>();
    	//HashMap hm = new TreeMap<String, String>();
    	//HashMap hm = new LinkedHashMap<String, String>();
        //add key-value pair to hashmap
        hm.put(3, "C");
        hm.put(1, "A");
        hm.put(2, "B");
        //hm.put(3, "C");
        System.out.println(hm);
        //getting value for the given key from hashmap
       /* Set<Entry<String, String>> entires = hm.entrySet();
        for(Entry<String,String> ent:entires){
            System.out.println(ent.getKey()+" ==> "+ent.getValue());*/
       //d }
    }
}

Open in new window


{1=A, 2=B, 3=C}


i wonder how i got above sorted output based on key all the time first 1 then 2 then 3 key set? please advise
0
Comment
Question by:gudii9
  • 4
  • 3
  • 2
9 Comments
 
LVL 28

Expert Comment

by:rrz
ID: 42031683
0
 
LVL 7

Author Comment

by:gudii9
ID: 42031883
What is the answer for this question I could not find there
0
 
LVL 7

Author Comment

by:gudii9
ID: 42031898
you mean below
The ordering is preserved in that particular case since the hash code of 1 is 1, 2 is 2 etc.

http://stackoverflow.com/questions/2817695/how-does-java-order-items-in-a-hashmap-or-a-hashtable



HashMap stores the values using the unique hash-value generated using a part of the key

which part of the key it uses
how to see Integer 1 hashcode in documentation where it is preserving order?
same how to see String 1 hashcode in documentation where it is messing order
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Accepted Solution

by:
Jeffrey Dake earned 1000 total points
ID: 42032056
With the maps you really have to look at the contracts that they guarantee. A hashmap, just like a hashset may give you the same order but it is not guaranteed. To have it sorted you really need to use the tree map, that guarantees the order based on a comparator.
0
 
LVL 28

Assisted Solution

by:rrz
rrz earned 1000 total points
ID: 42032405
From the documentation,
http://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html   
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
 
Since the class uses the same algorithm each time, the results will probably be the same each time you run your program. But, this behavior is not guaranteed.  
Also from the documentation,
http://docs.oracle.com/javase/8/docs/api/java/util/Map.html   
The order of a map is defined as the order in which the iterators on the map's collection views return their elements.

But, there no guarantees.  
The important feature of Map is the time it takes to "get" the value that is associated with a particular key. If we have a lot of data in the Map, then using HashMap will be faster than using one of the other Maps that do guarantee order.
0
 
LVL 7

Author Comment

by:gudii9
ID: 42032647
i am getting same result many many times consistently which makes me believe it is predictable
0
 
LVL 28

Expert Comment

by:rrz
ID: 42032665
i am getting same result many many times
I see the same behavior. But, what if we move our code to a different environment? What if we run our code with different Java version?    We could see different results.
0
 
LVL 7

Author Comment

by:gudii9
ID: 42032709
make sense
0
 
LVL 11

Expert Comment

by:Jeffrey Dake
ID: 42032729
Also if the hashing function underneath changed you could get different results.  That's why the ordered sets and maps use comparable objects so the order can always be guaranteed.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question