Java List Reverse reverseCopy(List original)

Here you can find the source of reverseCopy(List original)

Description

Returns a reversed shallow copy of the given list.

License

Open Source License

Parameter

Parameter Description
original the list to revert

Return

a reversed shallow copy of the given list

Declaration

public static <E> List<E> reverseCopy(List<E> original) 

Method Source Code


//package com.java2s;
/*/* w ww .  ja  va 2  s  .c  o m*/
 * Copyright 2014 Stathis Aliprantis
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with This program. If not, see http://www.gnu.org/licenses/.
 */

import java.util.*;

public class Main {
    /**
     * Returns a reversed shallow copy of the given list.
     * The result is {@link Serializable} and {@link RandomAccess}.
     * 
     * @param original the list to revert
     * @return a reversed shallow copy of the given list
     */
    public static <E> List<E> reverseCopy(List<E> original) {
        List<E> result = new ArrayList<E>(original.size());

        if (original instanceof RandomAccess) {
            for (int i = original.size() - 1; i >= 0; i--) {
                result.add(original.get(i));
            }
        } else {
            ListIterator<E> it = original.listIterator(original.size());
            while (it.hasPrevious()) {
                result.add(it.previous());
            }
        }

        return result;
    }
}

Related

  1. reverse(List src, List dst)
  2. reverse(List xs)
  3. reverse(List l)
  4. reverseCopy(Iterable list)
  5. reverseCopy(List list)
  6. reversed(final List list)
  7. reversed(List l)
  8. reversed(List list)
  9. reversedCopy(List src, List dest)