Java------Stream流式编程常用API
分为基础API和高阶API
StreamAIP分类:
1.生成流
- Collection:调用Collection接口中的默认方法stream()生成流对象
- Map:获取所有的键、或值所对应的Set集合,调用Stream()方法生成流。
- 数组:Stream of(T… values)
2.中间操作 - 无状态:unordered()、filter()、map()、mapToInt()、mapToLong()、mapToDouble()、flatMap()、flatMapToInt()、flatMapToLong()、flatMapToDouble()、peek()
- 有状态:distinct()、sorted()、limit()、skip()
3.终结操作 - 非短路操作:forEach()、forEachOrdered()、toArrary()、reduce()、collect()、max()、min()、count()
- 短路操作:anyMatch()、allMactch()、noneMatch()、findFirst()、findAny()
生成流
针对数组、List、Map、Set生成流对象。
基于collection对象生成流。
@Test
public void test1() {
// String[] strs = new String[]{"hello", "world", "hi", "com", "student"};
//基于collection集合生成流
List<String> list = new ArrayList<>();
list.add("hello");
list.add("world");
list.add("hi");
list.add("com");
list.add("student");
//调用stream方法生成流对象
Stream<String> stream = list.stream();
stream.forEach(System.out::println);
//一般开发中会直接调用,链式编程
list.stream().forEach(System.out::println);
}
map是通过对keySet,以及Values集合生成流对象
@Test
public void test1() {
// String[] strs = new String[]{"hello", "world", "hi", "com", "student"};
Map<String,String> map = new HashMap<>();
map.put("1","hello");
map.put("2","world");
map.put("3","hi");
map.put("4","com");
map.put("5","student");
//map是通过对key集合、或value集合生成流对象
Set<String> keySet = map.keySet();
keySet.stream().forEach(x->{
System.out.println(x+":value:"+map.get(x));
});
//values
Collection<String> values = map.values();
values.stream().forEach(System.out::println);
}
基于数组生成流对象。但是int数组返回的两种形式不一致。既可以通过Stream.of(),也可以直接调用.stream方法。
@Test
public void test1() {
String[] strs = new String[]{"hello", "world", "hi", "com", "student"};
Stream<String> stream1 = Arrays.stream(strs);
Stream<String> strs1 = Stream.of(strs);
strs1.forEach(System.out::println);
//int数组返回的两种流形式不一致
int[] ints = new int[]{1,2,3,4,5};
IntStream stream = Arrays.stream(ints);
Stream<int[]> ints1 = Stream.of(ints);
}
中间操作—Filter()过滤方法
Stream常用基础方法
filter():过滤方法:
方法原型如下:
Stream<T> filter(Predicate<? super T> predicate);
返回由流中匹配的元素构成的新流,接受Predicate函数式接口参数,Predicate接口的方法如下:
boolean test(T t);
test方法产生一个boolean结果,为true则为新流的元素保留,为false则舍弃该元素。
案例1:
@Test
public void test1() {
String[] strs = new String[]{"hello", "world", "hi", "com", "student"};
Stream<String> strs1 = Stream.of(strs);
Stream<String> stringStream = strs1.filter(x->x.length()>4 && x.length()<6);
//第二种方式
strs1.filter(x->x.length()>4).filter(x->x.length()<6);
stringStream.forEach(System.out::println);
}
可以使用或、与进行多条件判断。
也可以再进行一次filter过滤方法。
map():映射方法
map():映射方法,用于元素的转换,方法原型如下:
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
将流中的元素映射到一个新流中,接受Function函数式接口参数,Function接口的方法如下:
R apply(T t);
将类型T转换为类型R。
示例代码2:
@Test
public void test1() {
String[] strs = new String[]{"hello", "world", "hi", "com", "student"};
Stream<String> strs1 = Stream.of(strs); //基于String的流
Stream<Integer> integerStream = strs1.map(x -> x.length()); //经过map转换后得到的一个新流
integerStream.forEach(System.out::println);
}
示例2,返回Map形式的流
@Test
public void test1() {
String[] strs = new String[]{"hello", "world", "hi", "com", "student"};
Stream<String> strs1 = Stream.of(strs); //基于String的流
strs1.map(x->{
Map<String,String> map= new HashMap<>();
map.put(x.toUpperCase(),x);
return map;
}).forEach(System.out::println);
}
List<List> allList = new ArrayList<>();
list里放list的结构,流操作时,需要进行两次流转换
@Test
public void test1() {
List<StringBuffer> strs = new ArrayList<>();
strs.add(new StringBuffer("hello"));
strs.add(new StringBuffer("world"));
strs.add(new StringBuffer("hi"));
strs.add(new StringBuffer("com"));
strs.add(new StringBuffer("student"));
List<StringBuffer> strs2 = new ArrayList<>();
strs2.add(new StringBuffer("hello"));
strs2.add(new StringBuffer("world"));
strs2.add(new StringBuffer("hi"));
strs2.add(new StringBuffer("com"));
strs2.add(new StringBuffer("student"));
List<List<StringBuffer>> allList = new ArrayList<>();
allList.add(strs2);
allList.add(strs);
Stream<Stream<StringBuffer>> streamStream = allList.stream().map(x -> x.stream().map(item -> {
if (item.length() > 3) {
item.append("大于三");
}
return item;
}));
streamStream.forEach(x->x.forEach(i->System.out.println(i)));
}