Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA: Preventing Duplicate Entries to an ArrayList

Tags:

java

arraylist

I am trying to prevent duplicate entries from being added to an ArrayList as the list is being populated whilst reading through each line of a file. Each line of the file is in the format "node1 node2" (tab delimiter). A duplicate here could either be "node1 node2" or "node2 node1". Here is my code to try and perform this operation:

while((line = bufferedReader.readLine()) != null) {

     String delimiter = "\t";
     String[] tempnodelist;  
     tempnodelist = line.split(delimiter);

     for (int i=0; i <= edgesnumber; i++) {   //edgesnumber = edges.size()

         if (tempnodelist[0] && tempnodelist[1] != edges.get(i)) {

             edges.add(line);

            }
        }

     nodes.add(tempnodelist[0]);  
     nodes.add(tempnodelist[1]); //intial intended use of tempnodelist.

}

Since I'm already splitting each line to make a HashSet of each node, I'm trying to use this to check for duplicates. At the moment I just can't seem to get the syntax right. How can I check through previous entries of the ArrayList for duplicates, and prevent them from being added, whist continuing to populate the ArrayList? what is wrong with this code currently?

Please ask any questions if anything is unclear,

Thanks in advance!

like image 950
user2941526 Avatar asked Dec 16 '13 13:12

user2941526


People also ask

How to remove duplicates from an ArrayList in Java?

We can remove duplicates from an arraylist using the set method in Java. Set can’t contain any duplicate elements so it will store only the unique elements from the arraylist. Create an arraylist in Java and add some elements to it. Convert the arraylist into a set.

How to remove duplicate elements using an iterator in Java?

To remove duplicate elements using an iterator we can create another arraylist and then traverse through the arraylist and store the first occurrence of each element. Create an arraylist in Java and add some elements to it.

How to avoid duplicate entries in an array list in Python?

Create an array list, and check whether it contains the string to be inserted. If it does not contain the string, then you can add it into the array list. This way you can avoid the duplicate entries in the array list.

How to get the first appearance of each element in ArrayList?

Traverse through the first arraylist and store the first appearance of each element into the second arraylist using contains () method. The second ArrayList contains the elements with duplicates removed.


1 Answers

Use a LinkedHashSet and then convert it to an ArrayList, because a LinkedHashSet has a predictable iteration order (the insertion-order) and it is a Set.

For example

LinkedHashSet<String> uniqueStrings = new LinkedHashSet<String>();

uniqueStrings.add("A");
uniqueStrings.add("B");
uniqueStrings.add("B");
uniqueStrings.add("C");
uniqueStrings.add("A");

List<String> asList = new ArrayList<String>(uniqueStrings);
System.out.println(asList);

will output

 [A, B, C]
like image 70
René Link Avatar answered Nov 13 '22 17:11

René Link