Java tutorial
//package com.java2s; /* * USE - UML based specification environment * Copyright (C) 1999-2004 Mark Richters, University of Bremen * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ import java.util.HashMap; import java.util.Map; public class Main { /** * Returns the same map <code>theMap</code> if it has elements, * otherwise it returns a new <code>HashMap</code>. * Can be used in combination with {@link CollectionUtil#emptyMapIfNull(List)} to * use a singleton empty map as initial value. * <p> * <strong>Pre:</strong> <code>theMap != null</code> * </p> * <p> * <strong>Post:</strong> <code>result</code> is writable. * @param <TK> Element type of the keys * @param <TV> Element type of the values * @param theList A <code>List</code> * @return The same <code>Map</code> if <code>theMap.size() > 0</code> or a new <code>HashMap</code> */ public static <TK, TV> Map<TK, TV> initAsHashMap(final Map<TK, TV> theMap) { if (theMap.size() == 0) return new HashMap<TK, TV>(); else return theMap; } }