Android examples for java.util:Map
new Read Only Map
/*/* ww w . j a v a2s . c o m*/ * -------------------- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of the Common Development * and Distribution License("CDDL") (the "License"). You may not use this file * except in compliance with the License. * * You can obtain a copy of the License at * http://opensource.org/licenses/cddl1.php * See the License for the specific language governing permissions and limitations * under the License. * * When distributing the Covered Code, include this CDDL Header Notice in each file * and include the License file at http://opensource.org/licenses/cddl1.php. * If applicable, add the following below this CDDL Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * -------------------- */ //package com.book2s; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; public class Main { public static <T, K> Map<T, K> newReadOnlyMap(Map<T, K> map) { return Collections.unmodifiableMap(new HashMap<T, K>( nullAsEmpty(map))); } public static <T> Map<T, T> newReadOnlyMap(T[][] kv) { Map<T, T> map = new HashMap<T, T>(); for (int i = 0; kv != null && i < kv.length; i++) { T key = kv[i][0]; T value = kv[i][1]; map.put(key, value); } return Collections.<T, T> unmodifiableMap(map); } /** * Protects from <code>null</code> and returns a new instance of * {@link HashSet}. * * if the parameter <strong>c</strong> is <strong>null</strong>. * * @param c * collection to check * @param <T> * the type of the collection * @return if null new {@link HashSet} otherwise the parameter that was * passed in or */ public static <T> Collection<T> nullAsEmpty(Collection<T> c) { return c == null ? new HashSet<T>() : c; } /** * Protects from <code>null</code> and returns a new instance of * {@link HashMap} if the parameter <code>map</code> is <code>null</code>. * Otherwise return the parameter that was passed in. */ public static <T, K> Map<T, K> nullAsEmpty(Map<T, K> map) { return (map == null) ? new HashMap<T, K>() : map; } /** * Protects from <code>null</code> and returns a new instance of * {@link HashSet} if the parameter <code>set</code> is <code>null</code>. * Otherwise return the parameter that was passed in. */ public static <T> Set<T> nullAsEmpty(Set<T> set) { return (set == null) ? new HashSet<T>() : set; } /** * Protects from <code>null</code> and returns a new instance of * {@link ArrayList} if the parameter <code>list</code> is <code>null</code> * . Otherwise return the parameter that was passed in. */ public static <T> List<T> nullAsEmpty(final List<T> list) { return (list == null) ? new ArrayList<T>() : list; } }