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.

2
  • put the List in HashSet to check for duplicated. Commented Nov 3, 2017 at 6:02
  • 2
    Put it into a Set . refer stackoverflow.com/questions/203984/… Commented Nov 3, 2017 at 6:03

5 Answers 5

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

The best answer. Thank you very much!
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);
    }

}

Comments

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.

Comments

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

2 Comments

The output should not contain C:\123\456\NameOfUniqueFolder0! Please, read the question carefully
What do you mean...0 th position value is comes twice time am i right..
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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.