Java的List集合对象插入排序

需求背景,在一个循环里,每次循环出一个对象,将这个对象存放到另一个集合中,且另一个集合需要根据对象的某个属性的大小进行排序。
类似如下的集合

//RelateCountMatrixEntity对象,属性如下
private String xAxis;
private String yAxis;
private Integer count;
private double relateScore;
//RelateCountMatrixEntity对象集合
List<RelateCountMatrixEntity> recommandUserList = new ArrayList<>();

比如对Integer count属性进行排序,如果recommandUserList集合的长度不是很长,可以直接使用List里自带的排序方法即可recommandUserList.sort((a, b) -> b.count.compareTo(a.count));。 遗憾的是,这种方式适合int整形的数据,不能适用double relateScore这种类型。

一次只产生一个数据,放入一个列表中,并且这个列表最终顺序,插入排序可能是比较优雅的方式。
结合Java的list集合特性,该排序分为三个步骤:

  • 添位置: 首先将待排序的数据直接存放到list中,因为list是一个自增数组,把list集合延长一个位置避免数组越界;然后判断该条数据是否首次插入。
  • 找位置: 因为最后一条数据就是待排序的数据,依次与它前面数据进行比较找到自己合适的位置。
  • 进入位置: 位置找到后,根据位置下标插入数据即可。

提示:这时会想到待排序的数据第一次就插在最后一个位置了,会不会数据冗余;在找位置的时候,倒数第二个数据就把最后一个数据覆盖了;所以不会造成冗余。

List<RelateCountMatrixEntity> sort(
RelateCountMatrixEntity entity, List<RelateCountMatrixEntity> relateList) {
    
    
   //添位置
   relateList.add(entity);
   int size = relateList.size() - 1;
   if (size == 0) {
    
    
       return relateList;
   }
   //找位置
   int index = size;
   while (relateList.get(size).getRelateScore() 
   > relateList.get(index - 1).getRelateScore()) {
    
    
       index--;
       if (index <= 0) {
    
    
           break;
       }
   }
   //进入位置
   for (int i = size; i > index; i--) {
    
    
       relateList.set(i, relateList.get(i - 1));
   }
   relateList.set(index, entity);
   return relateList;
}

为了能够快速使用与验证,测试数据如下

RelateCountMatrixEntity entity = new RelateCountMatrixEntity(0.01);
RelateCountMatrixEntity entity1 = new RelateCountMatrixEntity(0.334);
RelateCountMatrixEntity entity2 = new RelateCountMatrixEntity(0.23);
RelateCountMatrixEntity entity3 = new RelateCountMatrixEntity(0.11);
RelateCountMatrixEntity entity4 = new RelateCountMatrixEntity(0.31);

List<RelateCountMatrixEntity> list = new ArrayList<>();
sort(entity, list);
System.out.println(JSON.toJSONString(list));
sort(entity1, list);
System.out.println(JSON.toJSONString(list));
sort(entity2, list);
System.out.println(JSON.toJSONString(list));
sort(entity3, list);
System.out.println(JSON.toJSONString(list));
sort(entity4, list);
System.out.println(JSON.toJSONString(list));

猜你喜欢

转载自blog.csdn.net/Hello_Ray/article/details/109106236