헝D의 일기장

○ list 정렬

List<String> list = Arrays.asList("abc", "aaa", "bbb", "ddd", "aaa");

Collections.sort(list, (s1, s2) -> s2.compareTo(s1));

 

○list의 모든요소 출력

list.forEach(i -> System.out.print(i+","));

 

○list에서 특정 조건인 경우 제거

list.removeIf(x -> x%2 == 0 || x%3 ==0);

 

○list 에서 replace

list.replaceAll(i -> i*10);

 

○map의 모든 요소를 {k,v} 형식으로 출력

map.forEach((k, v) -> System.out.println("{"+k+","+v+"},"));

 

○메서드 참조

(String s) -> Integer.parseInt(s)   -->  Integer::parseInt; //이렇게 표현 가능하다 

() -> new MyClass();  --> Myclass::new; //생성자의 메서드 참조

x -> new int[x]  --> int[]::new;

 

○스트림으로 데이터 정렬

배열 arr, 리스트 list

Stream<String> s1= list.stream();

Stream<String> s2= Arrays.stream(arr);

s1.sorted().forEach(System.out::println);

s2.sorted().forEach(System.out::println);

 

○스트림으로 정렬된 결과를 컬렉션에 담아 반환하기(스트림은 데이터를 읽을뿐 데이터를 변경하진 않음)

List<String> list= s2.sorted().collect(Collectors.toList()); //배열을 정렬해서 새로운 list에 담아 반환 

profile

헝D의 일기장

@헝D

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!