0

I have an ArrayList of data class named Customer:

public class Customer {

    private String userid;
    private String fullName;
    private String phoneNumber;
    private String email;
    private String addressOne;
    private String addressTwo;
    private String city;
    private String state;
    private String zip;
    private String country;

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddressOne() {
        return addressOne;
    }

    public void setAddressOne(String addressOne) {
        this.addressOne = addressOne;
    }

    public String getAddressTwo() {
        return addressTwo;
    }

    public void setAddressTwo(String addressTwo) {
        this.addressTwo = addressTwo;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

I create array list of customers

ArrayList<Customer> customersList = new ArrayList<Customer>();

and I load the ArrayList with many customers in a loop.

How can I check if a customer with the name "David Paul" is already in the ArrayList?

2 Answers 2

3

You can do:

boolean contains = customersList.stream()
                     .anyMatch(c -> c.getFullName().equals("David Paul"));
Sign up to request clarification or add additional context in comments.

7 Comments

it works, I will accept it in 10 minutes when stackoverflow allows. But c -> c is new syntax to me. What is this called?
Would contains() suffice?
@DizzyCode Not in this case, I think because I'm using a dataclass and not a string
@Arya c -> ... is the lambda notation introduced in Java 8
@Arya you can override the equals method in that class to only use fullName if you're not using it anywhere else, then contains will work but still will be slow.
|
3

With an ArrayList you'll have to simply traverse the whole list element by element and check if a given customer is there. This will be slow (O(n^2) slow).

If you can changing a list to a Set would be a preferred way, then you can just override the hashCode and equals methods to use fullName and check it in constant time using set.contains(). Or you can make a Map of <String, Customer> where the keys will be fullName and again you can check in constant time whether a given key is present.

If you need to keep the order of insertions you can use something like LinkedHashMap or LinkedHashSet.

7 Comments

Going through the list is O(n). Then two customers are not necessarily because they have the same name. Also it does not really answer the OP question.
@JeanLogeart as I understood the question, the OP wants to check it before each insert while reading the data which will be in the end O(n^2) (should all customers be unique). And how does my answer not answer his question? I told him that with a list he needs to traverse the whole list or change the data structure. I did not write his code for him but that doesn't mean I did not answer his question.
@MateuszDymczyk Your approach is very good, but since I tried Jean's answer first and the list my program works with is very small I accepted his. I did do a upvote on your answer though.
@Arya no problem, not doing this for points :-) Have a look at Sets and Maps just for future reference, though! And maybe change that first ArrayList to a List if you don't explicitly need ArrayList methods :-)
Dumczyk, could you explain to me why your solution just takes constant time? yes you can access it this way, but then you reduce a comparison for the whole program to the full name... we do not know to much about the program, but especially in larger projects I would not suggest to do it like you said. does it make sense? hopefully...
|

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.