Add all the entries in the List as keys in the specified map. - Java java.util

Java examples for java.util:List Operation

Description

Add all the entries in the List as keys in the specified map.

Demo Code

/*//w w  w  .j  av  a  2 s  .c  om
 * Copyright 2006 - Gary Bentley
 *
 * 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.
 */
//package com.java2s;
import java.util.Map;
import java.util.List;

public class Main {
    /**
     * Add all the entries in the List as keys in the specified map.
     * Set "keyIsValue" to <code>true</code> to put the entry from
     * the List as the value for the map as well, if it's set to
     * <code>false</code> then we use "".
     *
     * @param list The List to get entries from.
     * @param map The Map to add entries to.
     * @param keyIsValue Indicate whether we should use the List entry
     *                   as the value as well or "".
     */
    public static void addListEntriesToMap(List list, Map map,
            boolean keyIsValue) {

        for (int i = 0; i < list.size(); i++) {

            if (keyIsValue) {

                map.put(list.get(i), list.get(i));

            } else {

                map.put(list.get(i), "");

            }

        }

    }
}

Related Tutorials