集册 Java实例教程 从映射创建不可修改的映射

从映射创建不可修改的映射

欢马劈雪     最近更新时间:2020-01-02 10:19:05

393
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
从映射创建不可修改的映射

/*
来 自*
 nowjava
*/

//package com.nowjava;


import java.util.Collections;

import java.util.HashMap;


import java.util.Map;

import java.util.Map.Entry;


public class Main {

    public static void main(String[] argv) {

        System.out.println(map());

    }


    /**

     * @param entries the <i>final</i> set of entries to add to the newly created <i>unmodifiable</i> map

     * @return an <i>unmodifiable</i> map with all given entries

     */

    public static <K, V> Map<K, V> map(final Entry<K, V>... entries) {

        final HashMap<K, V> map = new HashMap<K, V>(entries.length);

        for (final Entry<K, V> entry : entries) {

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

        }

        return Collections.unmodifiableMap(map);

    }//时代Java - nowjava.com 提供


    public static <K, V> Map<K, V> map() {

        return new HashMap<K, V>();

    }


    /**

     * @return an <b>UNMODIFIABLE</b> Map&lt;K, V&gt;

     */

    public static <K, V> Map<
展开阅读全文