์ด๋ฒ ํฌ์คํ ์์๋ List.of()๋ก ๋ง๋ ๋ถ๋ณ ๋ฆฌ์คํธ(๋ด๋ถ ์ ๋ณด ๋ฐ๊ฟ ์ ์์)๋ฅผ ํ์ตํด๋ณด๊ณ ,
์ด๋ฅผ ํตํด ๊ฐ๋ณ ๋ฆฌ์คํธ(๋ด๋ถ์ ๋ณด ๋ฐ๊ฟ ์ ์์)๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ๊น์ง ์์๋ณธ๋ค.
<ํฌ์คํ ์์>
Collections
- List
- Set
- Queue
- Map
์๋ฐ๋ฅผ ๊ณต๋ถํ ๋ ํ๋
์ต์ ๋ฒ์ + ํค์๋ ๊ตฌ๊ธ๋งํ์ฌ ์ ์ผ ์์ ๋์ค๋ ์ค๋ผํด ํฌํผ ์ผํฐ ๋ฌธ์๋ฅผ ์ด์ฌํ ์ฝ์ ๊ฒ.
List
์ ์
https://docs.oracle.com/javase/9/docs/api/java/util/List.html
์ ๋ ฌ๋ ์ปฌ๋ ์ ( sequence๋ผ๊ณ ๋ ํจ ).
์ด ์ธํฐํ์ด์ค์ ์ฌ์ฉ์๋ ๋ชฉ๋ก์์ ๊ฐ ์์๊ฐ ์ฝ์ ๋๋ ์์น๋ฅผ ์ ํํ๊ฒ ์ ์ดํ ์ ์์ต๋๋ค.
์ฌ์ฉ์๋ ์ ์ ์ธ๋ฑ์ค(๋ชฉ๋ก์ ์์น)๋ก ์์์ ์ก์ธ์คํ๊ณ ๋ชฉ๋ก์ ์์๋ฅผ ๊ฒ์ํ ์ ์์ต๋๋ค.
์งํฉ๊ณผ ๋ฌ๋ฆฌ ๋ชฉ๋ก์ ์ผ๋ฐ์ ์ผ๋ก ์ค๋ณต ์์๋ฅผ ํ์ฉํฉ๋๋ค.
An ordered collection (also known as a sequence).
์์์๋ ์งํฉ์ด๋ผ๋ ๊ฒ์ด ์ค์ํ๋ค. ์ด๊ฒ์ List์ ์ด๋ ์์๊ฐ ์ด๋์ ์ฝ์ ๋๋์ง ์ ๋ฐํ๊ฒ ์ ์ดํ ์ ์๋ค๋ ๊ฒ์ด๋ค.
List Interface
Modifier and Type | Method | Description |
boolean | addAll(int index, Colletion<> extends E> c) | ์ง์ ๋ ์ปฌ๋ ์ ์ ๋ชจ๋ ์์๋ฅผ ์ด ๋ชฉ๋ก์ ์ง์ ๋ ์์น์ ์ฝ์ ํ๋ค |
E | get(int index) | ์ด ๋ชฉ๋ก์ ์ง์ ๋ ์์น์ ์๋ ์์๋ฅผ ๋ฐํ |
E | set(int index, E element) | ์ด ๋ชฉ๋ก์ ์ง์ ๋ ์์น์ ์๋ ์์๋ฅผ ์ง์ ๋ ์์๋ก ๋ฐ๊พผ๋ค |
void | add(int index, E element) | ์ด ๋ชฉ๋ก์ ์ง์ ๋ ์์น์ ์ง์ ๋ ์์๋ฅผ ์ฝ์ ํ๋ค |
E | remove(int paramInt) | ์ด ๋ชฉ๋ก์ ์ง์ ๋ ์์น์ ์ง์ ๋ ์์๋ฅผ ์ ๊ฑฐํ๋ค |
int | indexOf(Object o) | ์ด ๋ชฉ๋ก์์ ์ง์ ๋ ์์๊ฐ ์ฒ์ ๋ํ๋๋ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํ๊ฑฐ๋ ์ด ๋ชฉ๋ก์ ์์๊ฐ ํฌํจ๋์ง ์์ผ๋ฉด -1์ ๋ฐํํ๋ค. |
int | lastIndexOf(Object o) | ์ด ๋ชฉ๋ก์์ ์ง์ ๋ ์์๊ฐ ๋ง์ง๋ง์ผ๋ก ๋ํ๋๋ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํ๊ฑฐ๋, ์ด ๋ชฉ๋ก์ ์์๊ฐ ํฌํจ๋์ด ์์ง ์์ผ๋ฉด -1์ ๋ฐํํ๋ค |
์์
์๋ฐ 9์์ ๊ฐ์ฅ ์ฝ๊ฒ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค ์ ์๋ ๋ฐฉ๋ฒ์ List.of()์ด๋ค.
of()๋ ๋ฆฌ์คํธ ์ธํฐํ์ด์ค ์์ ์กด์ฌํ๋ ์ ์ ๋ฉ์๋์ด๋ค. ์ด๊ฒ์ด ํน์ ์์๋ค์ ๊ฐ์ง๊ณ ๋ฆฌ์คํธ๋ฅผ ์ด๊ธฐํํ ์ ์๋ ์ ์ผ ์ฌ์ด ๋ฐฉ๋ฒ์ด๋ค.
import java.util.List;
public class Hello {
public static void main(String[] args) {
List<String> words = List.of("Jisoo", "Lisa", "Jenny", "Rose");
System.out.println(words);
}
}
- ๊ธธ์ด๋ฅผ ์ฐ๊ณ ์ถ๋ค๋ฉด, length๊ฐ ์๋๋ผ size๋ฅผ ์ด๋ค.
(๋ฐฐ์ด์์๋ ๊ธธ์ด ์์ฑ์ด ์๊ณ ArrayList๋ ์งํฉ์์๋ ํฌ๊ธฐ ์์ฑ์ด ์๋ค.)
import java.util.List;
public class Hello {
public static void main(String[] args) {
List<String> words = List.of("Jisoo", "Lisa", "Jenny", "Rose");
System.out.println(words.size()); // 4
System.out.println(words.isEmpty()); // false
System.out.println(words.get(0)); // Jisoo
System.out.println(words.get(1)); // Lisa
System.out.println(words.get(2)); // Jenny
System.out.println(words.get(3)); // Rose
System.out.println(words.contains("GD")); // false
System.out.println(words.contains("Jenny")); // true
System.out.println(words.indexOf("GD")); // -1
System.out.println(words.indexOf("Jenny")); // 2
}
}
๋ฆฌ์คํธ ์ธํฐํ์ด์ค์ ๋ถ๋ณ์ฑ๊ณผ ๊ฐ๋ณ๋ฆฌ์คํธ์ ๊ตฌํ
๋ฌธ์์ด ํด๋์ค -> ๋ถ๋ณ : ํน์ ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ๋ง๋ ์๊ฐ๋ถํฐ ์ด ๊ฐ์ ๋ฐ๊ฟ ์ ์๊ฒ ๋๋ค.
๋น์ทํ๊ฒ List.of()๋ผ๋ ๊ฐ Map.of() ๊ฐ์ ๊ฒ๋ค๋ก ๋ง๋ ๋ชจ๋ ๊ฒ๋ค์ ๋ถ๋ณ ๋ฆฌ์คํธ์ด๋ค.
๋ฐ๋ผ์, ์๋์ ๊ฐ์ ์ฝ๋๋ ์๋ฌ๊ฐ ๋๋ค.
System.out.println(words.add("V"));
Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:72)
at java.base/java.util.ImmutableCollections$AbstractImmutableCollection.add(ImmutableCollections.java:76)
at Hello.main(Hello.java:19)
์ด์ ๊ฐ๋ณ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค๊ณ ์ถ๋ค๋ฉด, ์ ํ์ง๋ก ์๋ ๊ฒ๋ค์ ArrayList๋ฅผ ๋ง๋ค๋๊ฐ, LinkedList, Vector๋ฅผ ๋ง๋ค ์ ์๋ค.
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
public class Hello {
public static void main(String[] args) {
List<String> words = List.of("Jisoo", "Lisa", "Jenny", "Rose");
List<String> wordsArrayList = new ArrayList<String>(words);
System.out.println(wordsArrayList); //[Jisoo, Lisa, Jenny, Rose]
System.out.println(wordsArrayList.size()); //4
List<String> wordsLinkedList = new LinkedList<String>(words);
System.out.println(wordsLinkedList); //[Jisoo, Lisa, Jenny, Rose]
System.out.println(wordsLinkedList.size()); //4
List<String> wordsVector = new Vector<String>(words);
System.out.println(wordsVector); //[Jisoo, Lisa, Jenny, Rose]
System.out.println(wordsVector.size()); //4
}
}
์ด์ ArrayList, LinkedList, Vector ๊ฐ๊ฐ์ ๊ฐ์ ์ถ๊ฐํ ์ ์๊ฒ ๋๋ค.
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
public class Hello {
public static void main(String[] args) {
List<String> words = List.of("Jisoo", "Lisa", "Jenny", "Rose");
List<String> wordsArrayList = new ArrayList<String>(words);
wordsArrayList.add("GD");
System.out.println(wordsArrayList); // [Jisoo, Lisa, Jenny, Rose, GD]
System.out.println(wordsArrayList.size()); // 5
List<String> wordsLinkedList = new LinkedList<String>(words);
wordsLinkedList.add("V");
System.out.println(wordsLinkedList); // [Jisoo, Lisa, Jenny, Rose, V]
System.out.println(wordsLinkedList.size()); // 5
List<String> wordsVector = new Vector<String>(words);
wordsVector.add("KAI");
System.out.println(wordsVector); // [Jisoo, Lisa, Jenny, Rose, KAI]
System.out.println(wordsVector.size()); // 5
}
}
ArrayList, LinkedList, Vector
์ด ๊ฐ๊ฐ์ ๋ฆฌ์คํธ๋ค์ด ์ง์ํ๋ ์์ ๋ค์ ์ฐจ์ด์ ์ ์ ์ดํดํ์ฌ, ์ ์ฌ์ ์์ ํ์ฉํ ์ค ์๋ ๊ฐ๋ฐ์๊ฐ ๋๋๋ก ํ์.
๋ถ๋์ ๋ค์ ํฌ์คํ ์ ์ด์ด์ ์์ฑํ๋๋ก ํ๋ค.
'Java & Spring > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ์ธํฐํ์ด์ค (0) | 2022.12.14 |
---|---|
[Java] ์๋ฐ List ์ธํฐํ์ด์ค ๊ตฌํ ArrayList vs LinkedList (0) | 2022.12.09 |
[Java] ๊ฐ์ฒด๋ ์์์ ์ต์์ ์์ (0) | 2022.11.23 |
[Java IDE] eclipse IDE ์คํ์ผ ๋ณ๊ฒฝํ๊ธฐ (0) | 2022.05.16 |
[์๋ฐ์ ์ ์] Hello Java & Eclipse ๋จ์ถํค (0) | 2022.04.25 |