util.collection.LPListMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xflp Show documentation
Show all versions of xflp Show documentation
Solver for realistic 3D container loading
package util.collection;
import util.Copyable;
import java.util.*;
/**
* Copyright (c) 2012-2022 Holger Schneider
* All rights reserved.
*
* This source code is licensed under the MIT License (MIT) found in the
* LICENSE file in the root directory of this source tree.
*
*
* @author Hogo
*
* @param Key
* @param Value
*/
public class LPListMap implements Copyable> {
private final HashMap> map;
/**
*
*/
public LPListMap() {
map = new HashMap<>();
}
public LPListMap(int size) {
map = new HashMap<>(size);
}
public void put(K key, E element) {
if(map.containsKey(key)) {
map.get(key).add(element);
} else {
List list = new ArrayList<>();
list.add(element);
map.put(key, list);
}
}
public void putSave(K key, E element) {
if(map.containsKey(key) && map.get(key) != null) {
map.get(key).add(element);
} else {
List list = new ArrayList<>();
list.add(element);
map.put(key, list);
}
}
public void put(K key, List list) {
if(map.containsKey(key) && map.get(key) != null)
map.get(key).addAll(list);
else
map.put(key, list);
}
public void putSave(K key, List list) {
if(list != null) {
if(map.containsKey(key) && map.get(key) != null)
map.get(key).addAll(list);
else
map.put(key, list);
}
}
public void remove(K key) {
map.remove(key);
}
public void remove(K key, E element) {
if(map.containsKey(key))
map.get(key).remove(element);
}
public void removeFully(K key, E element) {
if(map.containsKey(key)) {
map.get(key).remove(element);
if(map.get(key).isEmpty())
remove(key);
}
}
public List get(K key) {
return map.get(key);
}
public boolean containsKey(K key) {
return map.containsKey(key);
}
public Set keySet() {
return map.keySet();
}
public int size() {
return map.size();
}
public Iterator iterator(K key) {
if(map.containsKey(key))
return map.get(key).iterator();
return null;
}
@Override
public LPListMap copy() {
LPListMap copy = new LPListMap<>(size());
for (K key : keySet()) {
List list = get(key);
List newList = new ArrayList<>(list.size());
Collections.copy(newList, list);
copy.put(key, newList);
}
return copy;
}
public void removeAll(List list, E e) {
for (K k : list)
removeFully(k, e);
}
public void put(List list, E e) {
for (K k : list)
put(k, e);
}
public void putSave(List list, E e) {
for (K k : list)
putSave(k, e);
}
public List getSave(K key) {
List l = null;
if(map.containsKey(key)) {
l = map.computeIfAbsent(key, k -> new ArrayList<>());
}
return l;
}
public List getSave2(K key) {
if(map.containsKey(key)) {
return map.computeIfAbsent(key, k -> new ArrayList<>());
}
return new ArrayList<>();
}
public void clear() {
map.clear();
}
}