[Java8] Stream counting 개수 얻기

반응형
728x90
반응형

샘플 리스트

// sample List
List<Product> products = Arrays.asList(
    new Product(0, "Note_red", 1, 100),
    new Product(1, "Note_blue", 2, 200),
    new Product(2, "Note_green", 3, 300),
    new Product(3, "Note_pink", 4, 400),
    new Product(4, "Note_yellow", 5, 500),
    new Product(5, "Note_black", 6, 600),
    new Product(6, "Note_white", 7, 700),
    new Product(7, "Note_purple", 8, 800)
);

 

 

요약 Collectors.coutning()

/* Collectors 의 정적 메서드 counting */
long count = products.stream()
			.collect(Collectors.counting());

System.out.println("count : " + count); // 8

 

 

Stream

/* Stream 메서드 */
long count2 = products.stream()
			.count();

System.out.println("count2 : " + count2); // 8

 

 

반응형

Designed by JB FACTORY