1.将List转为Set,通过2个集合的size大小是否相等来判断有无重复元素
public static void main(String[] args) { List stringList = new ArrayList<>(Arrays.asList("a","a","b","c")); Set stringSet = new HashSet<>(stringList); if (stringList.size() == stringSet.size()) { System.out.println("没有重复元素"); } else { System.out.println("有重复元素"); } }
2.使用jdk8的Stream来判断
public static void main(String[] args) { List stringList = new ArrayList<>(Arrays.asList("a","a","b","c")); long count = stringList.stream().distinct().count(); if (stringList.size() == count) { System.out.println("没有重复元素"); } else { System.out.println("有重复元素"); }
标签:Java,重复,元素,List,System,stringList,size From: https://www.cnblogs.com/min225016/p/17477106.html