0

Suppose I have an ArrayList, which has paths to the specific file to be processed. But this file can only be proccessed, if there's only one in a folder. Here's what I mean:

My ArrayList

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

has

C:\123\456\NameOfUniqueFolder0
C:\123\456\NameOfUniqueFolder1
C:\123\456\NameOfUniqueFolder2
C:\123\456\NameOfUniqueFolder3
C:\123\456\NameOfUniqueFolder4

Suppose my 5th element is

C:\123\456\NameOfUniqueFolder0

Obviosuly, it's a duplicate of my 0 element, since the file inside this folder SHOULD NOT be proccessed at all. C:\123\456\NameOfUniqueFolder0 should be removed form the list. I can not use a SET here, because it will "remove" the duplicate, but one path to the folder will be still there, and the file inside get proccessed.

5 Answers5

2

Using a hashmap here might make sense. The keys could be the file paths, and the value, if the key be present, would be the number of times the file occurs. Something like this:

Map<String, Integer> map = new HashMap<>();
map.put("C:\123\456\NameOfUniqueFolder0", 1);
map.put("C:\123\456\NameOfUniqueFolder1", 1);
map.put("C:\123\456\NameOfUniqueFolder2", 1);
map.put("C:\123\456\NameOfUniqueFolder3", 1);
map.put("C:\123\456\NameOfUniqueFolder4", 1);

Now when a new path comes along, increment its counter:

String newPath = "C:\123\456\NameOfUniqueFolder0";
Integer val = map.get(newPath);
    map.put(newPath, val == null ? 1 : val.intValue() + 1);
}

At the end, you can iterate this map, and check the counter values for each key. You would then only process the files having occurred only once:

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    int count = entry.getValue();
    if (count == 1) {
        // process this file
    }
    // otherwise skip this path
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
2

You can do something like this:

public class RemoveDuplicatesFromList {

    public static void main(String[] args) {
            List<String> originalList = new ArrayList<String>();
            //add your strings to list here
            Set<String> duplicateValues = new HashSet<String>();
            for(String str:originalList){
                //if firstIndex != lastIndex duplicate is present
                if(originalList.indexOf(str)!=originalList.lastIndexOf(str))
                    duplicateValues.add(str);

            }
            //remove duplicates from original list
            originalList.removeAll(duplicateValues);
            System.out.println(originalList);
    }

}
NAK
  • 478
  • 2
  • 9
0

You can do something like below. it will give you map of path with it's number of occurrence. eg: {C:\123\456\NameOfUniqueFolder0=2, C:\123\456\NameOfUniqueFolder1=1}

Map<String, Long> collect = pathsToTheFile.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

After this you can process only path having occurrence equal to 1.

hiren
  • 1,742
  • 13
  • 20
0

you can use only HashSet for this if you want list in sorted order the use TreeSet

HashSet<String> set=new HashSet<String>();  
  set.add("C:\123\456\NameOfUniqueFolder0");  
  set.add("C:\123\456\NameOfUniqueFolder1");  
  set.add("C:\123\456\NameOfUniqueFolder2");  
  set.add("C:\123\456\NameOfUniqueFolder3");  
  set.add("C:\123\456\NameOfUniqueFolder4");  
  set.add("C:\123\456\NameOfUniqueFolder0");  
  //Traversing elements  
  Iterator<String> itr=set.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  

your output will be

C:\123\456\NameOfUniqueFolder0

C:\123\456\NameOfUniqueFolder1

C:\123\456\NameOfUniqueFolder2

C:\123\456\NameOfUniqueFolder3

C:\123\456\NameOfUniqueFolder4

Dilip
  • 2,622
  • 1
  • 20
  • 27
0

You can try this code

List<String> pathsToTheFile = new ArrayList<String>();
pathsToTheFile.add("C:/123/456/NameOfUniqueFolder0");
pathsToTheFile.add("C:/123/456/NameOfUniqueFolder1");
pathsToTheFile.add("C:/123/456/NameOfUniqueFolder2");
pathsToTheFile.add("C:/123/456/NameOfUniqueFolder0");
pathsToTheFile.add("C:/123/456/NameOfUniqueFolder3");
pathsToTheFile.add("C:/123/456/NameOfUniqueFolder4");
pathsToTheFile.add("C:/123/456/NameOfUniqueFolder0");
pathsToTheFile.add("C:/123/456/NameOfUniqueFolder0");

String newPathToBeAdded = "C:/123/456/NameOfUniqueFolder0";

while(pathsToTheFile.contains(newPathToBeAdded)) {  // the new path to be added
    pathsToTheFile.remove(newPathToBeAdded);
}
System.out.println(pathsToTheFile);
Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52