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
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]
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.
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);
}
}
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.
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.
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