Overriding Equals
1) equals method implementation
public boolean equals(Object o){
if((o instaceof x) && o.value() == this.value())
return true;
else
return false;
}
//Consideration of two different classes as equal is bad.
- if you override equals() then it is better to override hashcode()
2) collections
3) Collection - base class for list, set and queue (except map)
4) Collections - class with Static helper methods.
5) ArrayList - Non-synchronized list having get(int index) getIndex(Object o) method, easier for iteration, faster than Vector.
List<generic> = new ArrayList();
add() to add a value;
remove();
contains();
- AutoBoxing wraps primitive types to their corresponding objects.
- Collection/Array should be sorted before searching.
6) converting arrays to lists and lists to arrays
1) toArray();
2) Arrays.toList();
7) Sets are used when you dont want any duplicate values in your collection.
Set s = new HashSet();
add() , remove(), contain() ;
- To use a treeset the objects should be mutually comparable.
- Always override equals and hashcode methods.
8) Navigation through TreeSets and TreeMaps.
NavigableSet and NavigableMap.
NavigableSet methods - lower(), higher(), ceiliing() and floor() methods introduced as of java6
NavigationMap methods - lowerKey(), higherKey(), ceilingKey() and floorKey();
lower() - gives the element that is lower than specified;
higher() - gives element that is higher than specified.
ceil() - gives element that is equal to or higher than specified.
floor() - gives element that is equal to or higher than specified.
1) equals method implementation
public boolean equals(Object o){
if((o instaceof x) && o.value() == this.value())
return true;
else
return false;
}
//Consideration of two different classes as equal is bad.
- if you override equals() then it is better to override hashcode()
2) collections
3) Collection - base class for list, set and queue (except map)
4) Collections - class with Static helper methods.
5) ArrayList - Non-synchronized list having get(int index) getIndex(Object o) method, easier for iteration, faster than Vector.
List<generic> = new ArrayList();
add() to add a value;
remove();
contains();
- AutoBoxing wraps primitive types to their corresponding objects.
- Collection/Array should be sorted before searching.
6) converting arrays to lists and lists to arrays
1) toArray();
2) Arrays.toList();
7) Sets are used when you dont want any duplicate values in your collection.
Set s = new HashSet();
add() , remove(), contain() ;
- To use a treeset the objects should be mutually comparable.
- Always override equals and hashcode methods.
8) Navigation through TreeSets and TreeMaps.
NavigableSet and NavigableMap.
NavigableSet methods - lower(), higher(), ceiliing() and floor() methods introduced as of java6
NavigationMap methods - lowerKey(), higherKey(), ceilingKey() and floorKey();
lower() - gives the element that is lower than specified;
higher() - gives element that is higher than specified.
ceil() - gives element that is equal to or higher than specified.
floor() - gives element that is equal to or higher than specified.