package com.zxr.stream;
import java.util.ArrayList;
import java.util.stream.Stream;
public class Test03 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("张三丰");
list.add("张翠山");
list.add("王二麻子");
list.add("张飞");
list.add("谢广坤");
list.add("谢广坤");
list.add("谢广坤");
list.add("谢广坤");
list.add("谢广坤");
// Stream<T> limit(longmaxSize):截取指定参数个数的数据(保留前2两)
list.stream().limit(2).forEach(s -> System.out.println(s));
// Stream<T> skip(longn):跳过指定参数个数的数据(跳过前两个)
list.stream().skip(2).forEach(s -> System.out.println(s));
// static<T> Stream<T>concat(Streama,Streamb):合并a和b两个流为一个流
ArrayList<String> list2 = new ArrayList<>();
list.add("张飞");
list.add("谢广坤");
Stream<String> stream1 = list.stream();
Stream<String> stream2 = list2.stream();
Stream<String> stream3 = Stream.concat(stream1, stream2);
stream3.forEach(s -> System.out.println(s));
// 简化写法
Stream.concat(list.stream(),list2.stream()).forEach(s -> System.out.println(s));
// Stream<T> distinct():去除流中重复的元素。依赖(hashCode和equals方法)
list.stream().distinct().forEach(s -> System.out.println(s));
//结果中只有一个“谢广坤”
}
}
标签:常用,Stream,stream,操作方法,list,System,谢广坤,add,四种
From: https://www.cnblogs.com/dragon-proud-sky/p/18093842