Data Containers in Java programming language

 Data Containers in Java programming language

The Java programming language provides many useful data containers. Data containers can be divided into two categories: one is collection, such as LinkedList and Stack; and the other is map, such as the type HashMap. Both are commonly used in practical development and also frequently met in interviews. For example, the following is an interview question about HashMap: What is the output of the piece of code in Listing 2-19?
Listing 2-19. Java Code with HashMap
public class MyString {
    public MyString(String data) {
        this.data = data;
    }
    private String data;
}
public static void main(String args[]) {
    Map<String, Integer> map1 = new HashMap<String, Integer>();
    String str1 = new String("Hello World.");
    String str2 = new String("Hello World.");
    map1.put(str1, new Integer(10));
    map1.put(str2, new Integer(20));
    Map<MyString, Integer> map2 = new HashMap<MyString, Integer>();
    MyString str3 = new MyString(str1);
    MyString str4 = new MyString(str2);
    map2.put(str3, new Integer(10));
    map2.put(str4, new Integer(20));
    System.out.println(map1.get(str1));
    System.out.println(map2.get(str3));
}
Java checks the existence of a key in a HashMap via its hash code, which is returned by the method hashCode. The method hashCode is defined in the class Object, and it can be overridden by subclasses. When two keys have the same hash code, it calls the method equals to check their equality. Similar to hashCode, equals is also defined in the class Object and can be overridden by subclasses.
The type of key in the map1 is String, which overrides the method hashCode and equals. The method String.hashCode returns the same hash code when two instances have the same string content. Because the contents in str1 and str2 are the same, they share the same hash code. When it tries to put the record with key str2 to map1, a key str1 exists already with the same hash code. The method equals also shows that these two keys are equal to each other because their contents are the same. Therefore, it just updates the corresponding value of the key str1 to 20, instead of inserting a new record.
The type of key in map2 is MyString, which does not override the methods hashCode and equals. It has to call the method Object.hashCode to compare hash codes of keys, which returns the object addresses. The keys str3 and str4 have different hash codes because they are two different objects and have different addresses. A new record with key str4 is inserted into the map2, and the value responding to the key str3 remains 10.
Therefore, the output of the code above contains two lines: The first line is a number 20, and the second one is a number 10.Java has good support for threads and synchronization.
www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

No comments:

 
Top Blogs