java求两个集合的交集和并集,比较器

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

java求两个集合的交集和并集,⽐较器求连个集合的交集:
import java.util.ArrayList;
import java.util.List;
public class TestCollection {
public static void main(String[] args) {
List<String> strList = new ArrayList<String>();
List<String> strList2 = new ArrayList<String>();
for(int i = 0; i < 10; i ++) {
strList.add("aaa>>" + i);
strList2.add("aaa>>" + (10 - i));
}
//求出交集
strList2.retainAll(strList);
System.out.println("交集⼤⼩:" + strList2.size());
for(int i = 0; i < strList2.size(); i++) {
System.out.println(strList2.get(i));
}
}
}
求两个集合的并集:
import java.util.ArrayList;
import java.util.List;
public class TestCollection {
public static void main(String[] args) {
List<String> strList = new ArrayList<String>();
List<String> strList2 = new ArrayList<String>();
for(int i = 0; i < 10; i ++) {
strList.add("aaa>>" + i);
strList2.add("aaa>>" + (10 - i));
}
//求出并集
strList2.removeAll(strList);
strList2.addAll(strList);
System.out.println("并集⼤⼩:" + strList2.size());
for(int i = 0; i < strList2.size(); i++) {
System.out.println(strList2.get(i));
}
}
}
3.差集:由属于A⼜不属于B的元素组成的叫差集
list1.remove(list2);
4.去重并排序
package twolist;
import java.util.Collections;
import parator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ListMapSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO ⾃动⽣成⽅法存根
List<Map<String,Object>> listMap1 = new LinkedList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String, Object>();
map.put("date", 20121010);
listMap1.add(map);
map = new HashMap<String, Object>();
map.put("date", 20011213);
listMap1.add(map);
listMap1.add(map);
map = new HashMap<String, Object>();
map.put("date", 20130502);
listMap1.add(map);
System.out.println("原始"+listMap1);
List<Map<String,Object>> listMap2 = new LinkedList<Map<String,Object>>();
Set<Map> setMap = new HashSet<Map>();
for(Map<String,Object> map1 : listMap1){
if(setMap.add(map1)){
listMap2.add(map1);
}
}
System.out.println("去重"+listMap2);
Collections.sort(listMap2, new Comparator<Map<String,Object>>(){
public int compare(Map<String,Object> o1,Map<String,Object> o2){
return o1.get("date").toString().compareTo(o2.get("date").toString());
}
});
System.out.println("排序:"+listMap2);
}
}
通常对象之间的⽐较可以从两个⽅⾯去看:
第⼀个⽅⾯:对象的地址是否⼀样,也就是是否引⽤⾃同⼀个对象。

这种⽅式可以直接使⽤“==“来完成。

第⼆个⽅⾯:以对象的某⼀个属性的⾓度去⽐较。

对于JDK8⽽⾔,有三种实现对象⽐较的⽅法:
1、覆写Object类的equals()⽅法;
2、继承Comparable接⼝,并实现compareTo()⽅法;
3、定义⼀个单独的对象⽐较器,继承⾃Comparator接⼝,实现compare()⽅法。

由于使⽤的排序⽅式的不同,具体选择哪种⽅法来实现对象的⽐较也会有所不同。

Comparable和Comparator接⼝都是为了对类进⾏⽐较,众所周知,诸如Integer,double等基本数据类型,java可以对他们进⾏⽐较,⽽对于类的⽐较,需要⼈⼯定义⽐较⽤到的字段⽐较逻辑。

可以把Comparable理解为内部⽐较器,⽽Comparator是外部⽐较器,基本的写法如下:
class Apple implements Comparable<Apple>{
int id;
double price;
public Apple(int id, double price) {
this.id = id;
this.price = price;
}
public int compareTo(Apple o) {
//return pare(this.getPrice(),o.getPrice());
if (Math.abs(this.price-o.price)<0.001)
return 0;
else
return (o.price-this.price)>0?1:-1;
}
@Override
public String toString() {
return "Apple{" +
"id=" + id +
", price=" + price +
'}';
}
}
class AESComparator implements Comparator<Apple>{
public int compare(Apple o1, Apple o2) {
if (Math.abs(o1.price-o2.price)<0.001)
return 0;
else{
return (o1.price-o2.price)>0?1:-1;
}
}
}
实现了Comparable接⼝的类需要实现compareTo()⽅法,传⼊⼀个外部参数进⾏⽐对,实现了Comparator接⼝的⽅法需要实现compare()⽅法,对外部传⼊的两个类进⾏⽐较,从⽽让外部⽅法在⽐较时调⽤。

先了解⼀下Arrays和Collections.sort(list, new MyComparator());//传⼊list和⽐较器排序
了解⽐较器之前⾸先来了解⼀下java.util包下的Arrays类。

这个类主要提供了各种操作数组的⽅法。

最常⽤的⼏个⽅法:
Arrays.toString(T[] data) //将数组以字符串的形式返回
Arrays.sort(T[] data)//将数组按指定的⽐较规则以升序的顺序排序,T类需要实现Comparable接⼝
Arrays.sort(T[],Comparator<? super T>)//将数组按指定的⽐较器以升序的顺序排序
Arrays.fill(T[] data,T val)//数组的每⼀个元素赋值为val
⽐较器之Comparable<T>接⼝
实现对象之间的⽐较有两个⽅法,第⼀个⽅法就是实现Compatable接⼝。

Comparable接⼝是ng包下的。

该接⼝只有⼀个⽅法:
int compareTo(T o)//返回三种情况:负整数、0、正整数。

分别表⽰⼩于、等于、⼤于
当我们想要⽐较两个对象的⼤⼩时,由于栈中存的是对象的地址,所以⽆法⽐较。

于是我们需要实现Comparable接⼝,并且重写compareTo⽅法。

其实,String类就是实现了Comparable接⼝,才有了compareTo()的⽅法。

⽐较器之Comparator<T>接⼝
实现对象的⽐较第⼆个⽅法是额外写⼀个⽐较⼯具类,该⼯具类需要实现Comparator接⼝。

既然有了Comparable接⼝可以实现⽐较,为什么还要有Comparator接⼝呢?因为Comparable接⼝在类定义的时候就要实现好Comparable的compareTo()⽅法。

假设我已经写好了City 类,不想再改变改类的内部结构,这时我们就可以通过再写⼀个⼯具类来实现City类的⽐较。

另外,通过多个⼯具类可以实现多种不同的⽐较⽅法。

public class A{
public xxxx fun(){
//业务逻辑
//xxxxxxxxxxxxxxxxxxx
//排序
Collections.sort(myList, new MyComparator(configValueList));
}
/**
*内部类实现排序
*configValueList 排序规则
*根据DtoList中的某⼀个字段,按照configValueList配置的规则来排序
*如configValueList ["a","b","c"]
*myList myList.get[0].getVal() = b,myList.get[1].getVal() = a,myList.get[2].getVal() = c
*那么排序后 myList.get[0].getVal() = a,myList.get[1].getVal() = b,myList.get[2].getVal() = c
*/
class MyComparator implements Comparator<Dto> {
private List<String> configValueList;
public MyComparator(List<String> configValueList) {
this.configValueList = configValueList;
}
@Override
public int compare(Dto dto1, Dto dto2) {
if(CollectionUtils.isEmpty(configValueList) || dto1 == null || dto2 == null){
return 0;
}
String val1 = dto1.getVal();
String val2 = dto2.getVal();
if(StringUtils.isBlank(val1) || StringUtils.isBlank(val2)){
return 0;
}
int sort1 = configValueList.indexOf(val1);
int sort2 = configValueList.indexOf(val2);
if(-1 == sort1 || -1 == sort2){
return 0;
}
return sort1 - sort2;
}
}
}
或者这样内部类
public class TestSorting {
public static void main(String[] args) {
List<Developer> listDevs = getDevelopers();
Collections.sort(listDevs, new Comparator<Developer>() {
@Override
public int compare(Developer o1, Developer o2) {
return o1.getAge() - o2.getAge();
}
});
for (Developer developer : listDevs) {
System.out.println(developer);
}
}
private static List<Developer> getDevelopers() {
List<Developer> result = new ArrayList<Developer>();
result.add(new Developer("mkyong", new BigDecimal("70000"), 33));
result.add(new Developer("alvin", new BigDecimal("80000"), 20));
result.add(new Developer("jason", new BigDecimal("100000"), 10));
result.add(new Developer("iris", new BigDecimal("10000"), 23));
return result;
}
}
在Java8使⽤Lamdba排序
public class TestSortingLamdba {
public static void main(String[] args) {
List<Developer> listDevs = getDevelopers();
listDevs.sort((Developer o1, Developer o2) -> o1.getAge() - o2.getAge()); listDevs.forEach(System.out::println);
System.out.println("----------------");
listDevs.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
listDevs.forEach(System.out::println);
System.out.println("----------------");
listDevs.sort(paring(Developer::getSalary));
listDevs.forEach(System.out::println);
System.out.println("----------------");
listDevs.sort(paring(Developer::getSalary).reversed()); listDevs.forEach(System.out::println);
}
private static List<Developer> getDevelopers() {
List<Developer> result = new ArrayList<Developer>();
result.add(new Developer("mkyong", new BigDecimal("70000"), 33));
result.add(new Developer("alvin", new BigDecimal("80000"), 20));
result.add(new Developer("jason", new BigDecimal("100000"), 10));
result.add(new Developer("iris", new BigDecimal("10000"), 23));
return result;
}
}。

相关文档
最新文档