Difference between Set, List and Map in Java Interview question

Glad you find this differences useful and like my explanation. In HashSet Heterogeneous elements are allowed, but incase of TreeSet and TreeMap ony Homogeneous elements are allowed…. In order to choose between the first two see the difference between HashSet and HashMap in Java.

List and Set both interface belongs to the Collection framework. They both are used to store a collection of objects java initialize hashmap as a single unit. Set interface is a collection or a group of objects that does not any duplicate object in it.

In this example, we will learn to calculate the difference between two sets in Java. To learn more about collections in Java, check this out. Thought LinkedHashSet maintains insertion order in java. The returned navigable set will be serializable if the specified navigable set is serializable.

TreeSet and HashSet are the two Java classes that implement the interface Set. Regardless which variant of difference you implement, I would use public static Set difference as signature, the method is then usable as generic utility function. In this post we are discussing the differences between List and Set interfaces in java. The garbage collection process is to identify the objects which are no longer referenced or needed by a program so that their resources can be reclaimed and reused. List is an ordered sequence of elements whereas Set is a distinct list of elements which is unordered.

The iterator method returns an Iterator over the Set. The most popular implementations of the List interface in Java are ArrayList, LinkedList, and Vector class. ArrayList is more general-purpose and provides random access with index, while LinkedList is more suitable for frequently adding and removing elements from List. The List implementation classes are LinkedList and Arraylist and the SET implementation classes are Linkhashset, HashSet and Tree set.

Set interface does not define any method in addition to the method defined in Collection. Instead, it restricts the add() and addall() methods of collection to add any duplicate object in a collection. If you try to add any duplicate object in a collection using add() method of Collection it returns false. Set interface is implemented by HashSet, LinkedHashSet, EnumSet, TreeSet, CopyOnWriteArraySet. The add() and remove() methods inherited from Collection that adds or removes the specified element from the index specified in the method argument. List is a kind of array whose size grow as we add elements to the list.

In particular, you can’t use decorators like ListOrderedSet on it, which silently assume that these contracts are fulfilled. The returned view contains all elements of a that are not a member of b. Returns a unmodifiable view of the union of the given Sets. Returns a transformed navigable set backed by the given navigable set.

Set2 may also contain elements not present in set1; these are simply ignored. The iteration order of the returned set matches that of set1. Returns an unmodifiable view of the intersection of two sets. The returned set contains all elements that are contained by both backing sets. Creates an EnumSet consisting of all enum values that are not in the specified collection. This is equivalent to EnumSet.complementOf(java.util.EnumSet), but can act on any input collection, as long as the elements are of enum type.

Similar Posts