Week 11 — What are the difference (and similarities) between Set, List & Map?
Question of the Week #11
What are the difference (and similarities) between Set, List & Map?
1 Reply
accepted answer by
Piotr Łyszkowski#0456
:
Set - not reapeted objects in collection List - depends on implementation, unsorted objects in colection Map - Structure included key and value, not repeated objects in collection e.g Map<Integer, String> map = new HashMap<>();accepted answer by
BlueTree242#9734
:
Set, List & Map are collections in java they are all used to store data, however each of them is unique on how it does it, for example Set Stores a set of objects, but without repeating the value, and without an order, and List is same as Set but the values are stored in an order and repeating is fine, but Map is storing a key and a value, where key is not repeatable however the value is repeatable. They all extend Collection anywayNote:
Technically,accepted answer byMap
does not extendCollection
but it is part of the Collections API.
MilaDog#1234
:
1) Set is a abstract data type that hold values of the same primitive data type, as well as not having duplicate values.x = {2,3,4,5,5,6}
as a SET would have the resulting value :{2,3,4,5,6}
. Ifx = {2,3,4,5,'!',7}
, and a set was made, an error would be thrown due to the set having more than one primitive data type. 2) List is a sequence of data that all share a primitive data type. Usually stored under a single variable. Instead of sayingint a = 2; int b=3
and so on, one is able to writea = {2,3,4}
, which will result in a variablea
that holds the values[2,3,4]
. A list can be iterated. 3) Map is a function that allows for an iterable to be passed, as well as a resulting data type. A new iterable of the specific data type is returned. Having a list of integers-[97,98,99]
- if we map the list to be a list of characters, we would have the result['a','b','c']
accepted answer bySui#7785
: Sets are collections of unique elements. Lists are collections of elements. And maps are combined version of both where the keys are sets and the values can be various things even lists or other sets. accepted answer bywandering11#0307
: Talking about the difference first : A list can accept same identities , whereas as we cannot input the same data again in a set or a map. Similarities : All three don't have a fixed size , can be changed over the course of the code. All are part of the java collections framework. accepted answer byPrismoid#3448
: Set is a list,but it eliminates the duplicates in the set. A list is a lost of items that has an unlimited amount of items it can store,as long as there is more memory. And it can have duplicates Map is a list, but there is a second identifier that you can grab the thing in the list with Set<Object> set = new HashSet<>(); List<Object> list = new ArrayList<>(); Map<String, Object> map = new HashMap<>(); best answer byMinecraftMan1013#7242
: A list is a list of values. The type of value stored in the list must be specified on initialization. A set is a list that can only contain one of each value A map is a list that has a key and a value. List Set Map best answer byDEEp#5953
: hi this is my first answer here i'll give my best Set , List and Map all three are interfaces Set and List are child interfaces of Collection Interface where as Map is not related to Collection interface or we can say that set and list interfaces extend Collection Interface 1. List-------> in list ----insertion order is preserved -----and duplicates are allowed methods in List interface gets implemented by --ArrayList class LinkedList class Vector class(a legacy class ) 2.Set--------> in set--- insertion order is not preserved ---and duplicates are not allowed methods in Set interface can get implemented by -- HashSet ----LinkedHashSet 3.Map---------> duplicates are not allowed in keys(null cant be key) duplicates are allowed in values(null can be used in value) Map Interface can get implemented by--HashMap ----LinkedHashMap ---HashTable SortedMap extends Map interface TreeMap can implement Sorted Map interface in sorted map or tree map elements are inserted in some sorting order default natural sorting order being ascending for numbers and alphabetical for strings examples of List ,Set and Map heterogenous data is not allowed in treeMap best answer bydan1st#7327
:java.util.List
,java.util.Set
andjava.util.Map
are important interfaces of the Collections API. They can be used for storing objects. BothList
andSet
implement the interfaceCollection
and allow (amongst other things) to add, remove objects, check whether an object is in the collection and to iterate through all objects stored in it. InList
s, each entry is stored at a unique position called index (informally the position of the element). The first element is stored at index 0 and the last element is stored at indexsize - 1
wheresize
is the number of elements (which can be obtained using thesize()
method). Elements can be accessed (retrieved, inserted, removed) at specific indices. The same element may occur multiple times in aList
.Set
s are collections of objects that cannot contain duplicates. If one attempts to add an element to aSet
which is present already, this operation will be ignored.Set
s are not indexed so elements are not (necessarily) accessible by a specific position.Set
s are typically used for storing data where it is only important what elements are in it and what elements aren't but not where in theSet
they are. Sets are generally unordered.Map
s provide a key-value storage. It is possible to put an object (value) into the map which is uniquely identified by another object (key). Each key identifies exactly one value but the same value can be present in theMap
using multiple keys. Just like withSet
s, elements in aMap
are generally unordered.