Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4908c18

Browse files
authored
Update Find Duplicate File in System.java
1 parent 2babe9b commit 4908c18

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
class Solution {
22
public List<List<String>> findDuplicate(String[] paths) {
3-
Map<String, List<String>> map = new HashMap<>();
3+
Map<String, Set<String>> map = new HashMap<>();
44
for (String path : paths) {
5-
String[] strs = path.split("\\s+");
6-
String filePath = strs[0];
7-
for (int i = 1; i < strs.length; i++) {
8-
int startIdx = strs[i].indexOf('(');
9-
String fileName = strs[i].substring(0, startIdx);
10-
String content = strs[i].substring(startIdx, strs[i].length());
11-
map.computeIfAbsent(content, k -> new ArrayList<>()).add(filePath + "/" + fileName);
5+
String[] splitPath = path.split("\\s+");
6+
String directory = splitPath[0];
7+
for (int i = 1; i < splitPath.length; i++) {
8+
String fileName = splitPath[i].substring(0, splitPath[i].indexOf('('));
9+
String fileContent = splitPath[i]
10+
.substring(splitPath[i].indexOf('(') + 1, splitPath[i].indexOf(')'));
11+
map.computeIfAbsent(fileContent, k -> new HashSet<>()).add(directory + "/" + fileName);
1212
}
1313
}
14-
List<List<String>> duplicateFiles = new ArrayList<>();
15-
for (String key : map.keySet()) {
16-
if (map.get(key).size() > 1) {
17-
duplicateFiles.add(map.get(key));
18-
}
19-
}
20-
return duplicateFiles;
14+
return map.values().stream().filter(entry -> entry.size() > 1).map(ArrayList::new)
15+
.collect(Collectors.toList());
2116
}
2217
}

0 commit comments

Comments
 (0)