All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.landawn.abacus.util.ListMultimap Maven / Gradle / Ivy

There is a newer version: 1.2.9
Show newest version
/*
 * Copyright (C) 2017 HaiYang Li
 *
 * 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.landawn.abacus.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.landawn.abacus.util.function.Function;

/**
 * 
 * @since 0.9
 * 
 * @author Haiyang Li
 * @see N#newListMultimap()
 */
public final class ListMultimap extends Multimap> {
    ListMultimap() {
        this(HashMap.class, ArrayList.class);
    }

    ListMultimap(int initialCapacity) {
        this(new HashMap>(initialCapacity), ArrayList.class);
    }

    @SuppressWarnings("rawtypes")
    ListMultimap(final Class mapType, final Class valueType) {
        super(mapType, valueType);
    }

    @SuppressWarnings("rawtypes")
    ListMultimap(final Map> valueMap, final Class valueType) {
        super(valueMap, valueType);
    }

    public static  ListMultimap from(final Map map) {
        final ListMultimap multimap = new ListMultimap<>(N.initHashCapacity(map == null ? 0 : map.size()));

        if (N.notNullOrEmpty(map)) {
            multimap.putAll(map);
        }

        return multimap;
    }

    public static  ListMultimap from2(final Map> map) {
        final ListMultimap multimap = new ListMultimap<>(N.initHashCapacity(map == null ? 0 : map.size()));

        if (N.notNullOrEmpty(map)) {
            for (Map.Entry> entry : map.entrySet()) {
                multimap.putAll(entry.getKey(), entry.getValue());
            }
        }

        return multimap;
    }

    public static  ListMultimap from(final Collection c, final Function keyExtractor) {
        final ListMultimap multimap = N.newListMultimap(N.initHashCapacity(N.min(9, c == null ? 0 : c.size())));

        if (N.notNullOrEmpty(c)) {
            for (E e : c) {
                multimap.put(keyExtractor.apply(e), e);
            }
        }

        return multimap;
    }

    @SuppressWarnings("rawtypes")
    public static > ListMultimap wrap(final Map map) {
        Class valueType = ArrayList.class;

        for (V v : map.values()) {
            if (v != null) {
                valueType = v.getClass();
                break;
            }
        }

        return new ListMultimap((Map>) map, valueType);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy