获取不同元素与相同元素

来源:互联网 发布:mac摄氏度符号怎么打 编辑:程序博客网 时间:2024/05/23 21:24
package com.icfcc.grjmail.util.tool;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.icfcc.grjmail.dto.QuantityDto;


public class CompareDataTool {

public static void main(String[] args) {
List<QuantityDto> list = new ArrayList<QuantityDto>();
List<QuantityDto> list2 = new ArrayList<QuantityDto>();
// 生成excel
QuantityDto dto = new QuantityDto();
dto.setOrgCode("111");
dto.setAccount("111");
dto.setStartDate("111");
dto.setType("111");
dto.setBalance("111");
dto.setName("111");
dto.setCertType("111");
dto.setCertNo("111");


QuantityDto dto2 = new QuantityDto();
dto2.setOrgCode("dto2");
dto2.setAccount("dto2");
dto2.setStartDate("dto2");
dto2.setType("dto2");
dto2.setBalance("dto2");
dto2.setName("dto2");
dto2.setCertType("dto2");
dto2.setCertNo("dto2");
list.add(dto);
list.add(dto2);

QuantityDto dto3 = new QuantityDto();
dto3.setOrgCode("dto2");
dto3.setAccount("dto2");
dto3.setStartDate("dto2");
dto3.setType("dto2");
dto3.setBalance("dto2");
dto3.setName("dto2");
dto3.setCertType("dto2");
dto3.setCertNo("dto2");
list2.add(dto3);
List<QuantityDto> samelist = getSameList(list, list2);
List<QuantityDto> differlist = getDiffList(list, list2);
for (int i = 0; i < samelist.size(); i++) {
System.out.println("same=="+samelist.get(i).getName());
}
for (int i = 0; i < differlist.size(); i++) {
System.out.println("differ=="+differlist.get(i).getName());
}


}



/**
* 获取两个List的不同元素
* @param list1
* @param list2
* @return
*/
public static List<QuantityDto> getDiffList(List<QuantityDto> list1,
List<QuantityDto> list2) {
List<QuantityDto> differList = new ArrayList<QuantityDto>();
List<QuantityDto> maxList = list1;
List<QuantityDto> minList = list2;
if (list2.size() > list1.size()) {
maxList = list2;
minList = list1;
}
// 用一个map存放lsit的所有元素,其中的key为lsit1的各个元素,value为该元素出现的次数,接着把list2的所有元素也放到map里,如果已经存在则value加1,
Map<QuantityDto, Integer> map = new HashMap<QuantityDto, Integer>(maxList.size());
for (QuantityDto string : maxList) {
map.put(string, 1);
}
for (QuantityDto string : minList) {
if (map.get(string) != null) {
map.put(string, 2);
continue;
}
differList.add(string);
}
for (Map.Entry<QuantityDto, Integer> entry : map.entrySet()) {
// 只获取value大于1的元素
if (entry.getValue() == 1) {
differList.add(entry.getKey());
}
}
return differList;
}
/**
* 获取两个List的相同元素
* @param list1
* @param list2
* @return
*/
public static List<QuantityDto> getSameList(List<QuantityDto> list1,
List<QuantityDto> list2) {
List<QuantityDto> sameList = new ArrayList<QuantityDto>();
List<QuantityDto> maxList = list1;
List<QuantityDto> minList = list2;
if (list2.size() > list1.size()) {
maxList = list2;
minList = list1;
}
// 用一个map存放lsit的所有元素,其中的key为lsit1的各个元素,value为该元素出现的次数,接着把list2的所有元素也放到map里,如果已经存在则value加1,
Map<QuantityDto, Integer> map = new HashMap<QuantityDto, Integer>(maxList.size());
for (QuantityDto string : maxList) {
map.put(string, 1);
}
for (QuantityDto string : minList) {
if (map.get(string) != null) {
map.put(string, 2);
continue;
}
}
for (Map.Entry<QuantityDto, Integer> entry : map.entrySet()) {
// 只获取value大于1的元素
if (entry.getValue() > 1) {
sameList.add(entry.getKey());
}
}
return sameList;
}

}


package com.icfcc.grjmail.dto;


public class QuantityDto {

private String orgCode;//金融机构代码
private String account;//业务号
private String startDate;//开户日期
private String type;//业务种类细分
private String balance;//余额
private String name;//姓名
private String certType;//证件类型
private String certNo;//证件号码
public String getOrgCode() {
return orgCode;
}
public void setOrgCode(String orgCode) {
this.orgCode = orgCode;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getBalance() {
return balance;
}
public void setBalance(String balance) {
this.balance = balance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCertNo() {
return certNo;
}
public void setCertNo(String certNo) {
this.certNo = certNo;
}
public String getCertType() {
return certType;
}
public void setCertType(String certType) {
this.certType = certType;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((account == null) ? 0 : account.hashCode());
result = prime * result + ((balance == null) ? 0 : balance.hashCode());
result = prime * result + ((certNo == null) ? 0 : certNo.hashCode());
result = prime * result
+ ((certType == null) ? 0 : certType.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((orgCode == null) ? 0 : orgCode.hashCode());
result = prime * result
+ ((startDate == null) ? 0 : startDate.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
QuantityDto other = (QuantityDto) obj;
if (account == null) {
if (other.account != null)
return false;
} else if (!account.equals(other.account))
return false;
if (balance == null) {
if (other.balance != null)
return false;
} else if (!balance.equals(other.balance))
return false;
if (certNo == null) {
if (other.certNo != null)
return false;
} else if (!certNo.equals(other.certNo))
return false;
if (certType == null) {
if (other.certType != null)
return false;
} else if (!certType.equals(other.certType))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (orgCode == null) {
if (other.orgCode != null)
return false;
} else if (!orgCode.equals(other.orgCode))
return false;
if (startDate == null) {
if (other.startDate != null)
return false;
} else if (!startDate.equals(other.startDate))
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}

}

原创粉丝点击