Saturday 13 January 2018

What are classes implementing Set interface?

We have three classes implementing set interface in java Collection framework hierarchy. Look at the diagram given below:

set interface java radar


From the diagram its clear that classes implementing set interface are:

1. Hashset

HashSet implements Set interface and extends AbstractSet class. It is part of java.util package.
HashSet contain unique elements only. If you add duplicate in it, previous value will be overwritten.
HashSet allows one null value only.
HashSet doesn’t maintain insertion order so when you retrieve elements from it they may be returned in random order. If you want to get element in order of insertion, use LinkedHashSet.

For more details about Treeset follow link Hashset in java


2. LinkedHashset

LinkedHashSet implements Set interface and extends HashSet class. It is part of java.util package.

LinkedHashSet is almost similar to HashSet except that it maintains the insertion order of the elements.

For more details about Treeset follow link LinkedHashset in java


3. Treeset

Treeset class implements set interface and extends AbstractSet class.
When storing element in treeset, it stores them in ascending order by default.

For more details about Treeset follow link Treeset in java

Tuesday 9 January 2018

What are the classes implementing List interface?

We have three classes implementing list interface in java Collection framework hierarchy. Look at the diagram given below:

list interface java radar



From the diagram its clear that classes implementing list interface are:


1. ArrayList
  • Arraylist was added in Java 1.2 version.
  • Arraylist uses grow able array and thus can grow automatically.
  • Arraylist in java is not synchronized or thread safe.
  • As arraylist is non-synchronized so it is better performance wise as multiple threads can access the resources of arraylist at the same time simultaneously.
  • Arraylist's  iterator and list iterator are fail fast in nature.
  • Follow the link to read in detail about arraylist

Read: Why iterator on arraylist is fail fast in java?


2. LinkedList
  • Linkedlist was added in Java 1.2 version
  • Linkedlist in java is not synchronized
  • Linkedlist stores element in nodes which has capability to store element and its address.
  • Follow the link to read in detail about linkedlist

Read: Difference Arraylist vs Linkedlist


3. Vector
  • Vector is synchronized in nature. This is the major difference between array list and vector.
  • Vector is a legacy class and is not recommended to be used at present.
  • Follow the link to read in detail about vector


You may also like to read about:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.