[JAVA] Stream 사용하기(스트림)- 3.결과만들기

Posted by 김성철

JAVA - Stream 사용하기 - 결과

https://mangkyu.tistory.com/114  

Max(최대값) , Min(최소값) , Sum(합계) , Average(평균) , Count(갯수)

Stream의 요소들의 최대값,최소값등을 구할 수 있음  
max , min , average 는 비어있는 값을 측정 할 수 없으므로 Optional 클래스를 사용함  
  
=================================================================================================================  
//Min 최소값  
OptionalInt min = IntStream.of(3,5,6,7,8,3,2,1).min();  
System.out.print("최소값 : ");  
System.out.println(min);  
  
//Max 최대값  
int max = IntStream.of().max().orElse(0);  
System.out.print("최대값 : ");  
System.out.println(max);  
  
//Average 평균값  
System.out.print("평균값 : ");  
IntStream.of(1,2,3,4,5,6,7,8,9,10).average().ifPresent(System.out::println);  
  
//sum 총합  
long sum = IntStream.of(1,2,3,4,5,6,7,8).sum();  
System.out.print("총합 : ");  
System.out.println(sum);  
  
//count 갯수  
long count = IntStream.of(1,2,3,4,5,6,7,8).count();  
System.out.print("갯수 : ");  
System.out.println(count);  
=================================================================================================================  

collect - 데이터 수집

Stream의 요소들을 List, Set , Map 등의 다른 종류의 결과로 생성하고 싶을 경우에 collect 함수를 사용  

collect - Collectors.toList() 결과를 List로 반환

=================================================================================================================  
    //value 값만 뽑아서 리스트 생성  
    List<String> resultValueList = test01List.stream()  
            .map(TestVO1::getValue)  
            .collect(Collectors.toList());  
    System.out.println(resultValueList);  
    //seq 값만 뽑아서 리스트 생성  
    List<Integer> resultSeqList = test01List.stream()  
            .map(TestVO1::getSeq)  
            .collect(Collectors.toList());  
    System.out.println(resultSeqList);  
=================================================================================================================  

collect - Collectors.joining() stream 에서 작업한 결과를 하나의 String 으로 붙일때 사용

아래와 같이 3개의 인자를 사용 할 수 있음  
delimiter : 각 요소 중간에 들어가 요소를 구분시켜주는 구분자  
prefix : 결과 맨 앞에 붙는 문자  
suffix : 결과 맨 뒤에 붙는 문자  
  
=================================================================================================================  
    //결과값을 하나의 스트링으로 생성  
    String listToString01 = test01List.stream()  
            .map(TestVO1::getValue)  
            .collect(Collectors.joining());  
    System.out.println(listToString01);  
  
    //결과값을 하나의 스트링으로 생성하면서, 중간에 공백을 추가함  
    String listToString02 = test01List.stream()  
            .map(TestVO1::getValue)  
            .collect(Collectors.joining(" "));  
    System.out.println(listToString02);  
  
    //결과값을 하나의 스트링으로 생성하면서, 중간에 콤마와 시작과 끝지점에  ( ) 를 추가함  
    String listToString03 = test01List.stream()  
            .map(TestVO1::getValue)  
            .collect(Collectors.joining("," , "(" , ")"));  
    System.out.println(listToString03);  
  
=================================================================================================================  

collect - Collectors.averagingInt(), Collectors.summingInt(), Collectors.summarizingInt()

Stream 에서 작업한 결과의 평균, 총합을 구함  
  
=================================================================================================================  
    Double average1 = test01List.stream()  
            .collect(Collectors.averagingInt(TestVO1::getSeq));  
    System.out.println("#### average1 : " + average1);  
  
    Integer sum1 = test01List.stream()  
            .collect(Collectors.summingInt(TestVO1::getSeq));  
    System.out.println("#### sum1 : " + sum1);  
=================================================================================================================  

collect - Collectors.groupingBy()

Stream 에서 작업한 결과를 특정 그룹으로 묶을 때 사용  
결과는 Map 으로 반환받게 됨  
groupingBy 는 함수형 인터페이스 Function을 필요로 함  
  
=================================================================================================================  
    //seq가 같은 값인 VO끼리 묶음  
    Map<Integer , List<TestVO1>> collectorMap = test03List.stream()  
            .collect(Collectors.groupingBy(TestVO1::getSeq));  
    System.out.println("#### collectorMap");  
    System.out.println(collectorMap);  
=================================================================================================================  

collect - Collectors.partitioningBy()

함수형 인터페이스 Predicate 를 받아 Boolean key 값으로 partitioning 함  
아래코드는 seq가 5보다 큰것과 작은 것을 나눈것임  
=================================================================================================================  
    Map<Boolean , List<TestVO1>> collectorMap2 = test03List.stream()  
            .collect(Collectors.partitioningBy(p -> p.getSeq() >5));  
    System.out.println("#### collectorMap2");  
    System.out.println(collectorMap2);  
=================================================================================================================  

match - 조건검사

anyMatch: 1개의 요소라도 해당 조건을 만족하는가  
allMatch: 모든 요소가 해당 조건을 만족하는가  
nonMatch: 모든 요소가 해당 조건을 만족하지 않는가  
=================================================================================================================  
    List<String> names = Arrays.asList("Hi", "Hellow", "Kimsungchul");  
    boolean anyMatch = names.stream().anyMatch(name -> name.contains("H"));  
    boolean allMatch = names.stream().allMatch(name -> name.length() > 3);  
    boolean noneMatch = names.stream().noneMatch(name -> name.endsWith("s"));  
    System.out.println("#### anyMatch : " + anyMatch);  
    System.out.println("#### allMatch : " + allMatch);  
    System.out.println("#### noneMatch : " + noneMatch);  
=================================================================================================================  

forEach - 특정 연산 수행

Stream 의 요소들로 특정 연산을 수행 할 떄 사용 할 수 있으며  
비슷한 함수로는 peek()가 있음  
peek는 중간연산으로 실제 요소에 영향을 주지 않으나 forEach는 최종연산으로 실제 요소에 영향을 줄 수 있음  
  
=================================================================================================================  
	 List<String> names = Arrays.asList("Hi", "Hellow", "Kimsungchul");  
	strList.stream().forEach(System.out::println);  
=================================================================================================================  

소스코드

=================================================================================================================  
	package com.sungchul;  
  
	import javax.xml.transform.sax.SAXSource;  
	import java.util.*;  
	import java.util.stream.Collectors;  
	import java.util.stream.IntStream;  
  
	public class StreamResultTestClass {  
		public static void main(String[] args) {  
  
			//Min 최소값  
			OptionalInt min = IntStream.of(3, 5, 6, 7, 8, 3, 2, 1).min();  
			System.out.print("최소값 : ");  
			System.out.println(min);  
  
			//Max 최대값  
			int max = IntStream.of().max().orElse(0);  
			System.out.print("최대값 : ");  
			System.out.println(max);  
  
			//Average 평균값  
			System.out.print("평균값 : ");  
			IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).average().ifPresent(System.out::println);  
  
			//sum 총합  
			long sum = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8).sum();  
			System.out.print("총합 : ");  
			System.out.println(sum);  
  
			//count 갯수  
			long count = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8).count();  
			System.out.print("갯수 : ");  
			System.out.println(count);  
  
			//리스트 생성  
			List<TestVO1> test01List = new ArrayList<>();  
			List<TestVO1> test02List = new ArrayList<>();  
			for (int i = 0; i < 10; i++) {  
				int temp = i + 2;  
				if (i % 3 == 0) {  
					temp = i + 100;  
				}  
				TestVO1 test01 = new TestVO1(i, i + "데이터");  
				TestVO1 test02 = new TestVO1(i + 2, temp + "데이터");  
				test01List.add(test01);  
				test02List.add(test02);  
			}  
  
			//value 값만 뽑아서 리스트 생성  
			List<String> resultValueList = test01List.stream()  
					.map(TestVO1::getValue)  
					.collect(Collectors.toList());  
			System.out.println(resultValueList);  
			//seq 값만 뽑아서 리스트 생성  
			List<Integer> resultSeqList = test01List.stream()  
					.map(TestVO1::getSeq)  
					.collect(Collectors.toList());  
			System.out.println(resultSeqList);  
  
			//결과값을 하나의 스트링으로 생성  
			String listToString01 = test01List.stream()  
					.map(TestVO1::getValue)  
					.collect(Collectors.joining());  
			System.out.println(listToString01);  
  
			//결과값을 하나의 스트링으로 생성하면서, 중간에 공백을 추가함  
			String listToString02 = test01List.stream()  
					.map(TestVO1::getValue)  
					.collect(Collectors.joining(" "));  
			System.out.println(listToString02);  
  
			//결과값을 하나의 스트링으로 생성하면서, 중간에 콤마와 시작과 끝지점에  ( ) 를 추가함  
			String listToString03 = test01List.stream()  
					.map(TestVO1::getValue)  
					.collect(Collectors.joining(",", "(", ")"));  
			System.out.println(listToString03);  
  
			Double average1 = test01List.stream()  
					.collect(Collectors.averagingInt(TestVO1::getSeq));  
			System.out.println("#### average1 : " + average1);  
  
			Integer sum1 = test01List.stream()  
					.collect(Collectors.summingInt(TestVO1::getSeq));  
			System.out.println("#### sum1 : " + sum1);  
  
			//리스트 생성  
			List<TestVO1> test03List = new ArrayList<>();  
			for (int i = 0; i < 10; i++) {  
				TestVO1 test01 = new TestVO1(i, i + "데이터" + i);  
				TestVO1 test02 = new TestVO1(i, i + "데이터");  
  
				test03List.add(test01);  
				test03List.add(test02);  
			}  
  
			//seq가 같은 값인 VO끼리 묶음  
			Map<Integer, List<TestVO1>> collectorMap1 = test03List.stream()  
					.collect(Collectors.groupingBy(TestVO1::getSeq));  
			System.out.println("#### collectorMap1");  
			System.out.println(collectorMap1);  
  
			Map<Boolean, List<TestVO1>> collectorMap2 = test03List.stream()  
					.collect(Collectors.partitioningBy(p -> p.getSeq() > 5));  
			System.out.println("#### collectorMap2");  
			System.out.println(collectorMap2);  
  
			List<String> strList = Arrays.asList("Hi", "Hellow", "Kimsungchul");  
			boolean anyMatch = strList.stream().anyMatch(name -> name.contains("H"));  
			boolean allMatch = strList.stream().allMatch(name -> name.length() > 3);  
			boolean noneMatch = strList.stream().noneMatch(name -> name.endsWith("s"));  
			System.out.println("#### anyMatch : " + anyMatch);  
			System.out.println("#### allMatch : " + allMatch);  
			System.out.println("#### noneMatch : " + noneMatch);  
  
			strList.stream().forEach(System.out::println);  
			test03List.stream().forEach(TestVO1::getSeq);  
  
		}  
	}  
  
	class TestVO1 {  
  
		int seq;  
		String value;  
  
		public TestVO1(int seq, String value) {  
			this.seq = seq;  
			this.value = value;  
		}  
  
		public int getSeq() {  
			return seq;  
		}  
  
		public void setSeq(int seq) {  
			this.seq = seq;  
		}  
  
		public String getValue() {  
			return value;  
		}  
  
		public void setValue(String value) {  
			this.value = value;  
		}  
  
		@Override  
		public String toString() {  
			return "TestVO{" +  
					"seq=" + seq +  
					", value='" + value + '\'' +  
					'}';  
		}  
  
		@Override  
		public boolean equals(Object o) {  
			TestVO t = (TestVO) o;  
			if (this.value.equals(t.getValue())) {  
				return true;  
			} else {  
				return false;  
			}  
		}  
	}  
=================================================================================================================