본문 바로가기
Language/Java

Java Collections Framework

by Zayne 2023. 5. 25.

The Java Collections Framework is a set of classes and interfaces

that implement commonly reusable collection data structures.

 

This includes List, Set, and Map interfaces, along with their various implementations

like ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap, and others.

 

  • Collection : This is the root interface in the Collection hierarchy. A collection represents a group of objects, known as its elements.
  • List : This interface is an ordered collection(also known as a sequence) and allows duplicates. Elements in a list can be accessed by their integer index(position). ArrayList, LinkedList are popular implementations.
  • Set : A Set is a collection that cannot contain duplicate elements. It models the mathematical set abstraction. HashSet and TreeSet are popular implementations.
  • Map : The Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date. HashMap and TreeMap are popular implementations.

 

1. List

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("Alice");
        list.add("Bob");
        list.add("Charlie");
        System.out.println(list); // prints [Alice, bob, Charlie]
    }
}

 

2. Set

import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<String> set = new HashSet<String>();
        set.add("Alice");
        set.add("Bob");
        set.add("Alice");
        System.out.println(set); // prints [Alice, Bob]
    }
}

 

3. Map

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("Alice", 25);
        map.put("Bob", 30);
        map.put("Charlie", 35);
        System.out.println(map); // prints {Alice=25, Bob=30, Charlie=35}
    }
}

'Language > Java' 카테고리의 다른 글

Generics  (0) 2023.05.29
replace, replaceAll  (0) 2023.05.25
Java access modifiers  (0) 2023.05.24
Super  (0) 2023.05.11
다형성(Polymorphism)  (0) 2023.05.07