list set and map

7
When to use Set, List and Map?

Upload: bharat-savani

Post on 15-Jan-2017

95 views

Category:

Education


2 download

TRANSCRIPT

Page 1: List set and map

When to use Set, List and Map?

Page 2: List set and map

• Set, List and Map are very important interfaces in Java Collections Framework.

When to use them also makes a good Java Collections interview question.

Page 3: List set and map

Set.

• Let’s say that we want a container or Collection of unique elements then we go with Set.

• If you want unique elements and insertion order is to be maintained use LinkedHashSet<E>.

• If you want unique elements and elements in natural order use TreeSet<E>.

Page 4: List set and map

List.

• Now, if you want an ordered or sequenced collection then go with List.

• If insertion order is to be maintained and add and remove operations are not frequent then use ArrayList<E> class.

• If insertion order is to maintained and there are frequent add remove operation use LinkedList<E> class.

Page 5: List set and map

Map.

• If you have a pair of Key and Value mapping:– a. And insertion order is not important use

HashMap<K, V>. – b. And insertion order is to be maintained then use

LinkedHashMap<K, V>. – c. And insert it in sorted order then use TreeMap<K,

V>. – d. And insertion order is not important but thread

safety is important then use ConcurrentHashMap<K, V>.

Page 6: List set and map

Quick Reference Chart.

Page 7: List set and map

That’s all folks.