Here you can find the source of reverse(Map
Parameter | Description |
---|---|
in | The map to reverse the order on. |
public static <K, V> LinkedHashMap<K, V> reverse(Map<K, V> in)
//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; } }