JAVA: Preventing Duplicate Entries to an ArrayList

与世无争的帅哥 提交于 2019-12-19 06:00:55

问题


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!


回答1:


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]



回答2:


If you want to keep the order of the lines read keep using the list but for the duplicates you can use a set for determining if a line (in its two forms as you described) was already added :

Set<String> duplicates = new HashSet<String>();
while((line = bufferedReader.readLine()) != null) {

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

     String lineReversed = tempnodelist[1] + delimiter + tempnodelist[0];

     if (!duplicates.contains(line) && !duplicates.contains(lineReversed )) {
         edges.add(line);
     }
}



回答3:


For each addition to the ArrayList you will have to iterate over all previous entries and check if duplicates entry exists(You can use .contains()) which is O(N).

Better I would suggest use a set.




回答4:


Firstly, use equals to compare strings.

Secondly, you can use Set rather than a List

And lastly, you can use contains method to check if the item already exists.




回答5:


ArrayList<String> ar=new ArrayList<String>();
String a[]={"cat","bat","cat","knife"};
for(int i=0;i<a.length;i++){
    if(!ar.contains(a[i])){
        ar.add(a[i]);
    }
}

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.

The elements in the array list for the above code would be

cat bat knife




回答6:


It sounds like what you really want is a Set<Set<String>>

Set<Set<String>> pairs = ...
try(BufferedReader br = ... ) {
    for(String line; (line = br.readLine()) != null;) 
        pairs.add(new HashSet<String>(Arrays.asList(line.split(" ")));
}

This creates a collection of pairs without duplicates regardless of the order of the words.



来源:https://stackoverflow.com/questions/20611751/java-preventing-duplicate-entries-to-an-arraylist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!