Java List Reverse reverse(List list)

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

Description

Reverse a list.

License

Apache License

Parameter

Parameter Description
list The list to reverse.

Declaration

public static void reverse(List<?> list) 

Method Source Code


//package com.java2s;
/*/*from w ww .  j a  v a2  s. c o  m*/
 * Copyright JTheque (Baptiste Wicht)
 *
 * 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.
 */

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Map;
import java.util.Map.Entry;

public class Main {
    /**
     * Reverse the order of a map. This method provide correct result only for map who retain the insertion order.
     *
     * @param map The map to reverse.
     * @param <T> The Key type.
     * @param <K> The value type.
     */
    public static <T, K> void reverse(Map<T, K> map) {
        List<Entry<T, K>> entries = new ArrayList<Entry<T, K>>(map.entrySet());

        map.clear();

        for (int i = entries.size() - 1; i >= 0; i--) {
            Entry<T, K> entry = entries.get(i);

            map.put(entry.getKey(), entry.getValue());
        }
    }

    /**
     * Reverse a list.
     *
     * @param list The list to reverse.
     */
    public static void reverse(List<?> list) {
        Collections.reverse(list);
    }
}

Related

  1. reverse(final List list)
  2. reverse(final List list)
  3. reverse(final List strings)
  4. reverse(List aList)
  5. reverse(List list)
  6. reverse(List as)
  7. reverse(List list)
  8. reverse(List source)
  9. reverse(List list)

    HOME | Copyright © www.java2s.com 2016