标签:stat 信息 复数 相同 collect nts 问题 arraylist turn
对象--JavaBean
重写equals方法,确定如何为同一对象(如对象中某几个字段相同)。
类--处理重复数据
调用处理方法:
1)新建一个新的集合,传入旧集合(某几个(属性)字段有相同记录的)
2)设置开关:boolean flag=false;()
3) 遍历旧集合, flag=false;,
在内部再次遍历新集合 ,
循环判断:旧集合的数据是否与新集合中某一元素相同(重写的equals方法);
若是:则说明该条记录已经出现过(旧计划中已有),则设置flag=true; break;跳出循环--------即该条数据不加入新集合中
if(!flag){ //旧集合的数据不与新集合中任一元素相同--------旧集合的该条数据加入集合中}
代码实例1:
package delSame;
public class Account {
private int id;
private String AccountName;
private String email;
private String address;
public Account() {
}
public Account(int id, String accountName, String email, String address) {
super();
this.id = id;
AccountName = accountName;
this.email = email;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAccountName() {
return AccountName;
}
public void setAccountName(String accountName) {
AccountName = accountName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
//出现名字与地址相同,认为是同一个部门
public boolean equals(Object o){
Account a=(Account) o;
return a.getAccountName().equals(AccountName)&&a.getAddress().equals(address)?true:false;
}
}
--------------------------------------------------------------处理逻辑--------------------------------------------------------------------------------------------
package delSame;
import java.util.ArrayList;
import java.util.List;
public class EquAccount {
public static void main(String[] args) {
List<Account> old=new ArrayList<Account>();
old.add(new Account(1,"a","ed","11"));
old.add(new Account(2,"a","ed","111"));
old.add(new Account(3,"b","ed","111"));
old.add(new Account(4,"b","ed","111"));
old.add(new Account(5,"b","ed","11"));
old.add(new Account(6,"a","ed","111"));
old.add(new Account(7,"b","ed","111"));
old.add(new Account(8,"b","ed","11"));
old.add(new Account(9,"a","ed","111"));
old.add(new Account(10,"a","ed","111"));
old.add(new Account(11,"b","ed","111"));
List<Account> newAcc=new EquAccount().newAccount(old);
for(Account a:newAcc){
System.out.println(a.getId()+" "+a.getAccountName()+" "+a.getAddress()+" "+a.getEmail());
}
}
//删除重复的部门信息
public List<Account> newAccount(List<Account> old){
List<Account> newAccount=new ArrayList<Account>();
boolean flag=false;
for(Account o:old){
flag=false;
for(Account n:newAccount){
if(n.equals(o)){//相同,
flag=true;//不添加
break;
}
}
if(!flag){//不同,添加入集合
newAccount.add(o);
}
}
return newAccount;
}
}
代码示例2:
package delSame;
class Box{
private String boxNo;//箱号
private String boxType; //箱型
private String boxName;//名字
private int count_20;//箱型20数量
private int count_40;//箱型40数量
public void setCount40(int count_40){
this.count_40 = count_40;
}
public void setCount20(int count_20){
this.count_20 = count_20;
}
public int getCount20(){
return count_20;
}
public int getCount40(){
return count_40;
}
public Box(String boxNo,String boxName,String boxType){
this.boxNo = boxNo;
this.boxName = boxName;
this.boxType = boxType;
}
public String getBoxNo() {
return boxNo;
}
public void setBoxNo(String boxNo) {
this.boxNo = boxNo;
}
public String getBoxName() {
return boxName;
}
public void setBoxName(String boxName) {
this.boxName = boxName;
}
public String getBoxType() {
return boxType;
}
public void setBoxType(String boxType) {
this.boxType = boxType;
}
//判断是否是同一个箱子
public boolean equals(Object o){
Box s = (Box)o;
return s.getBoxNo().equals(boxNo)&& s.getBoxName().equals(boxName)?true:false;
}
}
-------------------------------------------------------处理逻辑--------------------------------------------------------------------------------------------
package delSame;
import java.util.*;
/*
该示例为把集合中把同一箱子(箱号和名字相同即认定为同一箱子)根据型号的进行统计输出
例如:
箱号 箱型 名字
1 20 箱1
2 40 箱2
2 20 箱2
2 40 箱2
要求处理成的结果:
箱号 名字 20箱型数量 40箱型数量
1 箱1 1 0
2 箱2 2 1
注: 本示例是通过程序来控制的,问题:该问题应该可以通过sql语句来进行处理,首先把要统计的列根据某些条件进行列转行后,分组进行计数。
*/
public class EquComp{
public static void main(String[] args){
List<Box> hs = new ArrayList<Box>();
hs.add(new Box("1","箱1","20"));
hs.add(new Box("3","箱2","40"));
hs.add(new Box("2","箱2","40"));
hs.add(new Box("2","箱2","20"));
hs.add(new Box("2","箱2","40"));
hs.add(new Box("2","箱2","40"));
List<Box> newStats = new EquComp().getBox(hs);
System.out.println("箱号--------名字--------20箱型数量--------40箱型数量");
for(Box s:newStats){
System.out.println(s.getBoxNo()+"------------"+ s.getBoxName()+"------------"+s.getCount20()+"----------------"+ s.getCount40());
}
}
public List<Box> getBox(List<Box> hs){
List<Box> boxList = new ArrayList<Box>();
boolean flag = false;
String boxType = "";
for (Box sta : hs) {
flag = false;
boxType = sta.getBoxType();
// System.out.println("s1"+boxList);
for (Box n : boxList) { //没有数据不执行,跳出
//System.out.println("s2"+boxList);
if (n.equals(sta)) {// 新集合出现与该集合同一个箱子
if ("20".equals(boxType)) { //改变型号
n.setCount20(n.getCount20()+1);
} else if ("40".equals(boxType)) {
n.setCount40(n.getCount40()+1);
}
flag = true;//不加入新集合中
break;
}
}
if (!flag) { //箱子不相同,则重新记录箱子类型个数
if ("20".equals(boxType)) {
sta.setCount20(1);
} else if ("40".equals(boxType)) {
sta.setCount40(1);
}
boxList.add(sta); //加入集合
}
}
//通过程序进行排序
Collections.sort(boxList,new ComparableYS());
return boxList;
}
}
class ComparableYS implements Comparator{
public int compare(Object o1, Object o2){
Box s1 = (Box)o1;
Box s2 = (Box)o2;
//排序顺序 boxNo,boxName
if(!s1.getBoxNo().equals(s2.getBoxNo())){
return s1.getBoxNo().compareTo(s2.getBoxNo());
}else if(!s1.getBoxName().equals(s2.getBoxName())){
return s1.getBoxName().compareTo(s2.getBoxName());
}else{
return 0;
}
}
public boolean equals(Object o){
return false;
}
}
注: 本示例是通过程序来控制的,问题:该问题应该可以通过sql语句来进行处理,首先把要统计的列根据某些条件进行列转行后,分组进行计数。
标签:stat 信息 复数 相同 collect nts 问题 arraylist turn
原文地址:http://www.cnblogs.com/zx-zx/p/7811639.html