โ ์ปฌ๋ ์ ํ๋ ์์ํฌ๋?
โ List๋?
โ๏ธ ArrayList ์์ฑ ๋ฐ ์ด๊ธฐํ
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
}
}
โ add() ๋ฉ์๋๋ก ๋ฐ์ดํฐ ์ถ๊ฐ
public class Main {
public static void main(String[] args) {
List<String> fruits = new ArrayList<String>();
fruits.add("apple");
fruits.add("banana");
fruits.add("cherry");
fruits.add("dragonfruit");
fruits.add("mango");
}
}
}
โ๏ธ ๋ฐ๋ณต๋ฌธ์ผ๋ก ์ถ๋ ฅ
public class Main {
public static void main(String[] args) {
List<String> fruits = new ArrayList<String>();
fruits.add("apple");
fruits.add("banana");
fruits.add("cherry");
fruits.add("dragonfruit");
fruits.add("mango");
for(int i = 0; i < fruits.size(); i++){
System.out.println(fruits.get(i));
}
}
}
โถ๏ธ
apple
banana
cherry
dragonfruit
mango
โ๏ธ for-each๋ฌธ์ผ๋ก ์ถ๋ ฅ
public class Main {
public static void main(String[] args) {
List<String> fruits = new ArrayList<String>();
fruits.add("apple");
fruits.add("banana");
fruits.add("cherry");
fruits.add("dragonfruit");
fruits.add("mango");
for(String fruit : fruits){
System.out.println(fruit);
}
}
}
โถ๏ธ
apple
banana
cherry
dragonfruit
mango
โน๏ธ Iterator
Iterator<String> it = fruits.iterator();
while (it.hasNext()) {
String item = it.next();
System.out.println(item);
}
โ List ์ ๋ ฌํ๊ธฐ
Collections.sort(fruits);
โ List์ ํน์ ๊ฐ์ด ์๋์ง ํ์ธ
true ๋๋ false๋ก ๋ฐํif(fruits.contains("apple")){
System.out.println("apple์ ํฌํจ๋์ด ์์ต๋๋ค.");
}
โ List ์์ ์ญ์ ํ๊ธฐ
public class Main {
public static void main(String[] args) {
List<String> fruits = new ArrayList<String>();
fruits.add("mango");
fruits.add("apple");
fruits.add("dragonfruit");
fruits.add("cherry");
fruits.add("banana");
fruits.remove("apple");
fruits.remove(0);
System.out.println(fruits);
}
}
โถ๏ธ
[cherry, dragonfruit, mango]
โ List ์ ์ฒด ๋น์ฐ๊ธฐ
fruits.clear();
System.out.println(fruits); // ์ถ๋ ฅ: []
โ List๊ฐ ๋น์๋์ง ํ์ธ
if (fruits.isEmpty()) {
System.out.println("๋ฆฌ์คํธ๊ฐ ๋น์ด ์์ต๋๋ค.");
} else {
System.out.println("๋ฆฌ์คํธ์ ์์๊ฐ ์์ต๋๋ค.");
}
โ List ํฌ๊ธฐ ํ์ธ
System.out.println("๋ฆฌ์คํธ ํฌ๊ธฐ: " + fruits.size());
โ List ํ์ฉ ๋ฉ์๋
int index = fruits.indexOf("mango"); // ์์ผ๋ฉด ์ธ๋ฑ์ค, ์์ผ๋ฉด -1 ์ถ๋ ฅ
fruits.set(1, "Orange"); // 1๋ฒ ์ธ๋ฑ์ค ๊ฐ์ "Orange"๋ก ์์
List<String> sliced = fruits.subList(0, 2); // 0๋ฒ๋ถํฐ 1๋ฒ๊น์ง ์ถ์ถ (2๋ ๋ฏธํฌํจ)
โน๏ธ List vs ArrayList
๐ List ํต์ฌ ์์ฝ ์ ๋ฆฌ
| ํญ๋ชฉ | ์ค๋ช |
|---|---|
| ์๋ฃ ๊ตฌ์กฐ | ์์๋ฅผ ์ ์งํ๋ฉฐ ์ค๋ณต์ ํ์ฉํ๋ ์ปฌ๋ ์ |
| ๋ํ ๊ตฌํ์ฒด | ArrayList, LinkedList |
| ๋ฐ์ดํฐ ์ถ๊ฐ | add() |
| ๋ฐ์ดํฐ ์ญ์ | remove(), clear() |
| ๊ฐ ํ์ธ | contains(), indexOf() |
| ์ ์ฒด ํ์ | for๋ฌธ, for-each๋ฌธ |
| ์ ๋ ฌ | Collections.sort() |
| ์ํ ํ์ธ | size(), isEmpty() |
๐ธ1. new ArrayList<>()
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
๐ธ2. Arrays.asList(...)
List<String> list = Arrays.asList("apple", "banana");
java.util.Arrays class asList(...) method
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a); // ์๋ก์ด ArrayList ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
}
๐ธ3. List.of(...)
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
java.util.List interface of(...) method
static <E> List<E> of() {
return (List<E>) ImmutableCollections.EMPTY_LIST; // ๋ถ๋ณ ๋ฆฌ์คํธ ๋ฐํ.
}
๐ธ4. new ArrayList<>(List.of(...))
List<String> list = new ArrayList<>(List.of("apple", "banana"));
list.add("cherry"); // โ
OK
๐ธ5. Collections.nCopies(n, value)
List<String> list = new ArrayList<>(Collections.nCopies(5, "apple"));
// [apple, apple, apple, apple, apple]
๐ธ6. Stream + Collectors.toList()
List<Integer> list = IntStream.rangeClosed(1, 5)
.boxed()
.collect(Collectors.toList());
// [1, 2, 3, 4, 5]
๐ก(์ฐธ๊ณ ) Arrays.asList() ์ List.of() ์ฐจ์ด ์ถ์ฒ: [Inpa Dev ๐จโ๐ป:ํฐ์คํ ๋ฆฌ]
โ Set์ด๋?
HashSet, LinkedHashSet, TreeSet ๋ฑ์ด ๋ํ์ ์ธ Set ๊ตฌํ์ฒดโ HashSet ์์ฑ
import java.util.*;
public class Main {
public static void main(String[] args) {
Set<String> fruits = new HashSet<>();
}
}
โ add() ๋ฉ์๋๋ก ๋ฐ์ดํฐ ์ถ๊ฐ
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple"); // ์ค๋ณต ์ถ๊ฐ ๋ฌด์
โ for-each๋ฌธ์ผ๋ก ์ํ
for (String fruit : fruits) {
System.out.println(fruit);
}
โ๏ธ List์์ ๋น๊ต ์ค์ต
List<String> list = new ArrayList<>();
Set<String> set = new HashSet<>();
list.add("Apple");
list.add("Apple");
set.add("Apple");
set.add("Apple");
System.out.println(list); // [Apple, Apple]
System.out.println(set); // [Apple]
โ๏ธ Hash ๊ฐ ํ์ธ ์ค์ต
hashCode() ๊ฐ์ ์ด์ฉํด ์ ์ฅ ์์น๋ฅผ ๊ฒฐ์ ํฉ๋๋ค์๋ ์ฝ๋๋ ๋ฌธ์์ด ๋ ๊ฐ์ ํด์ ๊ฐ์ ์ถ๋ ฅํ๋ ์์ ์ ๋๋ค.
String a = "Apple";
String b = "Banana";
System.out.println(a.hashCode());
System.out.println(b.hashCode());
โ LinkedHashSet์ด๋?
LinkedHashSet์ HashSet๊ณผ ๋์ผํ๊ฒ ์ค๋ณต์ ํ์ฉํ์ง ์์ง๋ง, ์
๋ ฅ ์์๋ฅผ ์ ์งํ๋ ํน์ง์ด ์์ต๋๋ค.Set<String> linkedSet = new LinkedHashSet<>();
linkedSet.add("Banana");
linkedSet.add("Apple");
linkedSet.add("Cherry");
System.out.println(linkedSet); // [Banana, Apple, Cherry]
โ TreeSet์ด๋?
TreeSet์ ์์๋ฅผ ์๋์ผ๋ก ์ ๋ ฌํ๋ Set์
๋๋ค.HashSet๋ณด๋ค ๋๋ฆฌ์ง๋ง, ์ ๋ ฌ๋ ๋ฐ์ดํฐ๊ฐ ํ์ํ ๊ฒฝ์ฐ ์ ์ฉํฉ๋๋ค.Set<String> treeSet = new TreeSet<>();
treeSet.add("Banana");
treeSet.add("Apple");
treeSet.add("Cherry");
System.out.println(treeSet); // [Apple, Banana, Cherry]
โ๏ธ HashSet vs LinkedHashSet vs TreeSet
| ๊ตฌ๋ถ | HashSet | LinkedHashSet | TreeSet |
|---|---|---|---|
| ์ค๋ณต ํ์ฉ | โ ์์ | โ ์์ | โ ์์ |
| ์์ ์ ์ง | โ ์์ (๋ฌด์์) | โ ์ ๋ ฅ ์์ ์ ์ง | โ ์ ๋ ฌ๋ ์์ ์ ์ง |
| ์ ๋ ฌ | โ ์์ โ ์์ | โ ์๋ ์ ๋ ฌ (์์ฐ ์์ ๋๋ Comparator) | |
| ์๋ ํน์ง | ๊ฐ์ฅ ๋น ๋ฆ (O(1)) | ์ค๊ฐ (์์ ์ฒ๋ฆฌ ํฌํจ) | ๊ฐ์ฅ ๋๋ฆผ (์ ๋ ฌ ๋น์ฉ) |
| ์ฌ์ฉ ๋ชฉ์ | ๋น ๋ฅธ ์ฝ์ /๊ฒ์ | ์์ + ์ค๋ณต ์ ๊ฑฐ | ์ ๋ ฌ + ์ค๋ณต ์ ๊ฑฐ |
๐ Set ํต์ฌ ์์ฝ ์ ๋ฆฌ
| ํญ๋ชฉ | ์ค๋ช |
|---|---|
| ์๋ฃ ๊ตฌ์กฐ | ์์ ์์ด ์ค๋ณต์ ํ์ฉํ์ง ์๋ ์ปฌ๋ ์ |
| ๋ํ ๊ตฌํ์ฒด | HashSet, LinkedHashSet, TreeSet |
| ์ค๋ณต ์ฌ๋ถ | ์ค๋ณต ํ์ฉ โ |
| ์์ ๋ณด์กด | HashSet โ, LinkedHashSet โ , TreeSet โ (์ ๋ ฌ) |
| ์ ๋ ฌ ์ง์ | TreeSet๋ง ์๋ ์ ๋ ฌ (์ค๋ฆ์ฐจ์) |
| ๋ฐ์ดํฐ ์ถ๊ฐ | add() |
| ๋ฐ์ดํฐ ์ ๊ฑฐ | remove(), clear() |
| ๊ฐ ํ์ธ | contains() |
| ์ ์ฒด ํ์ | for-each๋ฌธ ์ฌ์ฉ ๊ถ์ฅ |
โ Map์ด๋?
โ Map ์์ฑ ๋ฐ ์ด๊ธฐํ
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<String, Integer> fruitPrices = new HashMap<>();
}
}
โ
put()์ผ๋ก ๊ฐ ์ถ๊ฐ
fruitPrices.put("Apple", 150);
fruitPrices.put("Banana", 50);
fruitPrices.put("Cherry", 200);
โ
entrySet()์ผ๋ก ์ ์ฒด ์ํ
for (Map.Entry<String, Integer> entry : fruitPrices.entrySet()) {
System.out.println(entry.getKey() + " costs " + entry.getValue() + " cents.");
}
โ
values() + size()๋ก ํ๊ท ๊ณ์ฐ
int sum = 0;
for (int score : studentScores.values()) {
sum += score;
}
double average = (double) sum / studentScores.size();
System.out.println("Average score: " + average);
โ
containsKey() & containsValue()
containsKey(key)๋ ํด๋น ํค๊ฐ ์กด์ฌํ๋์ง ํ์ธํฉ๋๋ค.containsValue(value)๋ ํด๋น ๊ฐ์ด ์กด์ฌํ๋์ง ํ์ธํฉ๋๋ค.boolean ๊ฐ์ ๋ฐํํ๋ฉฐ ์กฐ๊ฑด๋ฌธ์ ์์ฃผ ์ฌ์ฉ๋ฉ๋๋ค.Map<String, Integer> fruitPrices = new HashMap<>();
fruitPrices.put("Apple", 150);
fruitPrices.put("Banana", 50);
System.out.println(fruitPrices.containsKey("Apple")); // true
System.out.println(fruitPrices.containsKey("Grapes")); // false
System.out.println(fruitPrices.containsValue(50)); // true
System.out.println(fruitPrices.containsValue(300)); // false
โ
getOrDefault() ์ค์ต
getOrDefault(key, defaultValue)๋ ํด๋น ํค๊ฐ ์กด์ฌํ๋ฉด ๊ฐ์ ๋ฐํํ๊ณ , ์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ์ ๋ฐํํฉ๋๋ค.Map<String, Integer> fruitPrices = new HashMap<>();
fruitPrices.put("Apple", 150);
fruitPrices.put("Banana", 50);
System.out.println(fruitPrices.getOrDefault("Apple", 0)); // 150
System.out.println(fruitPrices.getOrDefault("Grapes", 0)); // 0 (๊ธฐ๋ณธ๊ฐ)
โ replace() & remove() ์ค์ต
replace(key, newValue)๋ ๊ธฐ์กด ํค์ ๊ฐ์ ์๋ก์ด ๊ฐ์ผ๋ก ๊ต์ฒดํฉ๋๋ค.remove(key)๋ ํด๋น ํค์ ๊ทธ์ ์ฐ๊ฒฐ๋ ๊ฐ์ Map์์ ์ ๊ฑฐํฉ๋๋ค.Map<String, Integer> fruitPrices = new HashMap<>();
fruitPrices.put("Apple", 150);
fruitPrices.put("Banana", 50);
fruitPrices.replace("Apple", 180); // Apple ๊ฐ๊ฒฉ์ 180์ผ๋ก ๋ณ๊ฒฝ
fruitPrices.remove("Banana"); // Banana ํญ๋ชฉ ์ญ์
System.out.println(fruitPrices); // ์ถ๋ ฅ: {Apple=180}
โ keySet()์ผ๋ก ํค๋ง ์ถ๋ ฅํ๊ธฐ
keySet() ๋ฉ์๋๋ Map์ ์กด์ฌํ๋ ๋ชจ๋ ํค(Set ํํ) ๋ฅผ ๋ฐํํฉ๋๋ค.Map<String, Integer> fruitPrices = new HashMap<>();
fruitPrices.put("Apple", 150);
fruitPrices.put("Banana", 50);
fruitPrices.put("Cherry", 200);
for (String key : fruitPrices.keySet()) {
System.out.println("Key: " + key);
}
โ TreeMap ์๊ฐ ๋ฐ ์ ๋ ฌ๋ ์ถ๋ ฅ
TreeMap์ ํค๋ฅผ ์๋์ผ๋ก ์ ๋ ฌ๋ ์์๋ก ์ ์ฅํ๋ Map ๊ตฌํ์ฒด์
๋๋ค.Comparator ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ๋ฉ๋๋ค.HashMap ๋์ TreeMap์ ์ฌ์ฉํ ์ ์์ต๋๋ค.Map<String, Integer> fruitPrices = new TreeMap<>();
fruitPrices.put("Banana", 50);
fruitPrices.put("Apple", 150);
fruitPrices.put("Cherry", 200);
for (Map.Entry<String, Integer> entry : fruitPrices.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
๐ Map ํต์ฌ ์์ฝ ์ ๋ฆฌ
| ํญ๋ชฉ | ์ค๋ช |
|---|---|
| ์๋ฃ ๊ตฌ์กฐ | ํค(Key)์ ๊ฐ(Value) ์์ผ๋ก ๊ตฌ์ฑ๋ ์ปฌ๋ ์ |
| ๋ํ ๊ตฌํ์ฒด | HashMap, TreeMap, LinkedHashMap |
| ํค ์ค๋ณต ์ฌ๋ถ | ์ค๋ณต ํ์ฉ โ (๊ณ ์ ํด์ผ ํจ) |
| ๊ฐ ์ค๋ณต ์ฌ๋ถ | ์ค๋ณต ํ์ฉ โ |
| ์ ๋ ฌ ์ง์ | TreeMap์ ํค ๊ธฐ์ค ์๋ ์ ๋ ฌ |
| ๋ฐ์ดํฐ ์ถ๊ฐ | put(key, value) |
| ๋ฐ์ดํฐ ์กฐํ | get(key), getOrDefault(key, ๊ธฐ๋ณธ๊ฐ) |
| ๊ฐ ์์ | replace(key, newValue) |
| ํญ๋ชฉ ์ญ์ | remove(key) |
| ํค ํ์ธ | containsKey(key) |
| ๊ฐ ํ์ธ | containsValue(value) |
| ์ ์ฒด ํ์ | entrySet(), keySet(), values() ํ์ฉ |