1031

스트림은 컬렉션의 저장 요소를 하나씩 참조해서 람다식으로 처리할 수 있도록 해주는 반복자

ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(1,2,3));
Intrator<Integer> iter = list.iterator();
while(iter.hasNext()) {
	int num = iter.next();
    System.out.println("값: " + num);
}
ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(1,2,3));
Stream<Integer> stream = list.stream();
stream.foreach(num -> System.out.println("ㄱㅏㅂㅅ: " + num));

//String 배열
String[] strArray = {'홍길동', '이순신', '임꺽정' };
Stream<String> strStream = Arrays.stream(strArray);
strStream.forEach(a -> System.out.println(a + ", "));

//int 배열
int[] intArray = {1,2,3,4,5};
IntStream intStream = Arrays.stream(intArray);
intStream.forEach(a -> System.out.print(a+ ","));
set.stream().sorted().mapToInt(Integer::intValue).toArray();

#2 완주하지 못한 선수
getOrDefault(): 찾는 키가 존재하면 해당 키의 값을 반환하고, 없으면 기본값을 반환

map.put(키,값) : map.put(a라는 키, a의 값이 존재하면 a의 값을 넣어주고 없다면 0) , 그리고 +1

public class Main {
public static void main(String[] args) {
	String[] arr1 = {"aaa", "bbb", "ccc", "aaa", "aaa", "ccc"};
    Map<String, Integer> map = new HashMap<String, Integer>();
    for(String a: arr1) {
    map.put(a, map.getOrDefault(a,0)+1);
   // {aaa=3, ccc=2, bbb=1} 
    }
   }


import java.util.*;
 
public class Main {
 
    public static void main(String[] args) {
        String[] people = {"Mike", "Anna", "Mike", "Harry"};
        Map<String, Integer> map = new HashMap<>();
        for(String a : people) map.put(a, map.getOrDefault(a, 0) + 1);
        System.out.println(map); //{Anna=1, Mike=2, Harry=1}
    }
}

쩌는 풀이들이 많다.

import java.util.HashMap;

class Solution {
    public String solution(String[] participant, String[] completion) {
        String answer = "";
        HashMap<String, Integer> hm = new HashMap<>();
        for (String player : participant) hm.put(player, hm.getOrDefault(player, 0) + 1);
        for (String player : completion) hm.put(player, hm.get(player) - 1);

        for (String key : hm.keySet()) {
            if (hm.get(key) != 0){
                answer = key;
            }
        }
        return answer;
    }
}

HashMap<String, Integer> hm = new HashMap<>();
for(String player : participant)
hm.put(player, hm.getOrDefault(player,0) + 1);
for(String player : completion)
hm.put(player, hm.get(player)-1);

for(String key: hm.keySet()) {
if(hm.get(key) != 0 ) {
answer = key;
}
}
return answer;

이건 좀 외우자 좋다.

#3 Listnode - AddTwoNumbers
2 next -> 4 next -> 3 null
5 next -> 6 next -> 2 null
0 next -> 7 next -> 0 next -> 6 null

class ListNode {
int val;
ListNode next;
ListNode(int x) {
this.val = x;
}
}

#3 같은숫자는 싫

current라는 변수를 통해 arraylist에 값이 없다면 그 값을 리스트에 넣어주고 current을 이번에 넣어준 값으로 바꿔주는 것으로 짜면 중복되지 않고 완성시킬수 있다.

import java.util.*;

public class Solution {
    public int[] solution(int []arr) {
        ArrayList<Integer> list = new ArrayList<>();
        int current = 10;
        for(int i=0; i<arr.length; i++) {
            if(arr[i] != current) {
                list.add(arr[i]);
                current = arr[i];
            }
        }
        
        int[] answer = new int[list.size()];
        for(int i=0; i<list.size(); i++) {
            answer[i] = list.get(i);
        }

        return answer;
    }
}

좋은 웹페이지 즐겨찾기