What is the Simplest Way to Reverse an ArrayList?

前端 未结 10 976
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 18:10

What is the simplest way to reverse this ArrayList?

ArrayList aList = new ArrayList<>();

//Add elements to ArrayList object
aList.add(\         


        
10条回答
  •  孤城傲影
    2020-11-28 18:38

    We can also do the same using java 8.

    public static List reverseList(List list) {
            List reverse = new ArrayList<>(list.size());
    
            list.stream()
                    .collect(Collectors.toCollection(LinkedList::new))
                    .descendingIterator()
                    .forEachRemaining(reverse::add);
    
            return reverse;
        }
    

提交回复
热议问题