π lambda μ΄μ΄μ





컬λ μ νλ μμν¬
컬λ μ
νλ μμν¬
1. list 컬λ μ
: νμ΄μ¬μμλ 리μ€νΈ
2. map 컬λμ
: νμ΄μ¬μμλ λμ
λ리
<List 컬λ μ >
- μμλ₯Ό μ μ§, μ€λ³΅μ μ₯ κ°λ₯
- ꡬνν΄λμ€ : ArrayList, Vector, LinkedList
Vectorλ λ©ν° μ€λ λκ° κ°λ₯νλ€.
μΈν°νμ΄μ€ list
ArrayList 리μ€νΈμμ μ 곡νλ μΆμλ©μλλ₯Ό λ€ κ°κ³ μλ€.
package com.gyuone.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List; // μΆκ°νμ
public class ArrayListExam01 {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("java"); // 0
list.add("python"); // 1
list.add("c++"); // 2 index μ κ·Ό κ°λ₯
list.add("Ruby");
list.add("C#");
System.out.println("μ΄ λ°μ΄ν° μ : " + list.size()); // μ 체 λ°μ΄ν° κ°μ λμ΄.
System.out.println("index 2 λ²μ§Έ λ°μ΄ν°" + list.get(2)); // index μ κ·Ό
System.out.println();
for(int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
} // μ 체 λ°μ΄ν° μ°κΈ°
System.out.println();
System.out.println();
for(String s : list) { // νμ₯ forλ¬Έ
System.out.println(s + " ");
}
System.out.println();
list.remove(2); // index λ 리기
for(String s : list) { // νμ₯ forλ¬Έ
System.out.println(s + " ");
}
System.out.println();
list.remove("python"); // λ΄μ©λ¬Όλ‘ μ§μ λ 리기
for(String s : list) { // νμ₯ forλ¬Έ
System.out.println(s + " ");
}
System.out.println();
List<String> list2 = Arrays.asList("μμΈ", "λμ ", "μΈμ°");
for(String s: list2) {
System.out.print(s + " ");
}
System.out.println();
list2.add("μΈμ²");
for(String s: list2) {
System.out.print(s + " ");
}
System.out.println(); // μΆκ° λΆκ°λ₯
}
}


μΆκ° λΆκ°λ₯
Map
package com.gyuone.collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapExam01 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
// HashTable() thread safe λ©ν°μ€λ λλ‘ λμμ 곡μ νλ €λ©΄ HashTableλ‘ μ΄λ
map.put("κ΅μ΄", 90);
map.put("μμ΄", 85);
map.put("μν", 95);
map.put("κ³Όν", 100);
System.out.println(map.size()); // λ°°μ΄ μ¬μ΄μ¦
System.out.println("μμ΄μ μ : " + map.get("μμ΄"));
Set<String> keySet = map.keySet();
Iterator<String> keyIterator = keySet.iterator(); // νμ΄μ¬μμ generator
// λ€ κ°κ³ μλ μνμμ νλμ© μ€λ€.
while(keyIterator.hasNext()) { // κΊΌλ΄μ¬ κ°μ΄ μλλ
String key = keyIterator.next();
int value = map.get(key);
System.out.println(key + " : " + value + " ");
}
System.out.println();
// λλ²μ§Έ λ°©λ²
Set<Map.Entry<String, Integer>> entrySet = map.entrySet(); // ν€λ value κ°μ΄ λ½μ μ£Όκ² λ€.
Iterator<Map.Entry<String, Integer>> entryIterator = entrySet.iterator();
while(entryIterator.hasNext()) {
Map.Entry<String, Integer> entry = entryIterator.next();
String key = entry.getKey();
int value = entry.getValue();
System.out.print(key + " : " + value + " ");
}
System.out.println();
}
}



stream

package com.gyuone.collection;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
import com.gyuone.lambda2.Person;
public class StreamExam01 {
public static void main(String[] args) {
List<String> list = Arrays.asList("μμΈ", "λμ ", "μΈμ°", "λΆμ°");
for(String s: list) {
System.out.println(s + " "); // κ°νΈνκ² μ¬μ©νκΈ° μν΄ λ§λ . νλμ© μ°Έμ‘°
}
System.out.println();
Iterator<String> iterator = list.iterator(); // μ¬λ¬ μ€μ νμ΄ννκΈ° νλλκΉ hasNextλ‘ λμ§μ΄λΈλ€.
while(iterator.hasNext()) { // μλ μ΄λ κ² μΌλ€.
String city = iterator.next();
System.out.print(city + " ");
}
System.out.println();
System.out.println();
Stream<String> stream = list.stream(); // ννμμΌλ‘ κ°λ¨νκ² λ§λ€κΈ° stream
stream.forEach(city -> System.out.print(city + " ")); // for each λ΄λΆμμ λ°λ³΅
System.out.println(); // λμ©λ λ°μ΄ν° μ²λ¦¬ν λ λ³λ ¬ μ€νΈλ¦Ό CPU μ½μ΄ κ°μλ‘ λ°μ΄ν°λ₯Ό μ²λ¦¬νλ€.
List<Person> list2 = Arrays.asList(
new Person("νκΈΈλ", "λ¨μ", 90),
new Person("μ΄μμ ", "λ¨μ", 95)
);
System.out.println();
Stream<Person> stream2 = list2.stream();
// μ΄λ¦ μ μ μΆλ ₯
stream2.forEach(person -> {
String name = person.getName();
int score = person.getScore();
System.out.println(name + " : " + score + " ");
});
}
}



'λΉνΈκ΅μ‘ > JAVA' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
| [18μΌμ°¨] JAVA μκ³ λ¦¬μ¦ λ¬Έμ νμ΄ 2 (0) | 2022.07.13 |
|---|---|
| [18μΌμ°¨] JAVA μκ³ λ¦¬μ¦ λ¬Έμ νμ΄ 1 (0) | 2022.07.13 |
| [17μΌμ°¨] JAVA Programmers κ°λ μ 리 (0) | 2022.07.12 |
| [17μΌμ°¨] JAVA Generic / lambda (0) | 2022.07.12 |
| [16μΌμ°¨] JAVA μμΈμ²λ¦¬. μ€λ λ (0) | 2022.07.11 |
