1、anyMatch?
判斷數(shù)據(jù)列表中是否存在任意一個元素符合設置的predicate條件,如果是就返回true,否則返回false。
接口定義:
boolean anyMatch(Predicate<? super T> predicate);
方法描述:
在anyMatch 接口定義中是接收 Predicate 類型參數(shù),在Lamdba表達式中 Predicate<T> 是接收一個T類型參數(shù),然后經(jīng)過邏輯驗證返回布爾值結果。這里anyMatch表示,判斷的條件里,任意一個元素符合條件,就返回true值。
使用場景:
兩個集合的交集
?
@Test
public void a17() {
List<User> list = new ArrayList<>();
list.add(new User("張三", 12, "南京"));
list.add(new User("李四", 13, "北京"));
list.add(new User("王五", 14, "蘇州"));
list.add(new User("王五", 17, "蘇州"));
List<User> userList = new ArrayList<>();
userList.add(new User("李四", 13, "北京"));
userList.add(new User("王五", 20, "廣州"));
// 獲取兩個集合中有相同名字或者年齡相同的,只要滿足其中一個條件即可,只會返回list集合里面的元素,有先后順序返回
List<User> users1 = list.stream()
.filter(a -> userList.stream().anyMatch(b -> a.getName().equals(b.getName()) || a.getAge() == b.getAge()))
.collect(Collectors.toList());
// 獲取兩個集合中相同名字并且年齡相同的,必須同時滿足兩個條件
List<User> users2 = list.stream()
.filter(a -> userList.stream().anyMatch(b -> a.getName().equals(b.getName()) && a.getAge() == b.getAge()))
.collect(Collectors.toList());
users1.forEach(item -> {
System.out.println(item.getName() + item.getAge() + item.getEmailAddress());
});
/** 第一種結果展示:
* 李四13北京
* 王五14蘇州
* 王五17蘇州
*/
users2.forEach(item -> {
System.out.println(item.getName() + item.getAge() + item.getEmailAddress());
});
/** 第二種結果展示:
* 李四13北京
*/
}
@Test
public void a15() {
Stream<String> stream = Stream.of("ac", "bcddddd", "bd");
// 判斷stream中其中任何一個元素中只要有包含b字符串或者l字符串就返回true
boolean isMatch = stream.anyMatch(str -> str.contains("b") || str.contains("l"));
System.out.println(isMatch); // true
}
2、allMatch
判斷數(shù)據(jù)列表中全部元素都符合設置的predicate條件,如果是就返回true,否則返回false,流為空時總是返回true。
接口定義:
boolean allMatch(Predicate<? super T> predicate);
方法描述:
在allMatch 接口定義中是接收 Predicate 類型參數(shù),在Lamdba表達式中 Predicate<T> 是接收一個T類型參數(shù),然后經(jīng)過邏輯驗證返回布爾值結果。這里allMatch表示,判斷的條件里,全部元素符合條件,就返回true值。
適用場景:文章來源:http://www.zghlxwxcb.cn/news/detail-740861.html
基本類型的集合,但不適合于對象集合(我自己的理解)
allMatch里面不適合寫 && ,只適合寫 ||,如果寫&&,編譯器會自動報黃色波浪線
?
@Test
public void a18() {
List<String> typeList1 = Arrays.asList("1", "2");
List<String> typeList2 = Arrays.asList("1", "2", "3", "4");
// 集合列表中全部元素必須在allMatch里面的那些字符串,只要全部元素中有任意一個不同的元素在AllMatch中就返回false
boolean isMatch1 = typeList1.stream().allMatch(a -> a.equals("1") || a.equals("2") || a.equals("3"));
boolean isMatch2 = typeList2.stream().allMatch(a -> a.equals("1") || a.equals("2") || a.equals("3"));
System.out.println(isMatch1); // true
System.out.println(isMatch2); // false
}
@Test
public void a16() {
Stream<String> stream = Stream.of("abc", "abc", "bcd");
// 判斷stream中全部所有元素必須全部包含b字符串和c字符串就返回true,如果有一個元素不包含這兩個字符串就返回false
boolean isMatch = stream.allMatch(str -> str.contains("b") && str.contains("c"));
System.out.println(isMatch); // true
}
3、noneMatch
判斷數(shù)據(jù)列表中全部元素都不符合設置的predicate條件,如果是就返回true,否則返回false,流為空時總是返回true。
接口定義:
boolean noneMatch(Predicate<? super T> predicate);
方法描述:
在noneMatch接口定義中是接收 Predicate 類型參數(shù),在Lamdba表達式中 Predicate<T> 是接收一個T類型參數(shù),然后經(jīng)過邏輯驗證返回布爾值結果。這里noneMatch表示與allMatch相反,判斷的條件里的元素,所有的元素都不符合,就返回true值。
適用場景:
兩個集合的差集 (本人只想到這么做,如果有更簡潔的可以告訴我怎么寫,感謝0.0)
?文章來源地址http://www.zghlxwxcb.cn/news/detail-740861.html
@Test
public void a17() {
List<User> list = new ArrayList<>();
list.add(new User("張三", 12, "南京"));
list.add(new User("李四", 13, "北京"));
list.add(new User("王五", 14, "蘇州"));
list.add(new User("王五", 17, "蘇州"));
List<User> userList = new ArrayList<>();
userList.add(new User("李四", 13, "北京"));
userList.add(new User("王五", 20, "廣州"));
// 獲取list集合和userList集合過濾掉兩者集合中名字和年齡相同的數(shù)據(jù)后,只返回list集合的數(shù)據(jù)
List<User> users3 = list.stream()
.filter(a -> userList.stream().noneMatch(b -> a.getName().equals(b.getName()) && a.getAge() == b.getAge()))
.collect(Collectors.toList());
// 獲取userlist集合和list集合過濾掉兩者集合中名字和年齡相同的數(shù)據(jù)后,只返回userList集合的數(shù)據(jù)
List<User> users4 = userList.stream()
.filter(a -> list.stream().noneMatch(b -> a.getName().equals(b.getName()) && a.getAge()==b.getAge()))
.collect(Collectors.toList());
// 獲取list和userList集合之間的差集,將上面兩者集合合并,就是兩個集合的差集
List<User> arrayList = new ArrayList<>();
arrayList.addAll(users3);
arrayList.addAll(users4);
arrayList.forEach(item -> {
System.out.println(item.getName() + item.getAge() + item.getEmailAddress());
});
/** 兩者集合之間的差集
* 張三12南京
* 王五14蘇州
* 王五17蘇州
* 王五20廣州
*/
System.out.println("-------------------");
users3.forEach(item -> {
System.out.println(item.getName() + item.getAge() + item.getEmailAddress());
});
/** 只返回list集合中過濾掉之后的元素集合
* 張三12南京
* 王五14蘇州
* 王五17蘇州
*/
System.out.println("-------------------");
users4.forEach(item -> {
System.out.println(item.getName() + item.getAge() + item.getEmailAddress());
});
/** 只返回userList集合中過濾掉之后的元素集合
* 王五20廣州
*/
}
@Test
public void a19() {
List<String> typeList1 = Arrays.asList("1", "2");
List<String> typeList2 = Arrays.asList("1", "2", "3", "4");
// 集合列表中全部元素只要都不在noneMatch里面的判斷中,就返回true,集合列表中任何元素中只要有一個在noneMatch的判斷中就返回false
boolean isMatch1 = typeList1.stream().noneMatch(a -> a.equals("3") || a.equals("4"));
boolean isMatch2 = typeList2.stream().noneMatch(a -> a.equals("1") || a.equals("2") || a.equals("3"));
System.out.println(isMatch1); // true
System.out.println(isMatch2); // false
}
/** noneMatch */
@Test
public void a20() {
Stream<String> stream = Stream.of("dddd", "ee", "qqq", "bcfff");
// 判斷 stream 中所有的元素都不是以 a 開頭,就返回true,如果所有的元素中只要其中一個元素是以a開頭的,就返回false
boolean isMatch = stream.noneMatch(str->str.startsWith("a"));
System.out.println(isMatch); // true
}
到了這里,關于java8利用Stream方法求兩個List對象的交集、差集與并集(即:anyMatch和allMatch和noneMatch的區(qū)別詳解)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!