Java 如何从arraylist中删除重复项
我正在尝试从arraylist中删除重复项:Java 如何从arraylist中删除重复项,java,java-stream,Java,Java Stream,我正在尝试从arraylist中删除重复项: public static List<List<String>> bigList = new ArrayList<>(); for (int i = 0; i < bigList.size(); i++) { bigList.get(i).stream() .map(str -> new LinkedHashSet<>(Arrays.asLis
public static List<List<String>> bigList = new ArrayList<>();
for (int i = 0; i < bigList.size(); i++) {
bigList.get(i).stream()
.map(str -> new LinkedHashSet<>(Arrays.asList(str.split(","))))
.distinct()
.map(set -> set.stream().collect(Collectors.joining(",")))
.collect(Collectors.toList());
}
}
publicstaticlist bigList=newarraylist();
对于(int i=0;inewlinkedhashset(Arrays.asList(str.split(“,”)))
.distinct()
.map(set->set.stream().collect(收集器.连接(“,”))
.collect(Collectors.toList());
}
}
在执行代码时,我的列表中仍然有重复的代码。
我想删除bigList和bigList.get(I)中的重复项。捕获新列表
代码每次通过for
循环创建一个新列表。但是新的列表马上就丢失了,走向垃圾收集。您忽略了捕获对新创建列表的引用
因此,解决方案是:用新列表替换存储在外部列表中作为元素的旧列表。用bigList.set(i,…)
包装流行
publicstaticlist>bigList=newarraylist();//字符串列表的列表。
…
对于(int i=0;inewlinkedhashset(Arrays.asList(str.split(“,”)))
.distinct()
.map(set->set.stream().collect(收集器.连接(“,”))
.collect(Collectors.toList())//生成新列表。存储在旧列表的位置。
) ;
}
为清晰起见,请将代码分成几行
public static List< List< String > > bigList = new ArrayList<>(); // A list of lists of strings.
…
for (int i = 0; i < bigList.size(); i++) {
List< String > oldList = bigList.get( i ) ;
List< String > newList =
oldList
.stream()
.map( str -> new LinkedHashSet<>( Arrays.asList( str.split( "," ) ) ) )
.distinct()
.map( set -> set.stream().collect( Collectors.joining( "," ) ) )
.collect( Collectors.toList() ) // Produce a new list. Stored in place of the old list.
) ;
bigList.set( i , newList ) ; // Replace the old list with the newly-created list.
}
publicstaticlist>bigList=newarraylist();//字符串列表的列表。
…
对于(int i=0;ioldList=bigList.get(i);
列表newList=
旧名单
.stream()
.map(str->newlinkedhashset(Arrays.asList(str.split(“,”)))
.distinct()
.map(set->set.stream().collect(收集器.连接(“,”))
.collect(Collectors.toList())//生成新列表。存储在旧列表的位置。
) ;
set(i,newList);//用新创建的列表替换旧列表。
}
捕获新列表
代码每次通过for
循环创建一个新列表。但是新的列表马上就丢失了,走向垃圾收集。您忽略了捕获对新创建列表的引用
因此,解决方案是:用新列表替换存储在外部列表中作为元素的旧列表。用bigList.set(i,…)
包装流行
publicstaticlist>bigList=newarraylist();//字符串列表的列表。
…
对于(int i=0;inewlinkedhashset(Arrays.asList(str.split(“,”)))
.distinct()
.map(set->set.stream().collect(收集器.连接(“,”))
.collect(Collectors.toList())//生成新列表。存储在旧列表的位置。
) ;
}
为清晰起见,请将代码分成几行
public static List< List< String > > bigList = new ArrayList<>(); // A list of lists of strings.
…
for (int i = 0; i < bigList.size(); i++) {
List< String > oldList = bigList.get( i ) ;
List< String > newList =
oldList
.stream()
.map( str -> new LinkedHashSet<>( Arrays.asList( str.split( "," ) ) ) )
.distinct()
.map( set -> set.stream().collect( Collectors.joining( "," ) ) )
.collect( Collectors.toList() ) // Produce a new list. Stored in place of the old list.
) ;
bigList.set( i , newList ) ; // Replace the old list with the newly-created list.
}
publicstaticlist>bigList=newarraylist();//字符串列表的列表。
…
对于(int i=0;ioldList=bigList.get(i);
列表newList=
旧名单
.stream()
.map(str->newlinkedhashset(Arrays.asList(str.split(“,”)))
.distinct()
.map(set->set.stream().collect(收集器.连接(“,”))
.collect(Collectors.toList())//生成新列表。存储在旧列表的位置。
) ;
set(i,newList);//用新创建的列表替换旧列表。
}
您没有将.collect(Collectors.toList())
的结果分配给任何对象。因此,您正在构建一个新列表,它会立即被丢弃。@l请用bigList来包装您的行。设置(i,…)
以用新列表替换旧列表。您发布的代码中与双精度无关。您的for
循环根本不会运行;您执行bigList=new ArrayList()
并在i
上循环-好吧,bigList.size()为零,因此不会有迭代。@BasilBourque不是循环并调用get
,然后调用set
,而是使用bigList.replaceAll(list->list.stream()…collect>更简单(Collectors.toList());
或者,OP可以真正从列表中删除重复项,而不是创建新列表。您没有将.collector(Collectors.toList())
的结果分配给任何对象。因此,您正在构建一个新列表,该列表会立即被丢弃。@l请使用bigList.set将您的行换行(i,…)
以新列表替换旧列表。您发布的代码中没有任何内容与双精度相关。您的for
循环将根本不会运行;您执行bigList=new ArrayList()
并循环i
-嗯,bigList.size()是零,因此不会有迭代。@BasilBourque没有循环并调用get
,然后调用set
,而是使用bigList.replaceAll(list->list.stream()…collect(Collectors.toList())更简单
或者,OP可以真正从列表中删除重复项,而不是创建新列表。