I know there's a lot of subject about "removing duplicates of a list". I liked the solution with HashSet
. However, what I have is an list of String[], and it won't work with it. Probably because stringArray1.equals(stringArray2)
will return false even if the two stringArray
are the same; to compare string Array, we have to use Arrays.equals, which is not the case with HashSet
.
So i have an userList of String[]
users with only 2 strings in it: username, and userID. Since both are linked (there's only one userID per username), it would be enough for me to compare only one of those strings.
What I need is a fast way to remove duplicates from the list.
I thought about something like this:
List<String> userNamesList = new ArrayList<String>();
List<String[]> userListWithoutDuplicates = new ArrayList<String[]>();
for(String[] user : userList){
if(!userNamesList.contains(user[0])){
userNamesList.add(user[0]);
userListWithoutDuplicates.add(user);
}
}
However, this need two new List and a loop (I'm pretty sure any other solution would need this loop, still).
I'm wondering if there's not a better solution. I thought something like that should already be implemented somewhere.
EDIT: I got my array from an sql query. In fact, i have a DB and some users. One user will search for others users responding to certain conditions in DB, DB send back a list of String[] {username, userID} to this user. So i already have an user class, which contains far more than only username and ID. I have one instance of this class per connected user, but the DB can't access those instances, so she can't send it. I thought a String array was an easy solution. I didn't thought that, in certain cases, an user can be referenced more than one time in DB and so selected more than one time. That's why i got duplicates in my list.