Generics and Collections

     Generics are the feature introduced in Java5. Except primitive, everything in java is an object, extends from java.lang.Object.

toString : overrirde toString() when you want a more mortal to be able to read something about the objects of your class.
System.out.println(s);
                Will give String@a47e0
                                Class      hashcode of the object
Equals:
   Suppose if you use (==) to compare two objects, evaluates to true, only when both the references refer to the same object, because (==) simply looks at the bits in the variable.
  When you need to know if two references are identical use (==). But when you need to know if the objects themselves are equal, use equals() method.
If you don’t override, equals() method, it just compare as (==) references it will compare.
String, wrapper classes overrides the equals() method.

The equals() contract
1). Its reflexive                  =>           x.equals(x)         => true
2). Its symmetric              =>           x.equals(y)         =>y.equals(x)
3). Its transitive                 =>           x.equals(y) & y.equals(z)    => x.equals(z)
4). It should be consistent
5). X.equals(null)              => should be false

HashCode();
Hashcodes are typically used to increase the performance of large collections of data.
The hashcode value of an object is used by some collection classes.
You can think of a object ID number, it isn’t necessarily unique.
Collections such as HashMap and HashSet use the hashcode value of an object to determine how the object should be stored in the collection, and to locate also.

HashCode() contract
1). When invoked from same object, it should be consistent
2). If 2 objects are equal according to the equals method, then calling hascode() should return same integer.
3). If 2 objects hashcode() results are equals, even then equals() method no need equal result.
But producing distinguishing integer results for hashcode() for different objects improves the performance of the collection.

Collections
   It is framework to maintain your larger collections of data.
Basic operations are
1). Add objects to it.
2). Remove objects from the collection
3). Searching for an object.
4). Retrieving an object from the collection
5). Iterating through the collection


Collections comes in four basic flavors
1). Lists                 - Lists of things
2). Sets                 - Unique things
3). Maps              - Things with unique id
4). Queues          - Things arranged by the order in which they are to be processed.

But there are sub-flavors within those four flavors of collections
1). Sorted
2). Unsorted
3). Ordered
4). Unordered

Ordered : When a collection is ordered means you can iterate through the collection in a specific order.
Sorted : A sorted collection means that the order in the collection is determined according to some rules known as sort order.

List Interface : A list cares about index.
All 3 list implementations are ordered by index position.
1). ArrayList : Think of this as a growable array. It gives you fast iteration and fast random access.
2). Vector : Vector is basically the same as arrayList, but vendor methods are synchronized for thread safery.
3). Linked List: The elements are doubly-linked to one another. Iteration is slow, but its good for fast insertion and deletion.

Set Interface : A set cares about uniqueness
1). Hash Set : A Hash set in unsorted, unordered set, use it when you want a collection with no duplicates and you don’t care about the order when you iterate through it.
2). LinkedHashSet : Its an ordered version of HashSet. That maintains a doubly-linked list across all elements use this class when you care about the iteration order.
3). TreeSet : It’s a sorted collection, means all elements will be in ascending order.

Map Interface : A Map cares about unique identities.
You map a unique key and the value.
1). HashMap : HashMap gives you an unsorted, unordered map.
HashMap allows one null key and multiple null values.
2). HashTable : Its like HashMap but the methods are synchronized.
HashTable doesn’t allow any null values or null key.
3). LinkedHashMap : It’s a ordered version of HashMap. Its slower than HashMap but, you can expect faster iterations.
4). TreeMap : It’s a sorted map.

Queue Interface: It cares about FIFO.
The purpose of a priority queue is to create a “Priority In – Priority Out”

To use Collection.sort( c );   ‘c’ must be object of a class that implements comparable interface.
To implement comparable, a class must implement a single method compareTo()
CompareTo() method returns an int
1). Negative if x<y
2). Zero if x==y
3). Positive x>y
By implementing comparator also we can do sort.
Class GenSort implements Comparator{
                compareTo();
}
Collections.sort(duplist, gs);
The Arrays.sort() method is overridden in the same way the collections.sort() method is
1). Arrays.sort(arraytosort);
2). Arrays.sort(arraytosort, comparator)
Arrays.asList() => method copies an array into a list.

Iterating through a list
List d = new ArrayList();
Iterator I = d.iterator();
While(i.hasNext()){
                Dog d = i.next();
}
d.toArray()   => method gives an object array.

Generics
With generics, we can get type safe collections.
ArrayList<Dog> d = new ArrayList<Dog>();
We can get polymorphism also
List<Dog> d = new ArrayList<Dog>();
Declaring & Invoking methds that declare generic  types
Int getCount(List<Animal> l) {  }

Comments

Popular posts from this blog

Hi Friends