List set and map

Download List set and map

Post on 15-Jan-2017

75 views

Category:

Education

2 download

TRANSCRIPT

When to use Set, List and Map?

When to use Set, List 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.

Set.Lets 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. If you want unique elements and elements in natural order use TreeSet.

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 class. If insertion order is to maintained and there are frequent add remove operation use LinkedList class.

Map.If you have a pair of Key and Value mapping:a. And insertion order is not important use HashMap. b. And insertion order is to be maintained then use LinkedHashMap. c. And insert it in sorted order then use TreeMap. d. And insertion order is not important but thread safety is important then use ConcurrentHashMap.

Quick Reference Chart.

Thats all folks.