List排序的问题
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
1.對List進(jìn)行排序可以通過這種方式,實(shí)現(xiàn)(implements) Comparable<T>接口@Overridepublic int compareTo( T o){if (null == o){return -1;}RunningInstancesItemType instance0 = this.instanceInfo;RunningInstancesItemType instance1 = o.getInstanceInfo();if (null == instance0 || null == instance1){return -1;}else{// 按運(yùn)行時(shí)間排序String launchTime0 = this.getInstanceInfo().getLaunchTime();String launchTime1 = o.getInstanceInfo().getLaunchTime();if (!launchTime0.equals(launchTime1)){return launchTime1.compareTo(launchTime0);}else{// 如果運(yùn)行時(shí)間相同,則按Id排序String instanceId0 = this.getInstanceInfo().getInstanceId();String instanceId1 = o.getInstanceInfo().getInstanceId();return instanceId0.compareTo(instanceId1);}}}@Overridepublic boolean equals(Object obj){boolean equals;if (null == obj){equals = false;}else if (this == obj){equals = true;}else if (this.getClass() != obj.getClass()){equals = false;}else{InstanceBean o = (InstanceBean)obj;RunningInstancesItemType instance1 = this.instanceInfo;RunningInstancesItemType instance2 = o.getInstanceInfo();if (null == instance1 || null == instance2){equals = false;}else{if (null == instance1.getLaunchTime()){equals = false;}else{equals = instance1.getLaunchTime().equals(instance2.getLaunchTime());}}}return equals;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result+ ((null == this.getInstanceInfo() || null == this.getInstanceInfo().getLaunchTime()) ?0 : this.getInstanceInfo().getLaunchTime().hashCode());return result;}
2.也可以使用TreesSet實(shí)現(xiàn)
SortedSet<T> sortedSet = new TreeSet<T>(); List<T> instanceList; sortedSet.add(***);//此處*為T的實(shí)例化 instanceList = new ArrayList<T>(sortedSet);
轉(zhuǎn)載于:https://my.oschina.net/lgscofield/blog/471202
總結(jié)
- 上一篇: VC++读取图像RGB值
- 下一篇: [目录]Pentaho Kettle解决