list去重
返回list
List<String> collect = list.stream().distinct().collect(Collectors.toList());
或
List<String> collect = list.stream().distinct().collect(toList());
toList() import static java.util.stream.Collectors.toList;
返回String字符串
String collect = list.stream().distinct().collect(Collectors.joining(","));
获取两个list不同元素
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class TestList {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
List<String> list2=new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list2.add("8");
list2.add("1");
Collection exists=new ArrayList(list2);
exists.removeAll(list);
Object[] array = exists.toArray();
List<Object> asList = Arrays.asList(array);
System.out.println(asList);
System.out.println(exists);
}
}
输出结果如下:
总结如下:
Collection exists=new ArrayList(list2);里面是哪个list,获取到的就是那个list中的不同元素