//既定顺序
List sortStrings = Arrays.asList("香蕉", "苹果", "梨子", "芒果", "橙子");
//需要排序
List needToSort = Arrays.asList("苹果", "香蕉", "苹果", "橙子", "芒果","梨子");
//通过对比 需要比较元素在ArrayList的index 就可以得到比较方法
//比for循环简洁
List stringList=needToSort.stream().sorted(Comparator.comparingInt(sortStrings::indexOf)).collect(Collectors.toList());
//输出
sortStrings.forEach(System.out::print);
System.out.println("");
needToSort.forEach(System.out::print);
System.out.println("");
stringList.forEach(System.out::print);
自定义排序,字符串排序,Comparator

相关文章:

  • 2021-05-21
  • 2021-09-01
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-30
  • 2022-03-02
  • 2022-12-23
  • 2021-06-07
相关资源
相似解决方案