J2SE 8的流库 --- 转换流, 得到的还是流

流的转换, 按照条件过滤/映射/摊平/截取/丢弃/连接/去重/排序。

辅助方法

public static int myCompare(String x, String y) {
	if(x.length()>y.length()){
		return 1;
	}else if(x.length()==y.length()){
		return x.compareTo(y);
	}else{
		return -1;
	}
}

private static Stream<Integer> letters(String s){
	return s.chars().boxed();			//将所有的chars抽成流
}


public static <T> void show(String title, Stream<T> stream){
	System.out.println("title:"+title);
	
	List<T> collect = stream.limit(10).collect(Collectors.toList());
	collect.forEach(n->System.out.println(n));
	System.out.println();
}

//????
@SuppressWarnings("unchecked")
public static <T extends Stream<?>> void deepShow(String title, T stream){
	System.out.println("title:"+title);
	
	stream.forEach(n->{
		if(n instanceof Stream){
			deepShow(title, (T)n);
		}else{
			System.out.println(n);
		}
	});
	
	System.out.println();
}

1. 过滤 filter()  Predicate<? super T> predicate  T->boolean   按照特定条件过滤

Stream<String> filterStream = arrayList.stream().filter((w)->(w.length()>2));
show("filterStream", filterStream);

2. 映射  map()   Function<? super T, ? extends R> mapper  T->R  按照特定方式转换

Stream<String> mapStream = arrayList.stream().map(String::toUpperCase);
show("mapStream", mapStream);

3. 摊平 flatMap()  包含流的流  摊平

Stream<Stream<Integer>> flatStream = arrayList.stream().map(w->letters(w));
deepShow("flatStream", flatStream);

//==>摊平
Stream<Integer> flatMapStream = arrayList.stream().flatMap(w->letters(w));
flatMapStream.forEach(n->System.out.println((char)(int)n));
System.out.println();

4. 截取 limit(n)  在n个元素后结束

Stream<String> limitStream = arrayList.stream().limit(2);
show("limitStream", limitStream);

5. 丢弃 skip(n)   丢弃前n个元素

Stream<String> skipStream = arrayList.stream().skip(2);
show("skipStream", skipStream);

6. 连接 Stream.concat()  将两个流连接起来,第一个流不能是无限流,否则第二个流没有处理的机会

Stream<String> concatStream = Stream.concat(arrayList.stream(), Stream.of("aa","bb","cc"));
show("concatStream", concatStream);

7. 去重 distinct()   剔除重复元素

Stream<String> distinctStream = Stream.of("aa","bb","cc","aa").distinct();
show("distinctStream", distinctStream);

8. 排序 sorted()   按照默认排序或者传入比较器

//(1) 按照默认排序, 字典顺序比较
Stream<String> sortedStream =  Stream.of("aa","bb","cc","aa").sorted();
show("sortedStream", sortedStream);

sortedStream = Stream.of("aa","b1b","c22c","a33a","a4444b","a55555B").sorted(Comparator.comparing(x->x));	//提取Comparator进行排序
show("sortedStream", sortedStream);

//(2) 传入String的比较器
//1) 字典顺序比较
sortedStream = Stream.of("aa","bb","cc","aa","ab","aB").sorted(String::compareTo);
show("sortedStream", sortedStream);

sortedStream = Stream.of("aa","bb","cc","aa","ab","aB").sorted((x,y)->x.compareTo(y));
show("sortedStream", sortedStream);

//2) 比较length
sortedStream = Stream.of("aa","b1b","c22c","a33a","a4444b","a55555B").sorted(Comparator.comparing(String::length));	//提取Comparator进行排序
show("sortedStream", sortedStream);

sortedStream = Stream.of("aa","b1b","c22c","a33a","a4444b","a55555B").sorted((x,y)->Integer.compare(x.length(), y.length()));
show("sortedStream", sortedStream);

//3) 传入比较器		比较多个条件
sortedStream = Stream.of("aa","bb","cc","aa","ab","aB").sorted(
		(x, y)->{
			if(x.length()>y.length()){
				return 1;
			}else if(x.length()==y.length()){
				return x.compareTo(y);		//字典顺序比较
			}else{
				return -1;
			}
		});
show("sortedStream", sortedStream);

sortedStream = Stream.of("aa","bb","cc","aa","ab","aB").sorted(new Comparator<String>() {
	@Override
	public int compare(String x, String y) {
		if(x.length()>y.length()){
			return 1;
		}else if(x.length()==y.length()){
			return x.compareTo(y);
		}else{
			return -1;
		}
	}
});
show("sortedStream", sortedStream);

//4) 传入自定义的比较器		比较多个条件
sortedStream = Stream.of("aa","bb","cc","aa","ab","aB").sorted(ConvertStreamTest::myCompare);
show("sortedStream", sortedStream);


//5) 语法糖		比较多个条件
sortedStream = Stream.of("aa","b1b","c22c","a33a","a4444b","a55555B").sorted(Comparator.comparing(String::length).thenComparing(String::compareTo));
show("sortedStream", sortedStream);
 
//(3) reversed()	指定比较器倒叙
sortedStream = Stream.of("aa","b1b","c22c","a33a","a4444b","a55555B").sorted(Comparator.comparing((String x)->x).reversed());
show("sortedStream", sortedStream);

sortedStream = Stream.of("aa","b1b","c22c","a33a","a4444b","a55555B").sorted(Comparator.comparing(String::length).reversed());
show("sortedStream", sortedStream);

9. 类似代理 peek()   获得每个元素时,做一些事情

Stream<String> peekStream = Stream.of("aa","b1b","c22c","a33a","a4444b","a55555B").peek(x->System.out.println("peek() fetch element: "+x));
show("peekStream", peekStream);
原文地址:https://www.cnblogs.com/xiang--liu/p/9710392.html