com.landawn.abacus.util.SetMultimap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-util-all Show documentation
Show all versions of abacus-util-all Show documentation
A general programming library in Java
/*
* 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.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.landawn.abacus.util.function.Function;
/**
*
* @since 0.9
*
* @author Haiyang Li
* @see N#newSetMultimap()
*/
public final class SetMultimap extends Multimap> {
SetMultimap() {
this(HashMap.class, HashSet.class);
}
SetMultimap(int initialCapacity) {
this(new HashMap>(initialCapacity), HashSet.class);
}
@SuppressWarnings("rawtypes")
SetMultimap(final Class extends Map> mapType, final Class extends Set> valueType) {
super(mapType, valueType);
}
@SuppressWarnings("rawtypes")
SetMultimap(final Map> valueMap, final Class extends Set> valueType) {
super(valueMap, valueType);
}
public static SetMultimap from(final Map extends K, ? extends E> map) {
final SetMultimap multimap = new SetMultimap<>(N.initHashCapacity(map == null ? 0 : map.size()));
if (N.notNullOrEmpty(map)) {
multimap.putAll(map);
}
return multimap;
}
public static SetMultimap from2(final Map extends K, ? extends Collection extends E>> map) {
final SetMultimap multimap = new SetMultimap<>(N.initHashCapacity(map == null ? 0 : map.size()));
if (N.notNullOrEmpty(map)) {
for (Map.Entry extends K, ? extends Collection extends E>> entry : map.entrySet()) {
multimap.putAll(entry.getKey(), entry.getValue());
}
}
return multimap;
}
public static SetMultimap from(final Collection extends E> c, final Function super E, ? extends K> keyExtractor) {
final SetMultimap multimap = N.newSetMultimap(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 > SetMultimap wrap(final Map map) {
Class extends Set> valueType = HashSet.class;
for (V v : map.values()) {
if (v != null) {
valueType = v.getClass();
break;
}
}
return new SetMultimap((Map>) map, valueType);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy