把list中根据相同属性(合为一个),计算不同属性转化为另外一个list

1.原list封装

package test; import java.io.Serializable; import java.util.Date; /** * Created by hongdi.ren. */ public class PrinterStatusLog implements Serializable{private int id; private int shopId; private int deviceNumber; private int connectStatus; private Date statusTime; private Date addTime; private Date updateTime; public int getPaperStatus() { return paperStatus; } public void setPaperStatus(int paperStatus) { this.paperStatus = paperStatus; } private int paperStatus; public int getId() { return id; }public void setId(int id) { this.id = id; }public int getShopId() { return shopId; }public void setShopId(int shopId) { this.shopId = shopId; }public int getDeviceNumber() { return deviceNumber; }public void setDeviceNumber(int deviceNumber) { this.deviceNumber = deviceNumber; }public int getConnectStatus() { return connectStatus; }public void setConnectStatus(int connectStatus) { this.connectStatus = connectStatus; }public Date getStatusTime() { return statusTime; }public void setStatusTime(Date statusTime) { this.statusTime = statusTime; }public Date getAddTime() { return addTime; }public void setAddTime(Date addTime) { this.addTime = addTime; }public Date getUpdateTime() { return updateTime; }public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; }@Override public String toString() { final StringBuilder sb = new StringBuilder("PrinterStatusLog{"); sb.append("shopId=").append(shopId); sb.append(", deviceNumber=").append(deviceNumber); sb.append(", connectStatus=").append(connectStatus); sb.append(", statusTime=").append(statusTime); sb.append(", updateTime=").append(updateTime); sb.append('}'); return sb.toString(); } }






2.转换后的list(根据shopId&&deviceNumber相同的元素,转化为另外一个list,并且计算出connectstatus中0和1的个数,放在第二个list的offlinderimes和onlinetimes中,并保留addtime,updatetime,statusTime最近的的时间)





package test; import java.util.Date; /** * Created by Administrator on 2016/5/24. */ public class PrinterStatusVO {private int shopId; private String shopName; private String cityName; private int deviceNumber; private Date statusTime; private Date firstTime; private Date lastTime; private int connectStatus; /** * 上线次数 */private int onlineTimes; /** * 下线次数 */ private int offlineTimes; public int getShopId() { return shopId; }public void setShopId(int shopId) { this.shopId = shopId; }public String getShopName() { return shopName; }public void setShopName(String shopName) { this.shopName = shopName; }public int getDeviceNumber() { return deviceNumber; }public void setDeviceNumber(int deviceNumber) { this.deviceNumber = deviceNumber; }public Date getStatusTime() { return statusTime; }public void setStatusTime(Date statusTime) { this.statusTime = statusTime; }public Date getLastTime() { return lastTime; }public void setLastTime(Date lastTime) { this.lastTime = lastTime; }public Date getFirstTime() { return firstTime; }public void setFirstTime(Date firstTime) { this.firstTime = firstTime; }public int getOnlineTimes() { return onlineTimes; }public void setOnlineTimes(int onlineTimes) { this.onlineTimes = onlineTimes; }public int getOfflineTimes() { return offlineTimes; }public void setOfflineTimes(int offlineTimes) { this.offlineTimes = offlineTimes; }public String getCityName() { return cityName; }public void setCityName(String cityName) { this.cityName = cityName; }public int getConnectStatus() { return connectStatus; }public void setConnectStatus(int connectStatus) { this.connectStatus = connectStatus; }@Override public String toString() { final StringBuilder sb = new StringBuilder("PrinterStatusLogVO{"); sb.append("shopId=").append(shopId); sb.append(", deviceNumber=").append(deviceNumber); sb.append(", statusTime=").append(statusTime); sb.append(", firstTime=").append(firstTime); sb.append(", lastTime=").append(lastTime); sb.append(", onlineTimes=").append(onlineTimes); sb.append(", offlineTimes=").append(offlineTimes); sb.append('}'); return sb.toString(); } }






3.测试:
package test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import net.sf.json.JSONArray; public class ListToList { public static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd"); public static void main(String[] args) { List list = new ArrayList(); PrinterStatusLog n5 = new PrinterStatusLog(); n5.setShopId(1); n5.setDeviceNumber(2); n5.setConnectStatus(0); try { n5.setStatusTime(FORMAT.parse("2016-03-27")); n5.setUpdateTime(FORMAT.parse("2016-04-01")); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }System.out.println("转化之前n1:" + n5.toString()); PrinterStatusLog n2 = new PrinterStatusLog(); n2.setShopId(1); n2.setDeviceNumber(3); n2.setConnectStatus(1); try { n2.setStatusTime(FORMAT.parse("2016-03-28")); n2.setUpdateTime(FORMAT.parse("2016-03-29")); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }System.out.println("转化之前:"+"n2:" + n2.toString()); PrinterStatusLog n3 = new PrinterStatusLog(); n3.setShopId(2); n3.setDeviceNumber(4); n3.setConnectStatus(1); try { n3.setStatusTime(FORMAT.parse("2016-03-29")); n3.setUpdateTime(FORMAT.parse("2016-04-09")); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("转化之前:"+"n3:" + n3.toString()); PrinterStatusLog n4 = new PrinterStatusLog(); n4.setShopId(2); n4.setDeviceNumber(4); n4.setConnectStatus(1); try { n4.setStatusTime(FORMAT.parse("2016-03-30")); n4.setUpdateTime(FORMAT.parse("2016-05-01")); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("转化之前:"+"n4:" + n4.toString()); PrinterStatusLog n1 = new PrinterStatusLog(); n1.setShopId(1); n1.setDeviceNumber(2); n1.setConnectStatus(1); try { n1.setStatusTime(FORMAT.parse("2016-03-30")); n1.setUpdateTime(FORMAT.parse("2016-05-01")); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("转化之前:"+"n5:" + n1.toString()); list.add(n1); list.add(n2); list.add(n3); list.add(n4); list.add(n5); List printerStatusLogVOs = mergeList(list); for (PrinterStatusVO a : printerStatusLogVOs) { System.out.println(a.toString()); } } public static List mergeList(List printerStatusLogs) { HashMap map = new HashMap(); List printerStatusLogVOs = newPrinterStatusLogVOList(printerStatusLogs); for (PrinterStatusVO bean : printerStatusLogVOs) { String keyStr = String.valueOf(bean.getShopId()) + String.valueOf(bean.getDeviceNumber()); if (map.containsKey(keyStr)) {bean.setStatusTime(map.get(keyStr).getStatusTime().compareTo(bean.getStatusTime()) == 1 ? map.get(keyStr).getStatusTime() : bean.getStatusTime()); System.out.println("状态:"+bean.getConnectStatus()); if (bean.getConnectStatus() == PrintStatusCode.ONLINE) { if (map.get(keyStr).getFirstTime() == null) { bean.setFirstTime(bean.getFirstTime()); }if (bean.getFirstTime() == null) { bean.setFirstTime(map.get(keyStr).getFirstTime()); }if (map.get(keyStr).getFirstTime() != null && bean.getFirstTime() != null) { bean.setFirstTime(map.get(keyStr).getFirstTime().compareTo(bean.getFirstTime()) == 1 ? bean.getFirstTime() : map.get(keyStr).getFirstTime()); } //问题找到 bean.setLastTime(map.get(keyStr).getLastTime()); } else { if (map.get(keyStr).getLastTime() == null) { bean.setLastTime(bean.getLastTime()); }if (bean.getLastTime() == null) { bean.setLastTime(map.get(keyStr).getLastTime()); System.out.println("map.get(keyStr).getLastTime():"+map.get(keyStr).getLastTime()); }if (map.get(keyStr).getLastTime() != null && bean.getLastTime() != null) { bean.setLastTime(map.get(keyStr).getLastTime().compareTo(bean.getLastTime()) == 1 ? map.get(keyStr).getLastTime() : bean.getLastTime()); } //问题找到 bean.setFirstTime(map.get(keyStr).getFirstTime()); }bean.setOfflineTimes(map.get(keyStr).getOfflineTimes() + bean.getOfflineTimes()); bean.setOnlineTimes(map.get(keyStr).getOnlineTimes() + bean.getOnlineTimes()); }map.put(keyStr, bean); System.out.println("map状态:"+map.get(keyStr).getConnectStatus()+""+"F:"+map.get(keyStr).getFirstTime()+""+"L:"+map.get(keyStr).getLastTime()); } printerStatusLogVOs.clear(); printerStatusLogVOs.addAll(map.values()); return printerStatusLogVOs; } private static List newPrinterStatusLogVOList(List list) { List printerStatusLogVOList = new ArrayList(); for (PrinterStatusLog bean : list) { // ...HbtShopBO shopBO = // hobbitShopService.loadShop(bean.getShopId()); PrinterStatusVO printerStatusLogVO = new PrinterStatusVO(); printerStatusLogVO.setShopId(bean.getShopId()); // printerStatusLogVO.setShopName(shopBO.getShopName()); // printerStatusLogVO.setCityName(shopBO.getCityName()); printerStatusLogVO.setDeviceNumber(bean.getDeviceNumber()); printerStatusLogVO.setStatusTime(bean.getStatusTime()); printerStatusLogVO.setConnectStatus(bean.getConnectStatus()); printerStatusLogVO.setOfflineTimes(bean.getConnectStatus() == PrintStatusCode.OFFLINE ? PrintStatusCode.ONLINE : PrintStatusCode.OFFLINE); printerStatusLogVO.setOnlineTimes(bean.getConnectStatus() == PrintStatusCode.ONLINE ? PrintStatusCode.ONLINE : PrintStatusCode.OFFLINE); if (bean.getConnectStatus() == PrintStatusCode.ONLINE) { printerStatusLogVO.setFirstTime(bean.getUpdateTime()); } else { printerStatusLogVO.setLastTime(bean.getUpdateTime()); } System.out.println("转化之后"); System.out.println("转化之后:"+printerStatusLogVO.toString()); printerStatusLogVOList.add(printerStatusLogVO); } return printerStatusLogVOList; } }




【把list中根据相同属性(合为一个),计算不同属性转化为另外一个list】

    推荐阅读