集合A List<VisitSaleProblem> 中有 id字段,集合B List<String> 如何使用Java8进行判断 B中的是数据是否和集合A中的id字段相匹配
时间: 2024-03-14 15:45:36 浏览: 18
可以使用Java8的Stream API来实现这个功能,具体实现步骤如下:
1. 将集合A中的VisitSaleProblem对象的id字段提取出来,使用Collectors.toSet()方法将其转换成Set集合。
```java
Set<Integer> idSet = listA.stream().map(VisitSaleProblem::getId).collect(Collectors.toSet());
```
2. 使用Stream的allMatch方法判断集合B中的每个元素是否都能在集合A中找到相应的VisitSaleProblem对象。
```java
boolean allMatch = listB.stream().allMatch(idSet::contains);
```
其中,allMatch方法会对集合B中的每个元素调用idSet::contains方法进行判断,如果所有元素都能在集合A中找到相应的VisitSaleProblem对象,则allMatch方法返回true,否则返回false。
完整实现代码如下:
```java
Set<Integer> idSet = listA.stream().map(VisitSaleProblem::getId).collect(Collectors.toSet());
boolean allMatch = listB.stream().allMatch(idSet::contains);
```
相关问题
java8写一个将List<A>与List<B>中多个字段的值进行匹配,匹配一致的List<A>与List<B>分别出力
假设List<A>中有属性a1、a2,List<B>中有属性b1、b2,要匹配a1与b1以及a2与b2是否一致,若一致则将相应的List<A>与List<B>分别输出。
代码如下:
```
import java.util.ArrayList;
import java.util.List;
public class MatchList {
public static void main(String[] args) {
List<A> aList = new ArrayList<>();
aList.add(new A("a1", 1));
aList.add(new A("a2", 2));
aList.add(new A("a3", 3));
List<B> bList = new ArrayList<>();
bList.add(new B("b2", 2));
bList.add(new B("b1", 1));
bList.add(new B("b4", 4));
List<A> matchedAList = new ArrayList<>();
List<B> matchedBList = new ArrayList<>();
for (A a : aList) {
for (B b : bList) {
if (a.getA1().equals(b.getB1()) && a.getA2() == b.getB2()) {
matchedAList.add(a);
matchedBList.add(b);
}
}
}
System.out.println("Matched List A:");
for (A a : matchedAList) {
System.out.println(a);
}
System.out.println("Matched List B:");
for (B b : matchedBList) {
System.out.println(b);
}
}
}
class A {
private String a1;
private int a2;
public A(String a1, int a2) {
this.a1 = a1;
this.a2 = a2;
}
public String getA1() {
return a1;
}
public int getA2() {
return a2;
}
@Override
public String toString() {
return "A{" +
"a1='" + a1 + '\'' +
", a2=" + a2 +
'}';
}
}
class B {
private String b1;
private int b2;
public B(String b1, int b2) {
this.b1 = b1;
this.b2 = b2;
}
public String getB1() {
return b1;
}
public int getB2() {
return b2;
}
@Override
public String toString() {
return "B{" +
"b1='" + b1 + '\'' +
", b2=" + b2 +
'}';
}
}
```
注:以上代码仅作为示例,实际开发中需要根据具体情况进行修改。
java8写一个当List<B>中多个字段与List<A>进行match,match不到时,将只在List<B>中存在的数据出力的共通方法
可以使用Java 8的Stream API来实现这个功能。具体实现步骤如下:
1. 定义一个方法,参数为List<A>和List<B>,返回值为List<B>,表示只在List<B>中存在的数据。
2. 使用Stream的filter方法和anyMatch方法,对每个B对象进行判断,如果B对象的多个字段与List<A>中任意一个A对象的多个字段匹配,则将该B对象过滤掉。
3. 最后使用Stream的collect方法,将剩余的B对象收集到一个新的List中并返回。
下面是代码实现:
```java
public static <A, B> List<B> filterByMatch(List<A> listA, List<B> listB,
Function<B, List<Object>> bFieldsGetter,
Function<A, List<Object>> aFieldsGetter) {
return listB.stream()
.filter(b -> !listA.stream()
.anyMatch(a -> bFieldsGetter.apply(b).equals(aFieldsGetter.apply(a))))
.collect(Collectors.toList());
}
```
其中,bFieldsGetter和aFieldsGetter分别是获取B对象和A对象多个字段值的函数。使用时需要根据具体情况实现这两个函数。
示例代码:
```java
class A {
private int id;
private String name;
public A(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
class B {
private int id;
private String name;
private int age;
public B(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args) {
List<A> listA = Arrays.asList(new A(1, "Alice"), new A(2, "Bob"));
List<B> listB = Arrays.asList(new B(1, "Alice", 20), new B(2, "Bob", 30),
new B(3, "Charlie", 25), new B(4, "David", 40));
List<B> result = filterByMatch(listA, listB,
b -> Arrays.asList(b.getId(), b.getName()),
a -> Arrays.asList(a.getId(), a.getName()));
System.out.println(result); // [B{id=3, name='Charlie', age=25}, B{id=4, name='David', age=40}]
}
}
```
在上面的示例中,我们通过调用filterByMatch方法,将只在List<B>中存在的数据筛选出来,并输出到控制台。