4

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>
4
  • I know how to search in a single arraylist, but since the user could be either an artist or a customer, i have to have a simultaneous search in both types of users. That I don't know how to approach. Commented Aug 25, 2018 at 8:12
  • I would use a combination of reduce to create a single Stream<User>, filter to locate the correct id and then findFirst to get the User if present. Commented Aug 25, 2018 at 8:26
  • 1
    What does the User interface look like, any getId() method in it? Commented Aug 25, 2018 at 8:32
  • User only has a stream method, since artists and customers are separate, i gave separate ranges for their ids, to make a distinction. Commented Aug 25, 2018 at 8:52

2 Answers 2

4

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You should think about having only one list of type User, because Artist and Customer are of type user (they both implement that interface).
Then you could simply use the stream api to determine wether a userId is there or not:

Optional<User> searchedUser = userList.stream().filter(User::getId.equals(userId).findFirst();

if(searchedUser.isPresent()) { //do something and handle missing }

2 Comments

The only issue with that is I require to have separate lists for both as all the other queries either only deal with one of them at a time. However, I could try to make a parallel list combining all users, if that is what you are suggesting.
for using a parallel approach, see @Sun answer. he tried with ´anyMatch()´

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.