Pure Java 8 solution with stream and lambda expression


  • 0
    J
    public List<List<String>> findDuplicate(String[] paths) {
            List<File> files = new LinkedList<>();
            for( int i = 0 ; i < paths.length ; i++){
                String str = paths[i];
                String[] strs = str.split(" ");
                for( int j = 1 ; j < strs.length ; j++){
                    files.add(new File(strs[0],strs[j]));
                }
            }
            Map<String,List<File>> group = files.stream().collect(Collectors.groupingByConcurrent(file->file.content));
            List<List<File>> duplicateFiles = group.values().stream().filter(list->list.size()>1).collect(Collectors.toList());
            return duplicateFiles.stream().map(files1 -> files1.stream().map(
                file -> file.directory+"/"+file.fileName).collect(Collectors.toList())).collect(Collectors.toList());
    
    
    
        }
    
    
        public static class File{
    
            String directory;
    
            String fileName;
    
            String content;
    
            public File(String directory, String fileAndContent){
                this.directory = directory;
                parse(fileAndContent);
            }
    
            private void parse(String fileAndContent){
                int index = fileAndContent.indexOf("(");
                this.fileName = fileAndContent.substring(0,index);
                this.content = fileAndContent.substring(index+1,fileAndContent.length() - 1);
            }
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.