group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
255 Members
Join this group to post and comment.
Soubhik Maji
Soubhik Maji
Computer Science
15 Jan 2019

Collections Complete Package (my original post is lost so I am posting one Complete package)

Collections in Java :-


List ------------contains---------indexof----

Arraylist - dynamic array 

 -random access fast 

- insertion deletion slow 


Vector- Synchronized version of arraylist 


Linked list- -faster insertion and deletion , Random access is slow , implements Dequeue 


-------_-------------------------------------------

Set (uses hashcode-prevents index)

Hashset - unsorted unordered


Linkedhashset-ordered ,unsorted 


Treeset-sorted,unordered,(sorted version of hashset so slower )


------_------------------------------------------------

Map (anything started with hash order is not guaranteed)


Sorted map (comparator can be provided)


Hash map -unsorted unordered  ..uses hashcode(keys) ,doesn't allow key with null value 


Hash table -synchronized  hash map ,allows key with null value ,hashcode of key is used


Linkedhashmap- order is maintained, slower insertion and deletion, iteration is faster ,


Treemap- //implements navigatablemap

-sorted 

-is navigatable

-------------------------------------------------------

Queue interface (extends collections) 


Dequeue  interface (extends queue)-insertion and deletion in both sides 


Blockind Queue interface extends queue(you can wait until an element is available or for space in queue to add)


Priority queue-( implements queue) - elements sorted in natural order,smaller numbers has higher priority


Array queue(implements deque)


Blockingqueue(implements blocking queue)


Linkedblockingqueue(implements blocking queue) -better  performance than array based queue 


----------------------------------------------------------

Hashcode and equals() method 


For the elements to be equal 


I. First two elements hashcode will be checked if its same then 


Ii.equals will be checked ,if it's also same ,they are equal 


Contract:If you override one you must override other 

-----_---------------------------------------------7-----------------------------------------

Comparable and comparator


-every class should implement comparable interface for it to be get sorted by inbuilt sort function


-the method name is compareTo(classname ob1)-the first object is this and second is ob1 .. Return -1 if firstsecond 



Comparator- you can pass this in sort function for classes you don't have access (classes that dont implement comparable)to - here method public int  compare(o T1,o T2); we create a instance of this interface using anonymous class and pass it in sort function