πŸš€ 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 + " ");
		});
		
		
	}

}

 

 

 

 


 

+ Recent posts