Rule 1: The connection between hashCode() and equals():
If x.equals(y), then x.hashCode() must be same as y.hashCode().
Rule 2: Overwriting hashCode() or equals():
If you overwrite one of these functions, you have to overwrite the second too.
When you need to overwrite equals?
Showing posts with label java collection. Show all posts
Showing posts with label java collection. Show all posts
Wednesday, December 12, 2012
hashCode() and equals()
Thursday, November 29, 2012
Collections questions
Differences between Iterator and Enumeration?
Iterator duplicate functionality of Enumeration with one addition of remove() method and both provide navigation functionally on objects of Collection.
Difference between Set and List in Java?
In Set the order of the elements in not saved.Set doesn't allowed duplicate while List does and List maintains insertion order while Set does not. Set uses the method equals() internally, not ==.
How do you Sort objects on collection?
The class Collections has functionality for sorting collections. There are two most used methods for this:
Collections.sort() - called without Comparator. It uses Comparable interface in Java. The order is the default , which is specified by CompareTo method;
Collections.sort(Comparator) will sort objects based on compare() method of Comparator. See Sorting in Java using Comparator and Comparable for more details.
Iterator duplicate functionality of Enumeration with one addition of remove() method and both provide navigation functionally on objects of Collection.
Difference between Set and List in Java?
In Set the order of the elements in not saved.Set doesn't allowed duplicate while List does and List maintains insertion order while Set does not. Set uses the method equals() internally, not ==.
How do you Sort objects on collection?
The class Collections has functionality for sorting collections. There are two most used methods for this:
Collections.sort() - called without Comparator. It uses Comparable interface in Java. The order is the default , which is specified by CompareTo method;
Collections.sort(Comparator) will sort objects based on compare() method of Comparator. See Sorting in Java using Comparator and Comparable for more details.
Tuesday, November 27, 2012
Similarities and differences between HashMap, Hashtable, ConcurrentHashMap and synchronizedMap
There are several similarities and differences between HashMap, Hashtable, ConcurrentHashMap and synchronizedMap in Java. (I name synchronizedMap the object retrieved by Collections.synchronizedMap(map) method.)
Monday, November 26, 2012
Fail-fast vs fail-safe iterators
Fail-fast iterators
"Fail-fast iterator" means that if the collection class is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException.
Throwing ConcurrentModificationException is not granted, so you have NOT to relay on it. ConcurrentModificationException is used to show the developer that his/her classes are not synced.
Most ifiterators in Java are fail-fast, these found in classes:
- LinkedList
- ArrayList
- Vector
Fail-safe iterators
Fail-safe iterator doesn't throw any Exception if Collection is modified structurally.
"Fail-fast iterator" means that if the collection class is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException.
Throwing ConcurrentModificationException is not granted, so you have NOT to relay on it. ConcurrentModificationException is used to show the developer that his/her classes are not synced.
Most ifiterators in Java are fail-fast, these found in classes:
- LinkedList
- ArrayList
- Vector
Fail-safe iterators
Fail-safe iterator doesn't throw any Exception if Collection is modified structurally.
Labels:
fail-fast,
fail-safe,
iterators,
java,
java collection
Advanced view over Concurrent collections
A nice article for java syncronization from IBM developers:
http://www.ibm.com/developerworks/java/library/j-jtp07233/index.html
http://www.ibm.com/developerworks/java/library/j-jtp07233/index.html
Thursday, November 22, 2012
LinkedList, ArrayList & Vector classes - differences
LinkedList, ArrayList & Vector are tree different implementations of the
List interface. LinkedList implements it with a doubly-linked list.
ArrayList & Vector classes implement it with a dynamically resizing array.
Subscribe to:
Posts (Atom)