如果元素在数组列表中重复,如何删除该元素的所有出现
How to remove all the occurences of a element if its duplicate in an arraylist
我有一个包含值的列表 {"16","b","c","d","e","16","f", "g","16","b"};
在这 16 和 b 中重复,所以我想删除它们的所有条目,我需要输出为 c、d、e、f、g。下面的程序工作正常。有更好的解决方案吗?
public class Test {
public static void main(String[] args) {
ArrayList < String > l = new ArrayList < String > ();
String[] str = {
"16",
"b",
"c",
"d",
"e",
"16",
"f",
"g",
"16",
"b"
};
for (String s: str) {
l.add(s);
}
List ll = removeDups(l);
l.removeAll(ll);
System.out.println("Final List " + l);
}
private static List < String > removeDups(ArrayList < String > l) {
List < String > ll = new ArrayList < String > ();
for (String a: l) {
int x = Collections.frequency(l, a);
if (x > 1) {
ll.add(a);
}
}
return ll;
}
}
您可以使用 Set 从给定数组列表中删除重复元素。
示例代码如下:
Set<String> myStrSet = new HashSet<String>();
Set<String> duplicateSet = new HashSet<String>();
for(String str : myArrayList){
if(myStrSet.contains(str)){
duplicateSet.add(str);
} else {
myStrSet.add(str);
}
}
for(String str : duplicateSet){
myStrSet.remove(str);
}
for(String str : myStrSet){
System.out.println("Print non-duplicate elements : " + str);
}
一种方法是使用流来查找每个元素的频率:
Map<String, Long> counts = yourList.stream()
.collect(Collectors.groupingBy(
Function.identity(), // keep the element as the key
Collectors.counting())); // values will be the count
然后,您可以使用 removeIf
根据条件删除元素,为此您将使用上面计算的频率图:
yourList.removeIf(elem -> counts.get(elem) > 1);
System.out.println(yourList); // [c, d, e, f, g]
另一种方法是首先找出哪些值有重复,哪些值是唯一的。为此,我们可以使用 Map<String, Boolean>
:
Map<String, Boolean> duplicates = new LinkedHashMap<>();
yourList.forEach(elem -> duplicates.compute(elem, (k, v) -> v != null));
我在这里迭代列表,对于每个元素,我将其放入映射中,如果该元素已经作为键存在,则将值计算为 true
,或者 false
如果它是唯一的。
然后,您可以在列表中使用 removeIf
,谓词只是 returns 地图中的值:
yourList.removeIf(duplicates::get);
System.out.println(yourList); // [c, d, e, f, g]
我想这样就可以了
public class DeleteDuplicates {
public static void main(String[] args) {
String[] str={"16","b","c","d","e","16","f","g","16","b"};
List<String> l= new ArrayList<String>();
Set<String> set = new HashSet<String>();
for(String string : str) {
if(set.add(string))
l.add(string);
else
l.remove(string);
}
System.out.println(l);
}
}
您可以比较每个元素的 index
和lastIndex
。如果它们是 same
,则该元素是 unique
。我们可以过滤那些元素。
// imports
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// sample code
String[] str = {"16","b","c","d","e","16","f","g","16","b"};
List<String> list = Arrays.asList(str); // List from the array
List<String> newList = new ArrayList<String>();
for(String myStr : list){
if(list.indexOf(myStr) == list.lastIndexOf(myStr)){
/*
* This is a unique element as its index and lastIndex in list are same.
* Add it to new list.
*/
newList.add(myStr);
}
}
// Freeing resources
str = null;
list = null;
System.out.println("Final List: "+ newList);
我有一个包含值的列表 {"16","b","c","d","e","16","f", "g","16","b"}; 在这 16 和 b 中重复,所以我想删除它们的所有条目,我需要输出为 c、d、e、f、g。下面的程序工作正常。有更好的解决方案吗?
public class Test {
public static void main(String[] args) {
ArrayList < String > l = new ArrayList < String > ();
String[] str = {
"16",
"b",
"c",
"d",
"e",
"16",
"f",
"g",
"16",
"b"
};
for (String s: str) {
l.add(s);
}
List ll = removeDups(l);
l.removeAll(ll);
System.out.println("Final List " + l);
}
private static List < String > removeDups(ArrayList < String > l) {
List < String > ll = new ArrayList < String > ();
for (String a: l) {
int x = Collections.frequency(l, a);
if (x > 1) {
ll.add(a);
}
}
return ll;
}
}
您可以使用 Set 从给定数组列表中删除重复元素。
示例代码如下:
Set<String> myStrSet = new HashSet<String>();
Set<String> duplicateSet = new HashSet<String>();
for(String str : myArrayList){
if(myStrSet.contains(str)){
duplicateSet.add(str);
} else {
myStrSet.add(str);
}
}
for(String str : duplicateSet){
myStrSet.remove(str);
}
for(String str : myStrSet){
System.out.println("Print non-duplicate elements : " + str);
}
一种方法是使用流来查找每个元素的频率:
Map<String, Long> counts = yourList.stream()
.collect(Collectors.groupingBy(
Function.identity(), // keep the element as the key
Collectors.counting())); // values will be the count
然后,您可以使用 removeIf
根据条件删除元素,为此您将使用上面计算的频率图:
yourList.removeIf(elem -> counts.get(elem) > 1);
System.out.println(yourList); // [c, d, e, f, g]
另一种方法是首先找出哪些值有重复,哪些值是唯一的。为此,我们可以使用 Map<String, Boolean>
:
Map<String, Boolean> duplicates = new LinkedHashMap<>();
yourList.forEach(elem -> duplicates.compute(elem, (k, v) -> v != null));
我在这里迭代列表,对于每个元素,我将其放入映射中,如果该元素已经作为键存在,则将值计算为 true
,或者 false
如果它是唯一的。
然后,您可以在列表中使用 removeIf
,谓词只是 returns 地图中的值:
yourList.removeIf(duplicates::get);
System.out.println(yourList); // [c, d, e, f, g]
我想这样就可以了
public class DeleteDuplicates {
public static void main(String[] args) {
String[] str={"16","b","c","d","e","16","f","g","16","b"};
List<String> l= new ArrayList<String>();
Set<String> set = new HashSet<String>();
for(String string : str) {
if(set.add(string))
l.add(string);
else
l.remove(string);
}
System.out.println(l);
}
}
您可以比较每个元素的 index
和lastIndex
。如果它们是 same
,则该元素是 unique
。我们可以过滤那些元素。
// imports
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// sample code
String[] str = {"16","b","c","d","e","16","f","g","16","b"};
List<String> list = Arrays.asList(str); // List from the array
List<String> newList = new ArrayList<String>();
for(String myStr : list){
if(list.indexOf(myStr) == list.lastIndexOf(myStr)){
/*
* This is a unique element as its index and lastIndex in list are same.
* Add it to new list.
*/
newList.add(myStr);
}
}
// Freeing resources
str = null;
list = null;
System.out.println("Final List: "+ newList);