0

I checked many examples but i could not applied for my variables. I have a ArratyList Of lists of Strings.

ArrayList<List<String>> bulkUploadList = new ArrayList<List<String>>();

and it's look like this:

[id, title, tags, descriptions]

[4291483113.0000000000000, Camden, camdentown;london, NoValue]
[4292220054.0000000000000, IMG_2720, NoValue, NoValue]
[4292223824.0000000000000, IMG_2917, london;camdentown, NoValue]
[4292224728.0000000000000, IMG_2945, London;CamdenTown, NoValue]

I want to remove those rows which have the same titles and the same tags. I do not know how work with HashSet since I have a ArrayList of List of Strings.

7
  • 4
    Why you have an arraylist of strings in the first place ? Create a class to hold this datas. Commented Dec 10, 2015 at 12:50
  • Would you please explain more, what do you mean by holding them in a class. It's the result of a selection query from DB and I am going to remove duplicates and store in another table in DB Commented Dec 10, 2015 at 12:57
  • 1
    Why are you doing duplication removal in Java? You have an RDBMS engine: use it! Commented Dec 10, 2015 at 12:59
  • 1-I am not professional i. 2- the order is important for me and based on which items are similar and based on which order I do different things as well and make a relationship tables for them.3-in case, there is a way, so I do not know how to do it! Commented Dec 10, 2015 at 13:01
  • You can remove duplicates on database by using 'distinct', 'group by' or something else Commented Dec 10, 2015 at 13:02

2 Answers 2

1

Not best solution, but you can start with this:

    ArrayList<List<String>> bulkUploadList = new ArrayList<List<String>>();
    ArrayList<List<String>> result = new ArrayList<List<String>>();

    HashSet<String> hashSet = new HashSet<>();

    for(List<String> item : bulkUploadList) {
        String title = item.get(1);
        String tags = item.get(2);
        String uniqueString = (title + "#" + tags).trim().toUpperCase();

        if(!hashSet.contains(uniqueString)) {
            result.add(item);
            hashSet.add(uniqueString);
        } else {
            System.out.println("Filtered element " + uniqueString);
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I added conversion to upper case and trim operation, thanks. We can also trim each of parameters separately
@EldarBudagov hi again. I have another question. I could removed those rows which have the same titles and tags and kept just one of those. How I can find which id in kept list corresponding which removed one?
Use HashMap instead of HashSet to retain binding between id and data
0

As suggested in one of the comments, you should create a class for the data, make that class implement equals(), and then use HashSet to remove dups. Like this.

class Foo {
String id;
String title;
String tags;
String description;

public boolean equals(Foo this, Foo other) {
   return this.id.equals(other.id) 
      && this.title.equals(other.title)
      && etc.
}

then you can remove dups with

 Set<Foo> set = new LinkedHashSet<Foo>(list);

as Sets do not allow duplication, and the equals() method is used to check.

You should use a linkedHashSet here because you want to preserve the order (according to a comment you made on elsewhere).

You should also implement a hashcode() method consistent with equals().

1 Comment

thanks for writing that I am going to use it as well to learn how to use it :)

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.