-1

I got an ArrayList of users who can login to web site multiple times per day. How can I create an ArrayList of unique users out of the first arraylist. In other words I need to ignore multiple logins. If for example there were 20 logins to the web site and all those logins were made by two users then the second list should contain just two user objects. I am newbie to Java programming language and I need help.

Here is the User class:

public class User {

    private String id
    private String date;

    public String getId() {
        return id
    }

    public void setId(String id) {
        this.id = id
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}

And here is how I was going to solve the issue but for some reason it doesn't work.

ArrayList<User> userList = new ArrayList<>();
ArrayList<User> uniqueUserlist = new ArrayList<>();

    String id = null;

    for(User user : userList) {

        if (user.getId() != id) {
            uniqueUserlist.add(user);
        }

        id = user.getId();
    }

Thank you.

3
  • You have not init data for your userList before using in the loop Commented Aug 3, 2017 at 22:16
  • 2
    == and != in Java compare reference equality, not data equality. If you want to check if the ids are equal, use user.getId().equals(id). Commented Aug 3, 2017 at 22:16
  • If you modify your User class to give it sensible equals and hashCode methods (and toString for completeness), then you can use the solution in "Get unique values from arraylist in java". Commented Aug 3, 2017 at 22:53

3 Answers 3

3

While your bug is simply not comparing Strings correctly, there's a far better way to do this:

  • Add a hashCode and equals to your User class
  • Place these items into a LinkedHashSet

What you accomplish:

  • Defining hashCode allows you to use data structures which require a hash value, like Set.
  • LinkedHashSet preserves insertion order and eliminates duplicates at the cost of a little extra memory.
Sign up to request clarification or add additional context in comments.

3 Comments

What is the hashCode is not int? What is the hashCode is string?
@DanielFoo: hashCode is always int. You are calculating a value. If you're not too sure, then you can use 31 * id.hashCode() + 31 * date.hashCode(), although be aware that I did this off the top of my head and will not vouch for its reliability whatsoever. That I leave as an exercise for the reader.
In my case the user id might have letters. How do I override hashcode and equals if I am going to compare users by string id.
1

If you don't want duplicates but want to use an ArrayList a quick fix would be to move all the items into a HashSet (which does not allow duplicates), clear the ArrayList and then add the items from the HashSet back into the ArrayList.

2 Comments

The hashcode is not int it is a String?
List<User> result = new ArrayList<User>(); Set<String> ids= new HashSet<String>(); for( User user : originalList ) { if( ids.add( user.getId() ) { result.add( user); } }
-1

You can make use of Stream API....put your list in the TreeSet from which you provide a custom comparator that compares id uniquely.

Something like the below would serve your need.

ArrayList<User> uniqueUserlist = userList.stream()
                            .collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingInt(User::getId))), ArrayList::new));

1 Comment

That'd require that the elements be Comparable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.