Java Map Reverse reverse(Map in)

Here you can find the source of reverse(Map in)

Description

Reverses the order of a LinkedHashMap .

License

Apache License

Parameter

Parameter Description
in The map to reverse the order on.

Return

A new with keys in reverse order.

Declaration

public static <K, V> LinkedHashMap<K, V> reverse(Map<K, V> in) 

Method Source Code


//package com.java2s;
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *

import java.util.*;

public class Main {
    /**/*from w  w  w  .j  ava 2  s .c om*/
     * Reverses the order of a {@link LinkedHashMap}.
     *
     * @param in The map to reverse the order on.
     * @return A new {@link LinkedHashMap} with keys in reverse order.
     */
    public static <K, V> LinkedHashMap<K, V> reverse(Map<K, V> in) {
        if (in == null)
            return null;
        LinkedHashMap<K, V> m = new LinkedHashMap<K, V>();

        // Note:  Entry objects are reusable in an entry set, so we simply can't
        // create a reversed iteration of that set.
        List<K> keys = new ArrayList<K>(in.keySet());
        List<V> values = new ArrayList<V>(in.values());
        for (int i = in.size() - 1; i >= 0; i--)
            m.put(keys.get(i), values.get(i));

        return m;
    }
}

Related

  1. reverse( Map map)
  2. reverse(final Map map)
  3. reverse(Map source, Map target)
  4. reverse(Map source, Map target)
  5. reverse(Map kvMap)
  6. reverse(Map map)
  7. reverse(Map map)
  8. reverse(Map source, Map target)