我有两个包含两种类型对象的数组列表。他们是两种类型的用户。我输入了一个id(两种类型的用户都有唯一的整数id),并想找出该用户是否存在于两个arraylist中。
ArrayList<Artist> artist = new ArrayList();
ArrayList<Customer> customer = new ArrayList();
class Artist implements User{
private String name;
private int a_id = 0;
private ArrayList<ArrayList> albums = new ArrayList();
private int money;
private int ma = 999;
private int mi = 100;
public Artist(String name) {
this.name = name;
a_id = (int)(Math.random()*((ma - mi) + 1)) + mi;
}
}
...<i>getters and setters</i>
class Customer implements User {
private String name;
private int subscription = 1;
private int due = 0;
private int c_id = 0;
private int ma = 9999;
private int mi = 1000;
public Customer(String name) {
this.name = name;
c_id = (int) (Math.random() * ((ma - mi) + 1)) + mi;
}
}
...<i>getters and setters</i>
答案 0 :(得分:4)
使用anyMatch
方法和||
:
public boolean exist(ArrayList<Artist> artist, ArrayList<Customer> customer, int id) {
return artist.stream().anyMatch(user -> user.getA_id() == id) ||
customer.stream().anyMatch(user -> user.getC_id() == id);
}
答案 1 :(得分:1)
您应该考虑只包含一个类型为User的列表,因为Artist和Customer属于user类型(它们都实现该接口)。
然后,您可以简单地使用流api来确定用户ID是否存在:
page2.html