JDK8漫談 集合更強大

2022-01-16 03:13:49 字數 3928 閱讀 3300

stream像操作sql一樣處理資料,所以很概念非常相近,可以對比著理解 。

分為靜態建立和例項建立,不同的方法適用於不同場景。目的都是為了轉成流

/**

* 初始

* * 適用於陣列和簡單的資料轉為stream

* */

@test

public void test_static_of()

/*** 範圍

* * 適用於整數迭代

* */

@test

public void test_static_range()

/*** 生成

* * 適用於批量生成並且賦值

* 比如生成id或者uuid

* */

@test

public void test_static_generate()

@test

public void test_static_iterate() ).limit(10).foreach(system.out::println);

}/**

* 集合

*/@test

public void test_instance_collection()

/*** 正則

*/@test

public void test_regex()

/*** 檔案

* @throws ioexception

*/@test

public void test_file() throws ioexception

private listnewlist()

private string newarray() ;

}

把乙個流轉為另乙個流,可以不斷的轉換。包括:過濾,轉換,限制,排序,去重。通過不斷的轉換最終獲取目標資料

private streamnewstream()

/*** 過濾

* * 過濾不符合條件的資料。

* 比如說:

*

* */

@test

public void test_filter()

/*** 型別轉換

* * 用於快速轉換資料型別

* 比如說:

*

* */

@test

newstream()

.map(person::getid)

.foreach(system.out::println);

}/**

* 限制

* * 取前幾個

* */

@test

public void test_limit()

/*** 排序

*/@test

public void test_sorted()else if (p1.getage()* 通過這些計算可以快速找出期待的資料,而且還不需要不斷的新增遍歷和破壞**可讀性

* */

@test

public void test_combine()else if (age1乙個流只能有乙個中止動作。中止後流就不能使用。包括:查詢,匹配,迴圈,統計,型別轉換,分組。

private streamnewstream()

/*** 查詢**/

@test

public void test_find()

/*** 匹配

*/@test

public void test_match()

/*** 統計**/

@test

public void test_stat_max() else if (p1.getage() < p2.getage()) else

});system.out.println(maxage);

}/**

* 個數

*/@test

public void test_stat_count()

/*** 迴圈

*/@test

public void test_foreach()

/*** 歸併

*/@test

public void test_reduce()

/*** 轉為陣列**/

@test

public void test_toarray()

/*** 轉為collection

*/@test

public void test_tocollection()

/*** 轉為map

*/@test

public void test_tomap()

/*** 通過分組轉為map

** * 分組完,一般會對元素進行聚合

* */

@test

public void test_tomap_group()

@test

public void test_format() else if (age1 < age2) else

}).limit(1)

.foreach(system.out::println);

newstream()

.filter(person -> null == person)

.distinct()

.filter(person -> person.getage() >= 18)

.map(person::getage)

//上面排序超過三行,不容易閱讀

.sorted(comparator.comparing(long::valueof))

.limit(1)

.foreach(system.out::println);

}@test

public void test_single()

if(person.getage() > 18)

if(person.getname().startswith("hello"))

return false;});

newstream()

.filter(person -> null != person)

.filter(person -> person.getage() > 18)

.filter(person -> person.getname().startswith("hello"));}

@test

public void test_foreach()

}listadultpersons2 = persons.stream()

.filter(person -> person.getage() > 18)

.collect(collectors.tolist());

}

@test

public void test_foreach()

system.out.println(person);

});newstream()

.filter(person -> person.getage()>18)

.foreach(system.out::println);

}

誠邀,有夢想有激情有實力的小夥伴一起加入我們,改變世界。下面是jd的詳情資訊

阿里-菜鳥國際-出口大團隊招新啦

jdk8學習總結

今天抽點時間對jdk8寫個總體的學習總結,一來當作乙個覆盤加深理解,二來也希望能夠對閱讀的朋友們有一點點幫助,同時也希望大家對文中不準確的內容予以指出。1 函式式介面與lambda表示式 首先說一下函式式介面的概念 介面中的抽象方法如果是重寫了object類的方法的話,是不參與計數的 另外如果乙個介...

JDK8 集合之List Map新增方法

public void testlist public void testlist2 在指定的鍵存在,有與之相關的對映值時,才會將指定的鍵對映到指定的值 在指定的鍵不存在時,方法會return回來乙個null,指定的鍵值不會新增到map中 public void testmap string rep...

jdk8 之方法引用

方法引用 實際上是lamda函式的語法糖 有時候並不適用 我們可以講方法引用看做乙個函式指標.function pointer 方法引用分為4類 類名 靜態方法名 建立乙個類 public class student public void setname string name public in...