본문 바로가기

java817

Java - default 메서드 자바8에서 새로 List에 sort(), Collection stream() 메서드가 추가 되었다. 해당 메서드가 추가 되면서 해당 인터페이스를 구현한 클래스에서 해당 메서드를 구현해야하는 문제가 발생, 그 문제를 해결 하기 위해 새로 생긴 문법 public interface AAA { void a(); default void b() { // default 메서드 인터페이스 안에서 구현한다. System.out.println("AAA"); } } default 특징 인터페이스 안에서 구현된다. 해당 인터페이스를 구현하는 클래스에서는 오버라이딩 하지 않아도 된다. 구현클래스에서 default 메서드를 오버라이딩 구현 했을 경우 해당 클래스의 메서드가 호출된다.클래스에서 구현한 메서드 -> 하위 인터페이스 .. 2017. 3. 13.
Java - collect(), Collector, Collectors collect(), Collector, Collectors collect()는 Collector를 매개변수로 하는 스트림의 최종연산 Object collect(Collector collector) // Collector를 구현한 클래스의 객체를 매개변수로 Object collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner) Collector는 collect에서 필요한 메서드를 정의해 놓은 인터페이스 public interface Collector { // T(요소)를 A에 누적한 다음, 결과를 R로 변환해서 반환 Supplier supplier(); // StringBuilder::new 누적할 곳 BiConsumer accumula.. 2017. 3. 11.
Java - 메서드 레퍼런스(method reference) 하나의 메서드만 호출하는 람다식은 ‘메서드 레퍼런스’로 간단히 할 수 있다. 1. 정적 메서드 레퍼런스 //패턴 (args) -> ClassName.staticMethod(args) ClassName::staticMethod Function f = (String s) -> Integer.parseInt(s); Function f = Integer::parseInt; // 메서드 레퍼런스 2. 인스턴스 메서드 레퍼런스 (arg0, rest) -> arg0.instanceMethod(rest) ClassName::instanceMethod BiFunction f = (s1, s2) -> s1.equals(s2); BiFunction f = String::equals; 3. 특정 객체의 인스턴스 메서드 레퍼런.. 2017. 3. 10.
Java - Stream 최종 연산자(reduce) reduce - 스트림의 요소를 하나씩 줄여가며 누적연산 수행 함수 인터페이스 Optional reduce(BinaryOperator accumulator) T reduce(T identity, BinaryOperator accumulator) U reduce(U identity, BiFunction accumulator, BinaryOperator combiner) 매개변수 identity - 초기값 accumulator - 이전 연산결과와 스트림의 요소에 수행할 연산 combiner - 병렬처리된 결과를 합치는데 사용할 연산(병렬 스트림) // OptionalInt reduce(IntBinaryOperator accumulator)// 연산된 결과값과 다음 값을 계속 비교한다. OptionalInt .. 2017. 3. 9.