What is the difference between set and list interface




















Vote count: 4. No votes so far! Be the first to rate this post. Primary Primary. Skip to content. This post will provide an overview of some of the major differences between these two interfaces: List in Java represents an ordered sequence of elements, and its elements can be accessible by index. Set in Java represents a distinct collection of elements that can be either ordered or unordered, depending on the implementation.

For example, HashSet implementation is unordered, LinkedHashSet implementation is ordered, and TreeSet implementation is ordered by natural order or by provided comparator. Another notable difference between List and Set interface is that the Set contains only distinct elements, whereas List allows duplicate elements. List allows any number of null values while a set can contain at most one null element.

Duplicate item will be ignored in Set and it will not print in the final output. Map represents a mapping between a key and a value. More specifically, a Java Map can store pairs of keys and values. Each key is linked to a specific value. Once stored in a Map, you can later look up the value using just the key.

Entry m : map. Map The list interface allows duplicate elements Set does not allow duplicate elements. The map does not allow duplicate elements The list maintains insertion order. Set do not maintain any insertion order. The map also does not maintain any insertion order.

We can add any number of null values. But in set almost only one null value. The map allows a single null key at most and any number of null values. That's all about the difference between Set, List, and Map in Java.

All three are the most fundamental interface of the Java Collection framework and any Java developer should know their distinguish feature and given a situation should be able to pick the right Collection class to use. It's also good to remember the difference between there implementation e. Collection API is huge and it's difficult to know every bits and piece but at the same time, there is no excuse for not knowing fundamentals like the difference between Set, List, and Map in Java.

Thanks for reading this article so far. If you find this Java interview question interesting and my explanation useful then please share it with your friends and colleagues. If you have you have any questions or feedback then please drop a note. Labels: core java , core java interview question answer , Java collection tutorial. Anonymous June 10, at PM. Anonymous September 8, at AM.

Anonymous March 10, at PM. Anonymous April 21, at AM. Anonymous May 10, at AM. Anonymous May 22, at PM. Anonymous June 3, at AM. Anonymous June 15, at AM. Anonymous October 8, at AM. Anonymous January 22, at PM. Anonymous June 7, at AM. Anonymous June 17, at PM. Anonymous June 29, at AM. Sagar June 30, at AM. Anonymous July 10, at AM. Unknown December 27, at PM. Unknown December 20, at AM. Anonymous March 1, at AM. Basant Kumar May 7, at AM. Anonymous June 20, at AM. Unknown June 20, at PM. Anonymous October 18, at PM.



0コメント

  • 1000 / 1000