How to get a reversed list view on a list in Java?

前端 未结 12 787
自闭症患者
自闭症患者 2020-11-28 21:05

I want to have a reversed list view on a list (in a similar way than List#sublist provides a sublist view on a list). Is there some function which provides this

12条回答
  •  鱼传尺愫
    2020-11-28 21:24

    Guava provides this: Lists.reverse(List)

    List letters = ImmutableList.of("a", "b", "c");
    List reverseView = Lists.reverse(letters); 
    System.out.println(reverseView); // [c, b, a]
    

    Unlike Collections.reverse, this is purely a view... it doesn't alter the ordering of elements in the original list. Additionally, with an original list that is modifiable, changes to both the original list and the view are reflected in the other.

提交回复
热议问题