将List中的某一个元素移动到首位或指定位置

1. Collections.swap(list,b,c);(注意:索引为a的元素与索引为b的元素交换位置,其他元素的索引值不变)

  list是你需要换位置的List。

  a是你当前需要换位置的元素的索引。

  b是你需要将这个元素换到哪个索引。

2.list.add(0, list.remove(i));(注意:先移除,然后把移除的元素放在指定位置,指定位置之后元素的索引值往后加1)



 List<String> test = Lists.newArrayList();
        test.add("test1");
        test.add("test2");
        test.add("test3");

        System.out.println("更改前:" + JSON.toJSONString(test));

        Collections.swap(test, 1, 0);

        System.out.println("更改后:" + JSON.toJSONString(test));
--------------------------------------------------------------
 Collections.sort(proList, Comparator.comparing(ScenicListResp::getKindsLevel).reversed());                     
if (searchScenicReq.getCityId() == 370400){
// Collections.swap(proList, 1, 0);
proList.add(0, proList.remove(4));
proList.add(1, proList.remove(23));
proList.add(2, proList.remove(3));
}
原文地址:https://www.cnblogs.com/yangsanluo/p/15094138.html