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
dan1st
dan1st2y ago
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 anyway
Note:
Technically, Map does not extend Collection but it is part of the Collections API.
accepted answer by 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}. If x = {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 saying int a = 2; int b=3 and so on, one is able to write a = {2,3,4}, which will result in a variable a 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 by Sui#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 by wandering11#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 by Prismoid#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 by MinecraftMan1013#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
List<String> names = new ArrayList<>();
names.add("Joe");
names.add("Jerry");
names.add("Joe");

// If you print out this list, the output is ["Joe", "Jerry", "Joe"]
List<String> names = new ArrayList<>();
names.add("Joe");
names.add("Jerry");
names.add("Joe");

// If you print out this list, the output is ["Joe", "Jerry", "Joe"]
Set
Set<String> names = new HashSet<>();
names.add("Joe");
names.add("Jerry");
names.add("Joe");

// If you print out this set, the output is ["Joe", "Jerry"]
Set<String> names = new HashSet<>();
names.add("Joe");
names.add("Jerry");
names.add("Joe");

// If you print out this set, the output is ["Joe", "Jerry"]
Map
Map<String, Integer> people = new HashMap<>();
people.put("Joe", 62);
people.put("Jerry", 53);
people.put("James", 45);

int joesAge = people.get("Joe");

// If you print out joesAge, it will print "62"
Map<String, Integer> people = new HashMap<>();
people.put("Joe", 62);
people.put("Jerry", 53);
people.put("James", 45);

int joesAge = people.get("Joe");

// If you print out joesAge, it will print "62"
best answer by DEEp#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
List al = new ArrayList(); /*an example of arraylist that accepts heter0genous data*/
al.add("hi");
al.add(23);
al.add('A');
System.out.println(al); //[hi,23,A]

Set hs = new HashSet();
hs.add(1);//example showing duplicates not allowed in Set
hs.add(3);
hs.add(3);
System.out.println(hs); // [3,1]

Map m = new HashMap<Integer,String>();
m.put(0,"hi");
m.put(1,"hello");
m.put(2,"deep");
System.out.println(m); //[0=hi 1=hello 2=deep]
List al = new ArrayList(); /*an example of arraylist that accepts heter0genous data*/
al.add("hi");
al.add(23);
al.add('A');
System.out.println(al); //[hi,23,A]

Set hs = new HashSet();
hs.add(1);//example showing duplicates not allowed in Set
hs.add(3);
hs.add(3);
System.out.println(hs); // [3,1]

Map m = new HashMap<Integer,String>();
m.put(0,"hi");
m.put(1,"hello");
m.put(2,"deep");
System.out.println(m); //[0=hi 1=hello 2=deep]
best answer by dan1st#7327: java.util.List, java.util.Set and java.util.Map are important interfaces of the Collections API. They can be used for storing objects. Both List and Set implement the interface Collection 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. In Lists, 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 index size - 1 where size is the number of elements (which can be obtained using the size() method). Elements can be accessed (retrieved, inserted, removed) at specific indices. The same element may occur multiple times in a List.
List<String> someList = new ArrayList<>();//create a List
someList.add("Hello");
someList.add("World");
someList.add("Hello");
System.out.println(someList);//[Hello, World, Hello]
someList.remove(0);//removes the first Hello
System.out.println(someList.get(0));//World
List<String> someList = new ArrayList<>();//create a List
someList.add("Hello");
someList.add("World");
someList.add("Hello");
System.out.println(someList);//[Hello, World, Hello]
someList.remove(0);//removes the first Hello
System.out.println(someList.get(0));//World
Sets are collections of objects that cannot contain duplicates. If one attempts to add an element to a Set which is present already, this operation will be ignored. Sets are not indexed so elements are not (necessarily) accessible by a specific position. Sets 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 the Set they are. Sets are generally unordered.
Set<String> someSet = new HashSet<>();
someSet.add("Hello");
someSet.add("World");
someSet.add("Hello");//does NOT add it twice
System.out.println(someSet);//[Hello, World]
someSet.remove("Hello");
System.out.println(someSet);//[World]
Set<String> someSet = new HashSet<>();
someSet.add("Hello");
someSet.add("World");
someSet.add("Hello");//does NOT add it twice
System.out.println(someSet);//[Hello, World]
someSet.remove("Hello");
System.out.println(someSet);//[World]
Maps 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 the Map using multiple keys. Just like with Sets, elements in a Map are generally unordered.
Map<String, String> someMap = new HashMap<>();
someMap.put("Hello", "World");
someMap.put("Hi", "World");//both "Hello" and "Hi" map to the value "World"
someMap.put("something","else");
System.out.println(someMap);//{Hi=World, Hello=World, something=else}
someMap.put("Hi","people");//this overwrites the entry with key "Hi"
System.out.println(someMap.containsKey("something"));//true
someMap.remove("something");
System.out.println(someMap);//{Hi=people, Hello=World}
Map<String, String> someMap = new HashMap<>();
someMap.put("Hello", "World");
someMap.put("Hi", "World");//both "Hello" and "Hi" map to the value "World"
someMap.put("something","else");
System.out.println(someMap);//{Hi=World, Hello=World, something=else}
someMap.put("Hi","people");//this overwrites the entry with key "Hi"
System.out.println(someMap.containsKey("something"));//true
someMap.remove("something");
System.out.println(someMap);//{Hi=people, Hello=World}
Want results from more Discord servers?
Add your server