Java tutorial
//package com.java2s; /* * JPPF. * Copyright (C) 2005-2010 JPPF Team. * http://www.jppf.org * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.*; public class Main { /** * Format a string with size information about a map whose values are lists of elements. * @param <T> the type of the keys in the map. * @param <U> the type of the values in the map. * @param name an arbitrary name given to the map. * @param map the map from which to get size information. * @return a string containing information about the number of elements in the map. */ public static <T, U> String formatSizeMapInfo(String name, Map<T, List<U>> map) { StringBuilder sb = new StringBuilder(); sb.append(name).append("[shallow size=").append(map.size()); sb.append(", total elements=").append(sizeOfListMap(map)).append("]"); return sb.toString(); } /** * Get the total number of elements in a map whose values are lists of elements. * @param <T> the type of the keys in the map. * @param <U> the type of the elements in the lists. * @param map the map of which to get the size. * @return the size of the map as an int value. */ public static <T, U> int sizeOfListMap(Map<T, List<U>> map) { int result = 0; for (Map.Entry<T, List<U>> entry : map.entrySet()) result += entry.getValue().size(); return result; } }