StreamApi是jdk1.8所提供的新特性,基于lambda表達式,對數據流的一系列操作。數據源可以來自很多地方,可以是數組、集合也可以是文件等等。對于Stream它包含兩個操作,中間操作和終止操作,終止操作出現之前是不會進行中間操作的,也就是說想要使用StreamAPI就必須要有一個終止操作,否者代碼將不執行。
Streamstream = Stream.of(1,2,3,4,5);
Listlist = List.of(1,2,3,4,5);
Streamstream = list.stream();
Integer a[] = new Integer[5];
Streamstream = Arrays.stream(a);
Streamlines = Files.lines(Paths.get("D://a.txt"));
public class StreamApp {
private ListdataList;
@Before
public void before() {
dataList = new ArrayList<>(List.of("zhangsan", "lisi", "wangwu", "zhaoliu", "gaoyuanyuan", "xietingfeng", "zhaoliu"));
}
@Test
public void testMap() {
// map操作
dataList.stream().map(User::new).forEach(System.out::println);
}
@Test
public void testFilter() {
// filter操作
dataList.stream().filter(name ->name.startsWith("z")).forEach(System.out::println);
}
@Test
public void testAllMatch() {
// allMatch操作
boolean boo = dataList.stream().allMatch(name ->name.matches("\\D+"));
System.out.println(boo);
}
@Test
public void testAnyMatch() {
// anyMatch操作
boolean boo = dataList.stream().anyMatch(name ->name.contains("y"));
System.out.println(boo);
}
@Test
public void testCount() {
//統計操作
System.out.println(dataList.stream().count());
}
@Test
public void testDistinct() {
//去重操作
dataList.stream().distinct().forEach(System.out::println);
}
@Test
public void testFindAny() {
//findAny操作
System.out.println(dataList.stream().findAny().orElse("liuhf"));
}
@Test
public void testFindFirst() {
// findFirst操作
System.out.println(dataList.stream().findFirst().orElse("liuhf"));
}
@Test
public void testFlatMap() {
// 扁平化
dataList.stream().flatMap(name->Stream.of(name.split("z"))).forEach(System.out::println);
}
@Test
public void testForeach(){
// 遍歷
dataList.stream().forEach(System.out::println);
}
@Test
public void testForeachOrder(){
dataList.stream().forEachOrdered(System.out::println);
}
@Test
public void testLimit(){
// limit操作
dataList.stream().limit(2).forEach(System.out::println);
}
@Test
public void testMax(){
// 查大值
System.out.println(dataList.stream().max(Comparator.comparingInt(n ->n.charAt(0))).orElse("liuhf"));
}
@Test
public void testMin(){
// 查最小值
System.out.println(dataList.stream().min(Comparator.comparingInt(n ->n.charAt(0))).orElse("liuhf"));
}
@Test
public void testNoneMatch(){
// 查不存在
System.out.println(dataList.stream().noneMatch(name ->name.startsWith("a")));
}
@Test
public void testPeek(){
// peek 操作
Listnames = dataList.stream().peek(name ->System.out.println(name.charAt(0))).toList();
}
@Test
public void testReduce(){
// 統計
int val = Stream.of(1, 2, 3, 4, 5).reduce(Integer::sum).orElse(0);
System.out.println(val);
int value = Stream.of(1, 2, 3, 4, 5).reduce(0, Integer::sum);
System.out.println(value);
Integer reduce = Stream.of(1, 2, 3, 4, 5).reduce(0, Integer::sum, Integer::sum);
System.out.println(reduce);
}
@Test
public void testSorted(){
// 排序
dataList.stream().sorted().forEach(System.out::println);
System.out.println(">>>>>>>>>>>>>>>>>>>");
dataList.stream().sorted(Comparator.comparing(x->x.charAt(1))).forEach(System.out::println);
}
@Test
public void testToArray(){
//轉換為數組
Object[] array = dataList.stream().toArray();
for (Object o : array) {
System.out.println(o);
}
System.out.println(">>>>>>>>>>>>>");
//可以使用構造引用,為了表訴清楚先這樣編寫
for (String s : dataList.stream().toArray(x ->new String[x])) {
System.out.println(s);
}
}
@Test
public void testSkip(){
//跳過
dataList.stream().skip(2).forEach(System.out::println);
}
static class User {
String name;
public User(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
}
public class CollectorApp {
private Listdata;
@Before
public void before() {
data = new ArrayList<>(List.of("1", "2", "3", "4", "5"));
}
@Test
public void testAveragingInt() {
//求平均值
Double collect = data.stream().collect(Collectors.averagingInt(Integer::valueOf));
System.out.println(collect);
}
@Test
public void testAveragingDouble() {
//求平均值
Double collect = data.stream().collect(Collectors.averagingDouble(Double::valueOf));
System.out.println(collect);
}
@Test
public void testAveragingLong() {
//求平均值
Double collect = data.stream().collect(Collectors.averagingLong(Long::valueOf));
System.out.println(collect);
}
@Test
public void testCollectingAndThen(){
//收集以及做額外的轉換
Setcollect = data.stream().collect(Collectors.collectingAndThen(Collectors.toSet(), Collections::unmodifiableSet));
}
@Test
public void testCounting(){
Long count = data.stream().collect(Collectors.counting());
System.out.println(count);
}
@Test
public void testGroupingBy(){
//參數1: 分組函數
Map>map = data.stream().collect(Collectors.groupingBy(num ->Integer.parseInt(num) % 2 == 0));
System.out.println(map);
//參數1:分組函數 參數2:收集器類型
Map>map1 = data.stream().collect(Collectors.groupingBy(num ->Integer.parseInt(num) % 2 == 0, Collectors.toSet()));
System.out.println(map1);
//參數1:分組函數 參數2:工廠,提供容器類型 參數3:收集器類型
TreeMap>map2 = data.stream().collect(Collectors.groupingBy(num ->Integer.parseInt(num) % 2 == 0, () ->new TreeMap<>(), Collectors.toSet()));
System.out.println(map2);
}
@Test
public void testJoining(){
//將元素合并為字符串且無分隔符
String collect = data.stream().collect(Collectors.joining());
System.out.println(collect);
String collect1 = data.stream().collect(Collectors.joining(","));
System.out.println(collect1);
String collect2 = data.stream().collect(Collectors.joining(",", "[", "]"));
System.out.println(collect2);
}
@Test
public void testMapping(){
// 同stream的map函數,使用頻率較少,只在stream的map函數不方便使用是才會選擇
Setcollect = data.stream().collect(Collectors.mapping(Integer::parseInt, Collectors.toSet()));
System.out.println(collect);
}
@Test
public void testMaxBy(){
// 求大值,同stream的max()
String max = data.stream().collect(Collectors.maxBy((n1, n2) ->Integer.parseInt(n1) - Integer.parseInt(n2))).orElse("0");
System.out.println(max);
}
@Test
public void testMinBy(){
// 求最小值,同stream的min()
String min = data.stream().collect(Collectors.minBy((n1, n2) ->Integer.parseInt(n1) - Integer.parseInt(n2))).orElse("0");
System.out.println(min);
}
@Test
public void testPartitioningBy(){
//分割,一個特殊分組,只能分出boolean類型的key
Map>map = data.stream().collect(Collectors.partitioningBy(num ->Integer.parseInt(num) % 2 == 0));
System.out.println(map);
//提供一個收集器
Map>map1 = data.stream().collect(Collectors.partitioningBy(num ->Integer.parseInt(num) % 2 == 0,Collectors.toSet()));
System.out.println(map1);
}
@Test
public void testReducing(){
//同stream函數reduce函數,合并
String s = data.stream().collect(Collectors.reducing((n1, n2) ->String.valueOf(Integer.parseInt(n1) + Integer.parseInt(n2)))).orElse("0");
System.out.println(s);
String s1 = data.stream().collect(Collectors.reducing("0",(n1, n2) ->String.valueOf(Integer.parseInt(n1) + Integer.parseInt(n2))));
System.out.println(s1);
String s2 = data.stream().collect(Collectors.reducing("0",(n)->String.valueOf(Integer.parseInt(n)+1),(n1, n2) ->String.valueOf(Integer.parseInt(n1) + Integer.parseInt(n2))));
System.out.println(s2);
}
@Test
public void testSummarizing(){
//總結
DoubleSummaryStatistics collect = data.stream().collect(Collectors.summarizingDouble(Double::parseDouble));
System.out.println(collect);
IntSummaryStatistics collect1 = data.stream().collect(Collectors.summarizingInt(Integer::parseInt));
System.out.println(collect1);
LongSummaryStatistics collect2 = data.stream().collect(Collectors.summarizingLong(Long::parseLong));
System.out.println(collect2);
}
@Test
public void testSumming(){
//計算總和
Double aDouble = data.stream().collect(Collectors.summingDouble(Double::parseDouble));
System.out.println(aDouble);
Integer aInt = data.stream().collect(Collectors.summingInt(Integer::parseInt));
System.out.println(aInt);
Long aLong = data.stream().collect(Collectors.summingLong(Long::parseLong));
System.out.println(aLong);
}
@Test
public void testToList(){
Listcollect = data.stream().collect(Collectors.toList());
}
@Test
public void testToSet(){
Setcollect = data.stream().collect(Collectors.toSet());
}
@Test
public void testCollection(){
LinkedListcollect = data.stream().collect(Collectors.toCollection(LinkedList::new));
}
@Test
public void testToMap(){
Mapcollect1 = data.stream().collect(Collectors.toMap(k ->Integer.parseInt(k) + 1, v ->v));
Mapcollect2 = data.stream().collect(Collectors.toMap(k ->Integer.parseInt(k) + 1, v ->v,(n1,n2)->String.valueOf(Integer.parseInt(n1)+Integer.parseInt(n2))));
Mapcollect3 = data.stream().collect(Collectors.toMap(k ->Integer.parseInt(k) + 1, v ->v,(n1,n2)->String.valueOf(Integer.parseInt(n1)+Integer.parseInt(n2)),TreeMap::new));
System.out.println(collect1);
System.out.println(collect2);
System.out.println(collect3);
//toConcurrentMap同toMap一樣,只是Map為線程安全的ConcurrentMap
}
}
你是否還在尋找穩定的海外服務器提供商?創新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統配攻擊溯源,準確流量調度確保服務器高可用性,企業級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
標題名稱:Stream流用法示例-創新互聯
分享鏈接:http://www.yijiale78.com/article12/dgedgc.html
成都網站建設公司_創新互聯,為您提供網站策劃、營銷型網站建設、外貿建站、網站營銷、網頁設計公司、手機網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯