La búsqueda de un objeto en 2 ArrayLists

therealshankman:

Tengo dos ArrayLists de dos tipos de objetos. Son dos tipos de usuarios. entrada I un id (ambos tipos de usuarios tienen identificadores de enteros únicos), y quiere averiguar si el usuario existe entre los dos 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:

Utilice anyMatchmétodo y ||:

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

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=186554&siteId=1
Recomendado
Clasificación