文章目录
- 记录一下集合去重的几种方法
- stream去重,推荐,使用java8的stream api的distinct方法去重
- LinkedHashSet去重,推荐,返回的数据顺序不变。可以使用hashset,但是不保证顺序。可以根据要求来。
- Collection的的contains方法进行判断。可以使用list(有序)也可以使用set(无序)。
- 双重for删除,不方便看。
- main代码:
记录一下集合去重的几种方法
stream去重,推荐,使用java8的stream api的distinct方法去重
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","a","b","b","b","c"));
System.out.println(JSONObject.toJSONString(list));
repeatRemovetest1(list);
}
public static void repeatRemovetest1(List<String> list) {
List<String> listWithoutDuplicates = list.stream().distinct().collect(Collectors.toList());
System.out.println(listWithoutDuplicates);
}
LinkedHashSet去重,推荐,返回的数据顺序不变。可以使用hashset,但是不保证顺序。可以根据要求来。
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","a","b","b","b","c"));
System.out.println(JSONObject.toJSONString(list));
repeatRemovetest2(list);
}
public static void repeatRemovetest2(List<String> list) {
LinkedHashSet<String> linkSet = new LinkedHashSet<>(list);
ArrayList<String> listWithoutDuplicates = new ArrayList<>(linkSet);
System.out.println(listWithoutDuplicates);
}
Collection的的contains方法进行判断。可以使用list(有序)也可以使用set(无序)。
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","a","b","b","b","c"));
System.out.println(JSONObject.toJSONString(list));
repeatRemovetest3(list);
}
public static void repeatRemovetest3(List<String> list) {
HashSet<Object> sets = new HashSet<>();
ArrayList<String> temList = new ArrayList<>(list.size());
for (String s : list) {
if (!temList.contains(s)) {
temList.add(s);
}
}
System.out.println(temList);
}
双重for删除,不方便看。
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "a", "b", "b", "b", "c"));
System.out.println(JSONObject.toJSONString(list));
repeatRemovetest4(list);
}
public static void repeatRemovetest4(List<String> list) {
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size(); j++) {
if (i != j && list.get(i) == list.get(j)) {
list.remove(list.get(j));
}
}
}
System.out.println(list);
}
main代码:
import com.alibaba.fastjson.JSONObject;
import java.util.*;
import java.util.stream.Collectors;
public class RepeatRemoveTest {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "a", "b", "b", "b", "c"));
System.out.println(JSONObject.toJSONString(list));
repeatRemovetest4(list);
}
public static void repeatRemovetest1(List<String> list) {
List<String> listWithoutDuplicates = list.stream().distinct().collect(Collectors.toList());
System.out.println(listWithoutDuplicates);
}
public static void repeatRemovetest2(List<String> list) {
LinkedHashSet<String> linkSet = new LinkedHashSet<>(list);
ArrayList<String> listWithoutDuplicates = new ArrayList<>(linkSet);
System.out.println(listWithoutDuplicates);
}
public static void repeatRemovetest3(List<String> list) {
HashSet<Object> objects = new HashSet<>();
ArrayList<String> temList = new ArrayList<>(list.size());
for (String s : list) {
if (!temList.contains(s)) {
temList.add(s);
}
}
System.out.println(temList);
}
public static void repeatRemovetest4(List<String> list) {
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size(); j++) {
if (i != j && list.get(i) == list.get(j)) {
list.remove(list.get(j));
}
}
}
System.out.println(list);
}
}