参考链接:
(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