实体类:
package test; public class NightlyRate { private String rate; public String getRate() { return rate; } public void setRate(String rate) { this.rate = rate; } public NightlyRate(String rate){ this.rate = rate; } }
package test; import java.util.List; public class RateInfo { private String name; private List<NightlyRate> nightlyRates; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<NightlyRate> getNightlyRates() { return nightlyRates; } public void setNightlyRates(List<NightlyRate> nightlyRates) { this.nightlyRates = nightlyRates; } public RateInfo(String name){ this.name = name; } public RateInfo(String name,List<NightlyRate> nightlyRates){ this.name = name; this.nightlyRates = nightlyRates; } }
测试类:
package test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import aA.RandomDemo; /** * * @Description:测试List排序 * @date 2018年7月16日,下午11:10:14 */ public class testDemo { public static void main(String[] args) { //List<RateInfo>的容量,最大容量:29300则nightNum最大容量为:5 //rateNum容量与nightNum容量成反比 int rateNum = 2000; //List<NightlyRate>的容量 int nightNum = 20; List<RateInfo> rList = new ArrayList<RateInfo>(); System.out.println("********************《start添加原始数据》************************************"); //添加原始数据 for(int i=0;i<rateNum;i++){ List<NightlyRate> nightlyRates = new ArrayList<NightlyRate>(); for(int j = 0;j<nightNum;j++){ NightlyRate nightlyRate = new NightlyRate(RandomDemo.getRandom(4,RandomDemo.TYPE.NUMBER)); nightlyRates.add(nightlyRate); } rList.add(new RateInfo(RandomDemo.getRandom(10,RandomDemo.TYPE.LETTER_CAPITAL),nightlyRates)); } //原始数据的输出 for (RateInfo rateInfo : rList) { System.out.println("RateInfo原始数据:"+rateInfo.getName()); System.out.print(" ----> "+"排序前价格:"); for(int j = 0;j<nightNum;j++){ System.out.print(rateInfo.getNightlyRates().get(j).getRate()+","); } System.out.println(); } System.out.println("********************《end添加原始数据》***************************************"); System.out.println(); sortList(rList, nightNum, rateNum); } /** * * @Description: 集合排序(rList(保存RateInfod对象)中有一个List的属性 nightlyRates(保存NightlyRate对象) * 首先给属性nightlyRates排序,然后再个rList根据排好序的属性nightlyRates第一个对象进行排序) * @param @param rList * @param @param nightNum * @param @param rateNum * @return void * @throws */ public static void sortList(List<RateInfo> rList,int nightNum,int rateNum){ long start; long end; long sortInTime = 0; long sortOutTime = 0; System.out.println("*******************《start RateInfo内属性价格排序》***************************"); for (RateInfo rateInfo : rList) { //进行价格排序 start = System.currentTimeMillis(); Collections.sort(rateInfo.getNightlyRates(), new Comparator<NightlyRate>() { public int compare(NightlyRate R1, NightlyRate R2) { Integer rateCode1 = Integer.parseInt(R1.getRate()); Integer rateCode2 = Integer.parseInt(R2.getRate()); if (rateCode1 > rateCode2) { return 1; } if (rateCode1 == rateCode2) { return 0; } return -1; } }); // Collections.sort(rateInfo.getNightlyRates(), Comparator.comparing(NightlyRate::getRate)); end = System.currentTimeMillis(); sortInTime = end-start; System.out.println("RateInfo数据内容排序getNightlyRates()价格:"+rateInfo.getName()); System.out.print(" ----> "+"排序后价格:"); for(int j = 0;j<nightNum;j++){ System.out.print(rateInfo.getNightlyRates().get(j).getRate()+","); } System.out.println(); } System.out.println("*******************《end RateInfo内属性价格排序》****************************"); System.out.println(); System.out.println("**********************《start RateInfo排序》********************************"); start = System.currentTimeMillis(); Collections.sort(rList, new Comparator<RateInfo>() { public int compare(RateInfo R1, RateInfo R2) { Integer rateCode1 = Integer.parseInt(R1.getNightlyRates().get(0).getRate()); Integer rateCode2 = Integer.parseInt(R2.getNightlyRates().get(0).getRate()); if (rateCode1 > rateCode2) { return 1; } if (rateCode1 == rateCode2) { return 0; } return -1; } }); end = System.currentTimeMillis(); sortOutTime = end-start; for(int i=0;i<rateNum;i++){ System.out.println("RateInfo的getNightlyRates()的第一个对象数据排序:"+rList.get(i).getName()); System.out.print(" ----> "+"排序后价格:"); for(int j = 0;j<nightNum;j++){ System.out.print(rList.get(i).getNightlyRates().get(j).getRate()+","); } System.out.println(); } System.out.println("**********************《end RateInfo排序》**********************************"); System.out.println("RateInfo内部价格排序运行时间:"+sortInTime); System.out.println("RateInfo排序运行时间:"+sortOutTime); } }
测试结果: