Thursday, 29 October 2015
Wednesday, 28 October 2015
Tuesday, 27 October 2015
Monday, 26 October 2015
Tuesday, 20 October 2015
JAVA : HashSet -> Equals and hashCode
JAVA : HashSet -> Equals and hashCode
1. In HashSet(value) -> if we are adding string or int as then no need of equals and hascode override, If we are using object (Eg : Employee) as then we need to override equals and hashcode method.
Below screen shot, since we are going to use Employee object in HashSet, If we dont override hashCode method how HashSet behaves.
WithOut hashCode override:
With hashCode override:
Ref: https://www.youtube.com/watch?v=IwUwIrz9Ge8
JAVA : HashMap -> Equals and HashCode
JAVA : HashMap -> Equals and HashCode
1. In HashMap (key, value) -> if we are adding string or int as key then no need of equals and hascode override, If we are using object (Eg : Employee) as a key then we need to override equals and hashcode method.
Below screen shot, since we are going to use Employee object as a key in HashMap, If we dont override hashCode method how HashMap behaves.
WithOut hashCode override:
With hashCode override:
Ref : https://www.youtube.com/watch?v=IwUwIrz9Ge8
Monday, 19 October 2015
Thursday, 15 October 2015
JAVA : COLLECTION Tips
1. Check Duplicate in collection.
2. Want to add objects in order
3. SORT HASHTABLE -> JUST CONVERT HASHTABLE TO TREEMAP (Automatically will be sorted)
public static void main (String[] args) {
Hashtable<String, String> ht= new Hashtable<String, String>();
ht.put("zac", "Chaitanya");
ht.put("abc", "Ajeet");
ht.put("yyt", "Test");
ht.put("ddc", "Demo");
ht.put("ssa", "Anuj");
Map<String, String> map = new TreeMap<String, String>(ht);
for(Map.Entry<String,String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " => " + value);
}
OR
Map hm = new TreeMap(ht);
Set<String> keys = hm.keySet();
for(String key: keys){
System.out.println("Value of "+key+" is: "+hm.get(key));
}
}
Subscribe to:
Posts (Atom)