List/ArrayList 去重/計數

參考鏈接:
(1)arraylist,去重,計數重複數據出現次數https://blog.csdn.net/hfaflanf/article/details/101701195
(2)List去重並統計重複的數據https://blog.csdn.net/m0_38101105/article/details/84593649

(1)arraylist,去重,計數重複數據出現次數:HashSet、Stream
HashSet
1.HashSet不會存在相同的元素,可以利用這一點去除List中的重複元素,但是不保證數據的順序。

List<String> beforeList = new ArrayList<String>();       
beforeList.add("sun");
beforeList.add("star");
beforeList.add("moon");
Set<String> middleHashSet = new HashSet<String>(beforeList);
List<String> afterHashSetList = new ArrayList<String>(middleHashSet);

2.LinkedHashSet不會存在相同的元素,同時也可保證順序。

List<String> beforeList = new ArrayList<String>();
beforeList.add("sun");
beforeList.add("star");
beforeList.add("moon");
Set<String> middleLinkedHashSet = new LinkedHashSet<String>(beforeList);
List<String> afterHashSetList = new ArrayList<String>(middleLinkedHashSet);

Stream
在JDK1.8中 Stream中對List進行去重
list.stream().distinct();首先獲得此list的Stream.然後調用distinct()方法。
java8中提供流的方式對數據進行處理,非常快,底層用的是forkJoin框架,提供了並行處理,使得多個處理器同時處理流中的數據,所以耗時非常短。

List<String> list = Arrays.asList("AA", "BB", "CC", "BB", "CC", "AA", "AA","DD");
long l = list.stream().distinct().count();
System.out.println("唯一數據個數爲:"+l);
String output = list.stream().distinct().collect(Collectors.joining(","));
System.out.println(output);
list.stream().distinct().forEach(System.out::println);

//唯一數據個數爲:4
AA,BB,CC,DD
AA
BB
CC
DD

但是無法對實體類集合進行去重。

List<Test> list2 = new ArrayList<Test>();{
list2.add(new Test(1, "123"));
list2.add(new Test(2, "123"));
list2.add(new Test(3, "789"));
list2.add(new Test(4, "456"));
list2.add(new Test(5, "123"));
}
long l2 = list2.stream().distinct().count();
System.out.println("No. of distinct Test:"+l2);
list2.stream().distinct().forEach(b -> System.out.println(b.getId()+ "," + b.getName()));

//No. of distinct Test:5
1,123
2,123
3,789
4,456
5,123

(2)List去重並統計重複的數據
在寫博客項目中,有個功能是博客分類標籤和歸檔,這個當時我一直矛盾在存到數據庫的格式問題,先是把id存成字符串這樣好遍歷查看分欄或者歸檔的文章,但是這個有個缺陷,刪除博客和修改博客的時候巨麻煩,後來我試了另一種就是把博客id存成一個個類,修改博客和刪除博客方便,但是顯示時麻煩,魚和熊掌不可兼得,於是果斷採用第二種。在顯示上運用這種方式來獲取我所想要的數據。

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class CountDuplicatedList {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("b");
        list.add("c");
        list.add("a");
        list.add("a");
        list.add("a");
        System.out.println("\n例子1 - 計算'a'出現的次數");
        System.out.println("a : " + Collections.frequency(list, "a"));
        System.out.println("\n例子2 - 計算所有對象出現的次數");
        Set uniqueSet = new HashSet(list);
        for (String temp : uniqueSet) {
            System.out.println(temp + ": " + Collections.frequency(list, temp));
        }
        System.out.println("\n例子3 -用Map來計算對象出現的次數");
        Map map = new HashMap();
        for (String temp : list) {
            Integer count = map.get(temp);
            map.put(temp, (count == null) ? 1 : count + 1);
        }
        printMap(map);
        System.out.println("\nMap排序-以key排序");
        Map treeMap = new TreeMap(map);
        printMap(treeMap);
    }
    public static void printMap(Map map) {
        for (Map.Entry entry : map.entrySet()) {
            System.out.println("Key-value : " + entry.getKey() + "- "
                    + entry.getValue());
        }
    }
}

//例子
例子1 - 計算'a'出現的次數
a : 4
例子2 - 計算所有對象出現的次數
d: 1
b: 2
c: 2
a: 4
例子3 -用Map來計算對象出現的次數
Key-value : d- 1
Key-value : b- 2
Key-value : c- 2
Key-value : a- 4
Map排序-以key排序
Key-value : a- 4
Key-value : b- 2
Key-value : c- 2
Key-value : d- 1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章