-
컬렉션 프레임웍과 함수형 인터페이스JAVA 2021. 4. 5. 18:03
함수형 인터페이스를 사용하는 컬렉션 프레임웍의 메서드(와일드 카드 생략)
인터페이스 메서드 설명 Collection bollean removeif(Predicate<E> filtter) 조건에 맞는 요소를 삭제 List void replaceAll(UnaryOperator<E> operator 모든 요소를 변환하여 대체 Iterable void forEach(Consumer<T> action) 모든 요소에 작업 action을 수행 Map V compute(K key, BiFunction<K,V,V> f) 지정된 키의 값에 작업 f를 수행 V computeIfAbsent(K key, Function<K,V> f) 키가 없으면, 작업 f 수행 후 추가 V computeIfPresent(K key, BiFunction<K,V.V> f) 지정된 키가 있을 때, 작업 f 수행 V merge(K key, V value, BiFunction<V.V.V> f) 모든 요소에 병합작업 f를 수행 void forEach(BiConsumer<K,V> action) 모든 요소에 작업 action을 수행 void replaceAll(BiFunction<K,V.V> f) 모든 요소에 치환작업 f를 수행 BiFunction<K,V,V> , Function<K,V> - 람다식
ex)
class Test{ public static void main(String[] args) { ArrayList<Integer> List = new ArraysList<>(); for(int i =0; i<10;i++) { list.add(i); } //list의 모든 요소를 출력 list.forEach(i -> System.out.print(i+",")); System.out.println(); //list에서 2또는 3의 배수를 제거한다. list.removeIf(x -> x%2==0 || x%3==0); System.out.println(list); list.replaceAll(i ->i*10); //list의 각 요소에 10을 곱한다. System.out.println(list); Map<String, String> map = new HashMap<>(); map.put("1","1"); map.put("2","2"); map.put("3","3"); map.put("4","4"); //map의 모든 요소를 {k,v}의 형식으로 출력한다. map.forEach((k,v)-> System.out.print("{"+k+","+v+"},")); System.out.println(); } }
'JAVA' 카테고리의 다른 글
#Builder 패턴 (0) 2021.05.11 인터페이스 Predicate<T> (0) 2021.04.05 람다식 (0) 2021.04.05 JRE , JDK ? (0) 2021.03.04 JAVA란? (0) 2021.03.04