CodeGym/Courses/New Java Syntax/Lesson about collections

Lesson about collections

Available

"Hi, Amigo. I'd like to tell you about collections today. In Java, a collection/container means a class whose main purpose is to store a collection of other elements. You already know one such class: ArrayList."

"In Java, collections are divided into three main groups: Set, List, Map."

"What is the difference between them?"

"Let me start with Set. Imagine many shoes thrown into a pile. This is a Set. You can add an element to a Set, search for it, or delete it. The important thing to note is that the set elements don't have a specific assigned order."

Lesson about collections - 1

"That's not much to go on…"

"Now imagine the same pile of shoes arranged neatly along the wall. Now there is order. Each element has its own number. You could simply grab Pair No. 4 based on its number (index). This is a List. You can add an element to the beginning or the middle of a list, or remove an element – simply by using its index."

Lesson about collections - 2

"I see. What about a Map?"

"Imagine the same shoes, but now each pair has a note with a name: 'Nick', 'Vic' or 'Anna'. This is a Map (also often called a dictionary). Each element has its own unique name which is used to address it. This unique name for each element is often called a 'key'. Thus, a Map is a set of key-value pairs. The key doesn't have to be a string: it can be any type. A Map whose keys are Integer is in fact a List (with some differences)."

Lesson about collections - 3

"I more or less understand, but I'd like to see more examples."

"Rishi will give you examples, but I'd like to add a few words."

"Immediately after being created, collections and containers don't store anything, but you can add elements into them one by one. And if you do, their size will change dynamically."

"Now that's interesting. How do I know how many elements a collection contains?"

"You have the size() method for that. Collections have everything you might need. I believe that after a few more lessons you'll see for yourself just how convenient collections are."

"I hope so, Ellie."

Comments (17)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Fadhil Radhian Software Engineer at Accenture
25 March 2023, 09:43
well done in giving proper examples. This is why I love CodeGym
Austin Okojie
Level 8 , London, Canada
14 July 2020, 12:38
Is a list similar to an ArrayList?
Mateusz
Level 29 , Poland
27 August 2020, 06:37
There are different Lists and an ArrayList is one of them. An ArrayList is a child class of a List (if I get it right).
Michaela Obertova
Level 30 , Palo Alto, United States
22 March 2021, 18:19
I am a bit late probably, but List isn't a class but an interface. ArrayList is one of several classes which implement the List interface, not its child class.
Madhu
Level 10 , Chennai, India
2 June 2020, 12:07
The examples really help in understanding the concepts. Great stuff!
Fido Lost
Level 8 , Timmins, Canada
17 May 2019, 21:55
One more feature about Sets is that no two or more elements can be the same. The members of a Set, mathematically, or otherwise, are unique or distinct from one another. So, for example, if you try and add duplicate String objects you'll see the duplicates being omitted from the collection. The super class Set is abstract and cannot be implemented directly, so, I'm using the concrete HashSet collection class extended from Set in this example:
import java.util.Arrays;
import java.util.HashSet;

public class Solution {
    public static void main(String[] args) {

        HashSet<String> uniqueExamples = new HashSet<>(Arrays.asList("one", "one", "two", "two"));

        for (String example : uniqueExamples) System.out.println(example);
        //Outputs:
        //one
        //two

    }
}
Cheers. Happy Learnin'!
Anastasia Falcon
Level 8 , Rio de Janeiro, Brazil
29 April 2019, 05:52
Is array the same as list?
Davor Kandic
Level 9 , Beograd, Serbia
6 May 2019, 17:37
No, but their functions are similar: list is like dynamic array(it's size is changeable, and size of array is fixed).
Anastasia Falcon
Level 8 , Rio de Janeiro, Brazil
11 May 2019, 18:29
thanks
Jonaskinny Java Developer at Sandmedia
17 February 2022, 15:38
0 to n arrays may be used internally to manage state within a List, but a List is an interface, so its the visible methods that one would use to access the data it contains.
Hrvoje
Level 17 , Zagreb, Croatia
21 April 2019, 23:31
Collections are awesome \o/
Renat Mukhametshin
Level 16 , Pervouralsk, Russain Federation
19 April 2019, 09:32
very good, so: set list map, and map ~ list (if the key type is an Integer)
Johan
Level 22 , La Paz, Bolivia, Plurinational State of
26 March 2019, 14:33
Good explanation.
Ashutosh Sharma
Level 8 , Delhi, India
23 March 2019, 07:47
Brilliant explanation about Set, List and Map.
Darko Jakimovski
Level 18 , Kriva Palanka, Macedonia, The Former Yugoslav Republic of
29 January 2019, 08:57
My first time encountering this topic. Explained pretty good! Code gym - Intergalactic mega imperators!
Davor Kandic
Level 9 , Beograd, Serbia
6 May 2019, 17:39
Are you referencing to RASMC? :)