![]() ![]() If there is a need to maintain the insertion order irrespective of the duplicity then List is a best option. If the requirement is to have only unique values then Set is your best bet as any implementation of Set maintains unique values only. The usage is purely depends on the requirement: We can use Iterator (It works with List too) to traverse a Set.Ħ) List interface has one legacy class called Vector whereas Set interface does not have any legacy class. Set can have only a single null value at most.ĥ) ListIterator can be used to traverse a List in both the directions(forward and backward) However it can not be used to traverse a Set. Set implementations: HashSet, LinkedHashSet, TreeSet etc.Ĥ) List allows any number of null values. ![]() All the elements of a Set should be unique if you try to insert the duplicate element in Set it would replace the existing value.ģ) List implementations: ArrayList, LinkedList etc. There are few implementations of Set which maintains the order such as LinkedHashSet (It maintains the elements in insertion order).Ģ) List allows duplicates while Set doesn’t allow duplicate elements. Set is an unordered collection, it doesn’t maintain any order. List Vs Setġ) List is an ordered collection it maintains the insertion order, which means upon displaying the list content it will display the elements in the same order in which they got inserted into the list. In this post we are discussing the differences between List and Set interfaces in java.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |