日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

优雅的找出ArrayList中重复的元素

發(fā)布時間:2023/12/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 优雅的找出ArrayList中重复的元素 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前些天發(fā)現(xiàn)了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。

方法1

代碼:

public class Main { public static void main(String[] args){ List<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("ccc"); list.add("ddd"); list.add("aaa"); list.add("aaaa"); list.add("eee"); list.add("bbb"); list.add("ccc"); StringBuilder builder = new StringBuilder(); for(String str : list) { // 如果不存在返回 -1。 if(builder.indexOf(","+str+",") > -1) { System.out.println("重復的有:"+str); } else { builder.append(",").append(str).append(","); } } } }

?

運行結果:

?

重復的有:aaa
重復的有:bbb
重復的有:ccc

方法2

代碼如下:

public class Main { public static void main(String[] args){ List<String> list=new ArrayList<String>(); list.add("string1"); list.add("string2"); list.add("string3"); list.add("string1"); list.add("string1"); list.add("string1"); list.add("string2"); //list.add("string3"); Map<String,Integer> hashMap=new HashMap<String, Integer>(); for(String string:list){ if(hashMap.get(string)!=null){ //hashMap包含遍歷list中的當前元素 Integer integer=hashMap.get(string); hashMap.put(string,integer+1); System.out.println("the element:"+string+" is repeat"); } else{ hashMap.put(string,1); } } } }

?

運行結果:

?

the element:string1 is repeat
the element:string1 is repeat
the element:string1 is repeat
the element:string2 is repeat

方法3

?因為我沒有用java8,所以idea不支持stream,所有此方法沒有驗證,暫時保存啦。

代碼如下:

public class Main { public static <E> List<E> getDuplicateElements(List<E> list) { return list.stream() // list 對應的 Stream .collect(Collectors.toMap(e -> e, e -> 1, (a, b) -> a + b)) // 獲得元素出現(xiàn)頻率的 Map,鍵為元素,值為元素出現(xiàn)的次數(shù) .entrySet().stream() // 所有 entry 對應的 Stream .filter(entry -> entry.getValue() > 1) // 過濾出元素出現(xiàn)次數(shù)大于 1 的 entry .map(entry -> entry.getKey()) // 獲得 entry 的鍵(重復元素)對應的 Stream .collect(Collectors.toList()); // 轉化為 List } public static void main(String[] args) throws Exception { List<String> list = Arrays.asList("a", "b", "c", "d", "a", "a", "d", "d"); List<String> duplicateElements = getDuplicateElements(list); System.out.println("list 中重復的元素:" + duplicateElements); } }

?

運行結果:

?

list 中重復的元素:[a,d]

?

方法4

public class Test { public static void main(String[] args) { List<String> strings = new ArrayList<>(); strings.add("1"); strings.add("2"); strings.add("2"); strings.add("3"); strings.add("3"); strings.add("3"); Set<String> set = new HashSet<>(); Set<String> exist = new HashSet<>(); for (String s : strings) { if (set.contains(s)) { exist.add(s); } else { set.add(s); } } System.out.println("重復的值:" + String.join(", ", exist)); } }

?

轉自:https://blog.csdn.net/Lin_88zq/article/details/72677040

?

https://blog.csdn.net/caoxiaohong1005/article/details/54286384

總結

以上是生活随笔為你收集整理的优雅的找出ArrayList中重复的元素的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。