借助索引排序列表

ave*_*496 1 java java-stream

我能以某种方式访问​​列表中对象的索引吗?

myList.stream().sorted((o1, o2) -> 0).collect(Collectors.toList())
Run Code Online (Sandbox Code Playgroud)

例如:

我希望首先显示奇数索引,最后显示索引.

Hol*_*ger 5

我不认为基于索引的重新排序操作是实际的排序操作.例如,没有人会考虑实施像Collections.reverse(List)分类操作那样的操作.

将奇数位置的元素移动到原位的有效方法是

public static <T> void oddFirst(List<T> list) {
    final int size = list.size();
    for(int index1=0, index2=list.size()/2|1; index2<size; index1+=2, index2+=2)
        Collections.swap(list, index1, index2);
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以在此答案中流式传输索引,以生成新的索引List.