fzy-blog

java List 集合常用工具类

2019-05-24

java List 集合常用工具类

比较一个 list 集合里是否有重复

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 比较一个list集合里是否有重复
* */
public static boolean isRepeat(List<Object> list) {
Set<Object> set = new HashSet<>(list);
return set.size() != list.size();
}
比较两个集合是否有重复
```java
/**
* 比较两个集合是否有重复
* */
public static boolean isRepeat(List<Object> list1, List<Object> list2){
Set<Object> set1 = new HashSet<>(list1);
Set<Object> set2 = new HashSet<>(list2);
Set<Object> setAll = new HashSet<>(set1);
setAll.addAll(set2);
int setSize = set1.size() + set2.size();
return setAll.size() != setSize;
}

单个 List 集合去除重复

1
2
3
4
5
6
7
/**
* 单个List集合去除重复
* */
public static List<Object> removeRepeat(List<Object> list) {
Set<Object> set = new HashSet<>(list);
return new ArrayList<>(set);
}

单个 List 集合去除重复 (根据对象中的属性进行去除重复)

1
2
3
4
5
6
7
8
9
private Comparator<Role> roleComparator = new Comparator<Role>() {
public int compare(Role r1, Role r2) {
return r1.getCode().compareTo(r2.getCode());
}
};

//调用方法:
Set<Role> roles = new TreeSet<>(this.roleComparator);
roles.addAll(roleList);

比较两个集合是否有重复(有相同移除第一个集合中的相同值)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* 比较两个集合是否有重复(有相同移除第一个集合中的相同值)
* */
//一般法
public static List<Object> removeRepeat(List<Object> list1, List<Object> list2) {
List<Object> newList = new ArrayList<Object>();
for (Object o1 : list1) {
boolean flag = true;
for (Object o2 : list2) {
if (o1.equals(o2)){
flag = false;
break;
}
}
if (flag){
newList.add(o1);
}
}
return newList;
}
//方法二:
public static List<Object> removeRepeat2(List<Object> list1, List<Object> list2) {
list1.removeAll(list2);
return list1;
}

比较两个 list 集合:childList 中 包含 parentList 的 map 中的 key 值,如果相等重新放到一个 map<String,List<Map<String,String>>里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 比较两个list集合中map:childList 中 包含 parentList的 map 中的 key值,如果相等重新放到一个map<String,List<Map<String,String>>里
*
* */
public static Map<String, List<Map<String, String>>> getMapsByKeyId(List<Map<String, String>> parentList,
List<Map<String, String>> childList, String keyId) {
Map<String, List<Map<String, String>>> newMap = new HashMap<>();
for (Map<String, String> parent : parentList) {
String value = parent.get(keyId);
List<Map<String, String>> newList = new ArrayList<>();
for (Map<String, String> child : childList) {
if (child.get(keyId).equals(value))
newList.add(child);
}
newMap.put(value, newList);
}
return newMap;
}
//调用方式:
Map<String, List<Map<String,String>>> mapNew2 = getMapsByKeyId(list1,list2,"aid");

Map<String, List<Map<String,String>>> mapNew3 = getMapsByKeyId(list2,list3,"bid");

求两个集合的交集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 求两个集合的交集(即两个集合都共有的)
* */
//最优法 利用hash这种很有用的数据结构来实现。我们知道,hash的特点之一就是不允许有重复元素,即hash表中的元素都是唯一的。所以,我们的思路就是:先把第一个集合的所有元素都放进hashSet中,时间复杂度O(M);再把第二个集合中的元素放进hashSet中,如果有重复元素,就是这2个集合的交集,时间复杂度为O(N)。即总的时间复杂度从O(M*N)降低到了O(M+N)。
public static List<String> getIntersection(List<String> list1, List<String> list2) {
List<String> commonList = new ArrayList<>();
Set<Object> hashSet = new HashSet<>();
Collections.addAll(hashSet,list1);

for (String item : list2) {
if (!hashSet.add(item)) {
commonList.add(item);
}
}
return commonList;
}

求两个集合的并集(即两个集合中重复的只保留一个)

1
2
3
4
5
6
7
8
/**
* 求两个集合的并集(即两个集合中重复的只保留一个)
* */
public static List<Object> unionAll(List<Object> list1, List<Object> list2) {
list1.removeAll(list2);
list1.addAll(list2);
return list1;
}

计算集合中元素重复次数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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());
}
}
使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏

扫描二维码,分享此文章