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
可以使用HashSet或LinkedHashSet来对ArrayList进行去重操作。HashSet是无序的,而LinkedHashSet是有序的。下面是两种方法的示例代码: 方法一:使用HashSet去重 ```java List<Integer> arrayList = new ArrayList<Integer>(){{add(3);add(2);add(4);add(2);add(3);add(5);add(4);add(5);}}; ArrayList<Integer> uniqueList = new ArrayList<>(new HashSet<>(arrayList)); ``` 在这个方法中,我们首先创建一个HashSet对象,将ArrayList的元素添加到HashSet中,由于HashSet的特性是不允许重复元素的,所以重复的元素会被自动去除。然后我们再将HashSet转换回ArrayList,得到去重后的结果。 方法二:使用LinkedHashSet去重 ```java List<Integer> arrayList = new ArrayList<Integer>(){{add(3);add(2);add(4);add(2);add(3);add(5);add(4);add(5);}}; ArrayList<Integer> uniqueList = new ArrayList<>(new LinkedHashSet<>(arrayList)); ``` 在这个方法中,我们使用LinkedHashSet代替HashSet,LinkedHashSet会保持元素的插入顺序,所以去重后的结果会保持原来的顺序。 无论是使用HashSet还是LinkedHashSet,都可以实现对ArrayList去重操作。具体选择哪种方法取决于你对元素顺序的要求。 #### 引用[.reference_title] - *1* *3* [【Java】为ArrayList去重](https://blog.csdn.net/yongh701/article/details/43266695)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [ArrayList去重常用的四种方式及性能对比(JMH性能分析)](https://blog.csdn.net/yeahPeng11/article/details/122904072)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值