Java List Reverse reverse(List list)

Here you can find the source of reverse(List list)

Description

Return a new list with the keys reversed.

License

Apache License

Parameter

Parameter Description
list the list to reverse

Return

a new list with the keys in the opposite order

Declaration

public static <T> List<T> reverse(List<T> list) 

Method Source Code

//package com.java2s;
/*/*from   w w w.  ja v  a2s  .  c o m*/
Copyright 1996-2010 Ariba, Inc.
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    
$Id: //ariba/platform/util/core/ariba/util/core/ListUtil.java#38 $
*/

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

public class Main {
    /**
    Return a new list with the keys reversed.
    @param list the list to reverse
    @return a new list with the keys in the opposite order
        
    @aribaapi documented
    */
    public static <T> List<T> reverse(List<T> list) {
        List newList = list(list.size());
        for (int i = list.size() - 1; i >= 0; i--) {
            newList.add(list.get(i));
        }
        return newList;
    }

    /**
    Constructs a new type-safe List with a small initial capacity. <p/>
        
    To construct a type-safe <code>List</code>:<ul>
    <li><code>List&lt;X> typesafe = ListUtil.list()</code>
    </ul>
    For a raw <code>List</code>:<ul>
    <li><code>List raw = ListUtil.list()</code>
    </ul>
    There will be no compile warnings either way.
        
    @return new List
    @aribaapi public
    */
    public static <T> List<T> list() {
        return new ArrayList<T>(1); // OK
    }

    /**
    Primitive constructor. Constructs a List large enough to
    hold <b>initialCapacity</b> elements. The List will grow to
    accommodate additional objects, as needed. <p/>
        
    To construct a type-safe <code>List</code>:<ul>
    <li><code>List&lt;X> typesafe = ListUtil.list()</code>
    </ul>
    For a raw <code>List</code>:<ul>
    <li><code>List raw = ListUtil.list()</code>
    </ul>
    There will be no compile warnings either way.
        
    @param initialCapacity the initial capacity; must be greater
    than or equal to zero
    @return an empty List with the specified capacity
        
    @aribaapi public
    */
    public static <T> List<T> list(int initialCapacity) {
        return new ArrayList<T>(initialCapacity); // OK
    }

    /**
    Helper function to create a new List containing one Object.
        
    @param object the Object to add to the List
        
    @return a new List containing the specified <b>object</b>
    @aribaapi documented
    */
    public static <T> List<T> list(T object) {
        List<T> l = list(1);
        l.add(object);
        return l;
    }

    /**
    Helper function to create a new List containing two Objects.
        
    @param a the first Object to add to the List
    @param b the second Object to add to the List
        
    @return a new List containing the specified objects
    @aribaapi documented
    */
    public static <T> List<T> list(T a, T b) {
        List<T> l = list(2);
        l.add(a);
        l.add(b);
        return l;
    }

    /**
    Helper function to create a new List containing three
    Objects.
        
    @param a the first Object to add to the List
    @param b the second Object to add to the List
    @param c the third Object to add to the List
        
    @return a new List containing the specified objects
    @aribaapi documented
    */
    public static List list(Object a, Object b, Object c) {
        List l = list(3);
        l.add(a);
        l.add(b);
        l.add(c);
        return l;
    }

    /**
    Helper function to create a new List containing four
    Objects.
        
    @param a the first Object to add to the List
    @param b the second Object to add to the List
    @param c the third Object to add to the List
    @param d the fourth Object to add to the List
        
    @return a new List containing the specified objects
    @aribaapi documented
    */
    public static List list(Object a, Object b, Object c, Object d) {
        List l = list(4);
        l.add(a);
        l.add(b);
        l.add(c);
        l.add(d);
        return l;
    }

    /**
    Helper function to create a new List containing five
    Objects.
        
    @param a the first Object to add to the List
    @param b the second Object to add to the List
    @param c the third Object to add to the List
    @param d the fourth Object to add to the List
    @param e the fifth Object to add to the List
        
    @return a new List containing the specified objects
    @aribaapi documented
    */
    public static List list(Object a, Object b, Object c, Object d, Object e) {
        List l = list(5);
        l.add(a);
        l.add(b);
        l.add(c);
        l.add(d);
        l.add(e);
        return l;
    }

    /**
    Helper function to create a new List containing six Objects.
        
    @param a the first Object to add to the List
    @param b the second Object to add to the List
    @param c the third Object to add to the List
    @param d the fourth Object to add to the List
    @param e the fifth Object to add to the List
    @param f the sixth Object to add to the List
        
    @return a new List containing the specified objects
    @aribaapi documented
    */
    public static List list(Object a, Object b, Object c, Object d, Object e, Object f) {
        List l = list(6);
        l.add(a);
        l.add(b);
        l.add(c);
        l.add(d);
        l.add(e);
        l.add(f);
        return l;
    }

    /**
    Helper function to create a new List containing seven
    Objects.
        
    @param a the first Object to add to the List
    @param b the second Object to add to the List
    @param c the third Object to add to the List
    @param d the fourth Object to add to the List
    @param e the fifth Object to add to the List
    @param f the sixth Object to add to the List
    @param g the seventh Object to add to the List
        
    @return a new List containing the specified objects
    @aribaapi documented
    */
    public static List list(Object a, Object b, Object c, Object d, Object e, Object f, Object g) {
        List l = list(7);
        l.add(a);
        l.add(b);
        l.add(c);
        l.add(d);
        l.add(e);
        l.add(f);
        l.add(g);
        return l;
    }

    /**
    Helper function to create a new List containing eight
    Objects.
        
    @param a the first Object to add to the List
    @param b the second Object to add to the List
    @param c the third Object to add to the List
    @param d the fourth Object to add to the List
    @param e the fifth Object to add to the List
    @param f the sixth Object to add to the List
    @param g the seventh Object to add to the List
    @param h the eighth Object to add to the List
        
    @return a new List containing the specified objects
    @aribaapi documented
    */
    public static List list(Object a, Object b, Object c, Object d, Object e, Object f, Object g, Object h) {
        List l = list(8);
        l.add(a);
        l.add(b);
        l.add(c);
        l.add(d);
        l.add(e);
        l.add(f);
        l.add(g);
        l.add(h);
        return l;
    }

    /**
    Helper function to create a new List containing nine
    Objects.
        
    @param a the first Object to add to the List
    @param b the second Object to add to the List
    @param c the third Object to add to the List
    @param d the fourth Object to add to the List
    @param e the fifth Object to add to the List
    @param f the sixth Object to add to the List
    @param g the seventh Object to add to the List
    @param h the eighth Object to add to the List
    @param i the ninth Object to add to the List
        
    @return a new List containing the specified objects
    @aribaapi documented
    */
    public static List list(Object a, Object b, Object c, Object d, Object e, Object f, Object g, Object h,
            Object i) {
        List l = list(9);
        l.add(a);
        l.add(b);
        l.add(c);
        l.add(d);
        l.add(e);
        l.add(f);
        l.add(g);
        l.add(h);
        l.add(i);
        return l;
    }

    /**
    Helper function to create a new List containing ten Objects.
        
    @param a the first Object to add to the List
    @param b the second Object to add to the List
    @param c the third Object to add to the List
    @param d the fourth Object to add to the List
    @param e the fifth Object to add to the List
    @param f the sixth Object to add to the List
    @param g the seventh Object to add to the List
    @param h the eighth Object to add to the List
    @param i the ninth Object to add to the List
    @param j the tenth Object to add to the List
        
    @return a new List containing the specified objects
    @aribaapi documented
    */
    public static List list(Object a, Object b, Object c, Object d, Object e, Object f, Object g, Object h,
            Object i, Object j) {
        List l = list(10);
        l.add(a);
        l.add(b);
        l.add(c);
        l.add(d);
        l.add(e);
        l.add(f);
        l.add(g);
        l.add(h);
        l.add(i);
        l.add(j);
        return l;
    }

    /**
    Helper function to create a new List containing eleven
    Objects.
        
    @param a the first Object to add to the List
    @param b the second Object to add to the List
    @param c the third Object to add to the List
    @param d the fourth Object to add to the List
    @param e the fifth Object to add to the List
    @param f the sixth Object to add to the List
    @param g the seventh Object to add to the List
    @param h the eighth Object to add to the List
    @param i the ninth Object to add to the List
    @param j the tenth Object to add to the List
    @param k the eleventh Object to add to the List
        
    @return a new List containing the specified objects
    @aribaapi documented
    */
    public static List list(Object a, Object b, Object c, Object d, Object e, Object f, Object g, Object h,
            Object i, Object j, Object k) {
        List l = list(11);
        l.add(a);
        l.add(b);
        l.add(c);
        l.add(d);
        l.add(e);
        l.add(f);
        l.add(g);
        l.add(h);
        l.add(i);
        l.add(j);
        l.add(k);
        return l;
    }

    /**
    Helper function to create a new List containing twelve
    Objects.
        
    @param a the first Object to add to the List
    @param b the second Object to add to the List
    @param c the third Object to add to the List
    @param d the fourth Object to add to the List
    @param e the fifth Object to add to the List
    @param f the sixth Object to add to the List
    @param g the seventh Object to add to the List
    @param h the eighth Object to add to the List
    @param i the ninth Object to add to the List
    @param j the tenth Object to add to the List
    @param k the eleventh Object to add to the List
    @param l the twelfth Object to add to the List
        
    @return a new List containing the specified objects
    @aribaapi documented
    */
    public static List list(Object a, Object b, Object c, Object d, Object e, Object f, Object g, Object h,
            Object i, Object j, Object k, Object l) {
        List list = list(12);
        list.add(a);
        list.add(b);
        list.add(c);
        list.add(d);
        list.add(e);
        list.add(f);
        list.add(g);
        list.add(h);
        list.add(i);
        list.add(j);
        list.add(k);
        list.add(l);
        return list;
    }

    public static int size(Collection list) {
        return list != null ? list.size() : 0;
    }
}

Related

  1. reverse(List list)
  2. reverse(List a)
  3. reverse(List l)
  4. reverse(List list)
  5. reverse(List list)
  6. reverse(List list)
  7. reverse(List list)
  8. reverse(List list)
  9. reverse(List src, List dst)