Java8涓殑Stream
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java8涓殑Stream相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/getc' title='getc'>getc
鍏冪礌 static tag override 鎺掑簭 student com 甯哥敤
java8涔熷嚭鏉ュソ涔呬簡锛屾帴鍙i粯璁ゆ柟娉曪紝lambda琛ㄨ揪寮忥紝鍑芥暟寮忔帴鍙o紝Date API绛夌壒鎬ц繕鏄湁蹇呰鍘讳簡瑙d竴涓嬨€傛瘮濡傚湪椤圭洰涓粡甯哥敤鍒伴泦鍚堬紝閬嶅巻闆嗗悎鍙互璇曚笅lambda琛ㄨ揪寮忥紝缁忓父杩樿瀵归泦鍚堣繘琛岃繃婊ゅ拰鎺掑簭锛孲tream灏辨淳涓婄敤鍦轰簡銆?/p>
Stream浣滀负java8鐨勬柊鐗规€э紝鍩轰簬lambda琛ㄨ揪寮忥紝鏄闆嗗悎瀵硅薄鍔熻兘鐨勫寮猴紝瀹冧笓娉ㄤ簬瀵归泦鍚堝璞¤繘琛屽悇绉嶆悶绗戯紝渚垮埄鐨勮仛鍚堟搷浣滄垨鑰呭ぇ鎵归噺鐨勬暟鎹搷浣滐紝鎻愰珮浜嗙紪绋嬫晥鐜囧拰浠g爜鍙鎬с€?/p>
Stream鐨勫師鐞嗭細灏嗚澶勭悊鐨勫厓绱犵湅浣滀竴绉嶆祦锛岀暀鍦ㄧ閬撲腑浼犺緭锛屽苟涓斿彲浠ュ湪绠¢亾鐨勮妭鐐逛笂澶勭悊锛屽寘鎷繃婊ょ瓫閫夈€佸幓閲嶃€佹帓搴忋€佽仛鍚堢瓑銆傚厓绱犳祦鍦ㄧ閬撲腑缁忚繃涓棿鎿嶄綔鐨勫鐞嗭紝鏈€鍚庣敱鏈€缁堟搷浣滃緱鍒板墠闈㈠鐞嗙殑缁撴灉銆?/p>
闆嗗悎鏈変袱绉嶆柟寮忕敓鎴愭祦锛?/p>
- stream() - 涓洪泦鍚堝垱寤轰覆琛屾祦
- parallelStream() - 涓洪泦鍚堝垱寤哄苟琛屾祦
涓婂浘涓槸Stream绫荤殑绫荤粨鏋勫浘锛岄噷闈㈠寘鍚簡澶ч儴鍒嗙殑涓棿鍜岀粓姝㈡搷浣溿€?/p>
涓棿鎿嶄綔涓昏鏈変互涓嬫柟娉曪紙姝ょ被鍨嬫柟娉曡繑鍥炵殑閮芥槸Stream锛夛細map (mapToInt, flatMap 绛?銆?filter銆?distinct銆?sorted銆?peek銆?limit銆?skip銆?parallel銆?sequential銆?unordered
缁堟鎿嶄綔涓昏鏈変互涓嬫柟娉曪細forEach銆?forEachOrdered銆?toArray銆?reduce銆?collect銆?min銆?max銆?count銆?anyMatch銆?allMatch銆?noneMatch銆?findFirst銆?findAny銆?iterator
涓句緥璇存槑
棣栧厛涓轰簡璇存槑Stream瀵瑰璞¢泦鍚堢殑鎿嶄綔锛屾柊寤轰竴涓猄tudent绫伙紙瀛︾敓绫伙級,瑕嗗啓浜唀quals()鍜宧ashCode()鏂规硶
public class Student {
private Long id;
private String name;
private int age;
private String address;
public Student() {
}
public Student(Long id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
Student student = (Student) object;
return age == student.age &&
Objects.equals(id, student.id) &&
Objects.equals(name, student.name) &&
Objects.equals(address, student.address);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age, address);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public static void main(String[] args) {
Student s1 = new Student(1L, "鑲栨垬", 15, "娴欐睙");
Student s2 = new Student(2L, "鐜嬩竴鍗?quot;, 15, "婀栧寳");
Student s3 = new Student(3L, "鏉ㄧ传", 17, "鍖椾含");
Student s4 = new Student(4L, "鏉庣幇", 17, "娴欐睙");
List<Student> students = new ArrayList<>();
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
List<Student> streamStudents = testFilter(students);
streamStudents.forEach(System.out::println);
}
/**
* 闆嗗悎绛涢€? * @param students
* @return
*/
private static List<Student> testFilter(List<Student> students) {
return students.stream().filter(s -> "娴欐睙".equals(s.getAddress())).collect(Collectors.toList());
}
map(杞崲)
public static void main(String[] args) {
Student s1 = new Student(1L, "鑲栨垬", 15, "娴欐睙");
Student s2 = new Student(2L, "鐜嬩竴鍗?quot;, 15, "婀栧寳");
Student s3 = new Student(3L, "鏉ㄧ传", 17, "鍖椾含");
Student s4 = new Student(4L, "鏉庣幇", 17, "娴欐睙");
List<Student> students = new ArrayList<>();
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
List<Student> streamStudents = testFilter(students);
testMap(students);
}
/**
* 闆嗗悎杞崲
* @param students
*/
private static void testMap(List<Student> students) {
// 鍦ㄥ湴鍧€鍓嶉潰鍔犱笂閮ㄥ垎淇℃伅锛屽彧鑾峰彇鍦板潃杈撳嚭
List<String> address = students.stream().map(s -> "浣忓潃:" + s.getAddress()).collect(Collectors.toList());
address.forEach(a -> System.out.println(a));
}
distinct(鍘婚噸)
public static void main(String[] args) {
testDistinct1();
}
/**
* 闆嗗悎鍘婚噸锛堝熀鏈被鍨嬶級
*/
private static void testDistinct1() {
// 绠€鍗曞瓧绗︿覆鐨勫幓閲? List<String> list = Arrays.asList("111", "222", "333", "111", "222");
list.stream().distinct().forEach(System.out::println);
}
public static void main(String [] args) {
testDistinct2();
}
/**
* 闆嗗悎鍘婚噸锛堝紩鐢ㄥ璞★級
*/
private static void testDistinct2() {
//寮曠敤瀵硅薄鐨勫幓閲嶏紝寮曠敤瀵硅薄瑕佸疄鐜癶ashCode鍜宔qual鏂规硶锛屽惁鍒欏幓閲嶆棤鏁? Student s1 = new Student(1L, "鑲栨垬", 15, "娴欐睙");
Student s2 = new Student(2L, "鐜嬩竴鍗?quot;, 15, "婀栧寳");
Student s3 = new Student(3L, "鏉ㄧ传", 17, "鍖椾含");
Student s4 = new Student(4L, "鏉庣幇", 17, "娴欐睙");
Student s5 = new Student(1L, "鑲栨垬", 15, "娴欐睙");
List<Student> students = new ArrayList<>();
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
students.add(s5);
students.stream().distinct().forEach(System.out::println);
}
public static void main(String[] args) {
Student s1 = new Student(1L, "鑲栨垬", 15, "娴欐睙");
Student s2 = new Student(2L, "鐜嬩竴鍗?quot;, 15, "婀栧寳");
Student s3 = new Student(3L, "鏉ㄧ传", 17, "鍖椾含");
Student s4 = new Student(4L, "鏉庣幇", 17, "娴欐睙");
List<Student> students = new ArrayList<>();
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
List<Student> streamStudents = testFilter(students);
testMap(students);
}
/**
* 闆嗗悎杞崲
* @param students
*/
private static void testMap(List<Student> students) {
// 鍦ㄥ湴鍧€鍓嶉潰鍔犱笂閮ㄥ垎淇℃伅锛屽彧鑾峰彇鍦板潃杈撳嚭
List<String> address = students.stream().map(s -> "浣忓潃:" + s.getAddress()).collect(Collectors.toList());
address.forEach(a -> System.out.println(a));
}
public static void main(String[] args) {
testDistinct1();
}
/**
* 闆嗗悎鍘婚噸锛堝熀鏈被鍨嬶級
*/
private static void testDistinct1() {
// 绠€鍗曞瓧绗︿覆鐨勫幓閲? List<String> list = Arrays.asList("111", "222", "333", "111", "222");
list.stream().distinct().forEach(System.out::println);
}
public static void main(String [] args) {
testDistinct2();
}
/**
* 闆嗗悎鍘婚噸锛堝紩鐢ㄥ璞★級
*/
private static void testDistinct2() {
//寮曠敤瀵硅薄鐨勫幓閲嶏紝寮曠敤瀵硅薄瑕佸疄鐜癶ashCode鍜宔qual鏂规硶锛屽惁鍒欏幓閲嶆棤鏁? Student s1 = new Student(1L, "鑲栨垬", 15, "娴欐睙");
Student s2 = new Student(2L, "鐜嬩竴鍗?quot;, 15, "婀栧寳");
Student s3 = new Student(3L, "鏉ㄧ传", 17, "鍖椾含");
Student s4 = new Student(4L, "鏉庣幇", 17, "娴欐睙");
Student s5 = new Student(1L, "鑲栨垬", 15, "娴欐睙");
List<Student> students = new ArrayList<>();
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
students.add(s5);
students.stream().distinct().forEach(System.out::println);
}
杩愯缁撴灉锛?/p>
Student{id=1, name='鑲栨垬', age=15, address='娴欐睙'}
Student{id=2, name='鐜嬩竴鍗?#39;, age=15, address='婀栧寳'}
Student{id=3, name='鏉ㄧ传', age=17, address='鍖椾含'}
Student{id=4, name='鏉庣幇', age=17, address='娴欐睙'}
鍙互鐪嬪嚭锛屼袱涓噸澶嶇殑鈥滆倴鎴樷€濆悓瀛﹁繘琛屼簡鍘婚噸锛岃繖涓嶄粎鍥犱负浣跨敤浜哾istinct()鏂规硶锛岃€屼笖鍥犱负Student瀵硅薄閲嶅啓浜唀quals鍜宧ashCode()鏂规硶锛屽惁鍒欏幓閲嶆槸鏃犳晥鐨?/p>
sorted(鎺掑簭)
public static void main(String[] args) {
testSort();
}
/**
* 闆嗗悎鎺掑簭锛堟寚瀹氭帓搴忚鍒欙級
*/
private static void testSort() {
Student s1 = new Student(1L, "鑲栨垬", 15, "娴欐睙");
Student s2 = new Student(2L, "鐜嬩竴鍗?quot;, 15, "婀栧寳");
Student s3 = new Student(3L, "鏉ㄧ传", 17, "鍖椾含");
Student s4 = new Student(4L, "鏉庣幇", 17, "娴欐睙");
List<Student> students = new ArrayList<>();
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
students.stream()
.sorted((stu1, stu2) -> Long.compare(stu2.getId(), stu1.getId()))
.sorted((stu1, stu2) -> Integer.compare(stu2.getAge(), stu1.getAge()))
.forEach(System.out::println);
}
limit(闄愬埗杩斿洖涓暟)
public static void main(String[] args) {
testLimit();
}
/**
* 闆嗗悎limit锛岃繑鍥炲墠鍑犱釜鍏冪礌
*/
private static void testLimit() {
List<String> list = Arrays.asList("333", "222", "111");
list.stream().limit(2).forEach(System.out::println);
}
skip(鍒犻櫎鍏冪礌)
public static void main(String[] args) {
testSkip();
}
/**
* 闆嗗悎skip锛屽垹闄ゅ墠n涓厓绱? */
private static void testSkip() {
List<String> list = Arrays.asList("333", "222", "111");
list.stream().skip(2).forEach(System.out::println);
}
reduce锛堣仛鍚堬級
public static void main(String[] args) {
testReduce();
}
/**
* 闆嗗悎reduce锛屽皢闆嗗悎涓瘡涓厓绱犳暣鍚堟垚涓€鏉℃暟鎹? */
private static void testReduce() {
List<String> list = Arrays.asList("娆?quot;, "杩?quot;, "浣?quot;);
String appendStr = list.stream().reduce("鍖椾含", (a, b) -> a + b);
System.out.println(appendStr);
}
anyMatch/allMatch/noneMatch锛堝尮閰?/h3>
public static void main(String[] args) {
testMatch();
}
/**
* anyMatch锛歋tream 涓换鎰忎竴涓厓绱犵鍚堜紶鍏ョ殑 predicate锛岃繑鍥?true
allMatch锛歋tream 涓叏閮ㄥ厓绱犵鍚堜紶鍏ョ殑 predicate锛岃繑鍥?true
noneMatch锛歋tream 涓病鏈変竴涓厓绱犵鍚堜紶鍏ョ殑 predicate锛岃繑鍥?true
*/
private static void testMatch() {
Student s1 = new Student(1L, "鑲栨垬", 15, "娴欐睙");
Student s2 = new Student(2L, "鐜嬩竴鍗?quot;, 15, "婀栧寳");
Student s3 = new Student(3L, "鏉ㄧ传", 17, "鍖椾含");
Student s4 = new Student(4L, "鏉庣幇", 17, "娴欐睙");
List<Student> students = new ArrayList<>();
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
boolean anyMatch = students.stream().anyMatch(s -> "婀栧寳".equals(s.getAddress()));
if (anyMatch) {
System.out.println("鏈夋箹鍖椾汉");
}
boolean allMatch = students.stream().allMatch(s -> s.getAge() >= 15);
if (allMatch) {
System.out.println("鎵€鏈夊鐢熼兘婊?5鍛ㄥ瞾");
}
boolean noneMatch = students.stream().noneMatch(s -> "鏉ㄦ磱".equals(s.getName()));
if (noneMatch) {
System.out.println("娌℃湁鍙潹娲嬬殑鍚屽");
}
}
public static void main(String[] args) {
testMatch();
}
/**
* anyMatch锛歋tream 涓换鎰忎竴涓厓绱犵鍚堜紶鍏ョ殑 predicate锛岃繑鍥?true
allMatch锛歋tream 涓叏閮ㄥ厓绱犵鍚堜紶鍏ョ殑 predicate锛岃繑鍥?true
noneMatch锛歋tream 涓病鏈変竴涓厓绱犵鍚堜紶鍏ョ殑 predicate锛岃繑鍥?true
*/
private static void testMatch() {
Student s1 = new Student(1L, "鑲栨垬", 15, "娴欐睙");
Student s2 = new Student(2L, "鐜嬩竴鍗?quot;, 15, "婀栧寳");
Student s3 = new Student(3L, "鏉ㄧ传", 17, "鍖椾含");
Student s4 = new Student(4L, "鏉庣幇", 17, "娴欐睙");
List<Student> students = new ArrayList<>();
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
boolean anyMatch = students.stream().anyMatch(s -> "婀栧寳".equals(s.getAddress()));
if (anyMatch) {
System.out.println("鏈夋箹鍖椾汉");
}
boolean allMatch = students.stream().allMatch(s -> s.getAge() >= 15);
if (allMatch) {
System.out.println("鎵€鏈夊鐢熼兘婊?5鍛ㄥ瞾");
}
boolean noneMatch = students.stream().noneMatch(s -> "鏉ㄦ磱".equals(s.getName()));
if (noneMatch) {
System.out.println("娌℃湁鍙潹娲嬬殑鍚屽");
}
}
涓婇潰浠嬬粛浜哠tream甯哥敤鐨勪竴浜涙柟娉曪紝铏界劧瀵归泦鍚堢殑閬嶅巻鍜屾搷浣滃彲浠ョ敤浠ュ墠甯歌鐨勬柟寮忥紝浣嗘槸褰撲笟鍔¢€昏緫澶嶆潅鐨勬椂鍊欙紝浣犱細鍙戠幇浠g爜閲忓緢澶氾紝鍙鎬у緢宸紝鏄庢槑涓€琛屼唬鐮佽В鍐崇殑浜嬫儏锛屼綘鍗村啓浜嗗ソ鍑犺銆傝瘯璇昹ambda琛ㄨ揪寮忥紝璇曡瘯Stream锛屼綘浼氭湁涓嶄竴鏍风殑浣撻獙銆?/p>
以上是关于Java8涓殑Stream的主要内容,如果未能解决你的问题,请参考以下文章