stream[storage]

さようなら。冬·2020년 12월 16일
0
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class StreamStorage {
	public static void main(String args[]) {
	List<Example> examList = new ArrayList<Example>();
	examList.add(new Example(1, "1111"));
	examList.add(new Example(2, "2111"));
	examList.add(new Example(3, "3111"));
	examList.add(new Example(4, "4111"));

	Stream<Example> streamList = examList.stream();
	streamList.forEach(s -> {
		String name = s.getName();
		int id = s.getId();
		System.out.println("[name]:::::" + name);
		System.out.println("[id]:::::" + id);
	});
	// stream은 재사용이 안된다 .따라서 list에서 따로 다시 빼던가 해야됨
       //double average= streamList.mapToDouble(Example::getId).average().getAsDouble();
       //System.out.println("[평균]:"+average);
	IntStream intStream = IntStream.range(1, 100);
	int sumOne = intStream.sum();
	System.out.println("sumOne::::" + sumOne);
	streamList.close();

	Stream<Example> streamList12 = examList.stream();
	int sum = streamList12.filter(s -> "1111".equals(s.getName())).distinct()
                  .mapToInt(Example::getId).sum();
	System.out.println(sum);

	List<String> nameList = Arrays.asList("AAA", "BBB", "CCC", "AAA", "BBB", "DDD");
	Stream<String> nameStream = nameList.stream().distinct();
	nameStream.forEach(s -> {
		System.out.println(s);
	});
	nameStream.close();
	Stream<String> nameStream12 = nameList.stream();
	nameStream12.filter(s -> s.startsWith("A"))
                    .forEach(s -> System.out.println("startWith:[A]" + s)

	);

	nameList.stream().flatMap(d -> {
		String[] arr = d.split(",");
		return Arrays.stream(arr);
	}).forEach(s -> System.out.println(s));

	int[] intArray = { 1, 2, 3, 4, 5 };
	IntStream intStreams = Arrays.stream(intArray);
	intStreams.asDoubleStream().forEach(System.out::println);
	intStreams.close();
	intStreams = Arrays.stream(intArray);
	intStreams.boxed().forEach(System.out::println);
}
}

https://ict-nroo.tistory.com/43 참조

0개의 댓글