Searching for an object in 2 arraylists

therealshankman :

I have two arraylists of two types of objects. They are two types of users. I input an id(both types of users have unique integer ids), and want to find out if the user exists among the two arraylists.

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>
xingbin :

Use anyMatch method and ||:

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);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=76182&siteId=1