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

com.jchanghong.core.map.TableMap Maven / Gradle / Ivy

The newest version!
package com.jchanghong.core.map;

import com.jchanghong.core.collection.CollUtil;
import com.jchanghong.core.collection.ListUtil;
import com.jchanghong.core.util.ObjectUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 可重复键和值的Map
* 通过键值单独建立List方式,使键值对一一对应,实现正向和反向两种查找
* 无论是正向还是反向,都是遍历列表查找过程,相比标准的HashMap要慢,数据越多越慢 * * @param 键类型 * @param 值类型 * @author looly */ public class TableMap implements Map, Iterable>, Serializable { private static final long serialVersionUID = 1L; private final List keys; private final List values; /** * 构造 * * @param size 初始容量 */ public TableMap(int size) { this.keys = new ArrayList<>(size); this.values = new ArrayList<>(size); } /** * 构造 * * @param keys 键列表 * @param values 值列表 */ public TableMap(K[] keys, V[] values) { this.keys = CollUtil.toList(keys); this.values = CollUtil.toList(values); } @Override public int size() { return keys.size(); } @Override public boolean isEmpty() { return CollUtil.isEmpty(keys); } @Override public boolean containsKey(Object key) { //noinspection SuspiciousMethodCalls return keys.contains(key); } @Override public boolean containsValue(Object value) { //noinspection SuspiciousMethodCalls return values.contains(value); } @Override public V get(Object key) { //noinspection SuspiciousMethodCalls final int index = keys.indexOf(key); if (index > -1 && index < values.size()) { return values.get(index); } return null; } /** * 根据value获得对应的key,只返回找到的第一个value对应的key值 * @param value 值 * @return 键 * @since 5.3.3 */ public K getKey(V value){ final int index = values.indexOf(value); if (index > -1 && index < keys.size()) { return keys.get(index); } return null; } /** * 获取指定key对应的所有值 * * @param key 键 * @return 值列表 * @since 5.2.5 */ public List getValues(K key) { return CollUtil.getAny( this.values, ListUtil.indexOfAll(this.keys, (ele) -> ObjectUtil.equal(ele, key)) ); } /** * 获取指定value对应的所有key * * @param value 值 * @return 值列表 * @since 5.2.5 */ public List getKeys(V value) { return CollUtil.getAny( this.keys, ListUtil.indexOfAll(this.values, (ele) -> ObjectUtil.equal(ele, value)) ); } @Override public V put(K key, V value) { keys.add(key); values.add(value); return null; } @Override public V remove(Object key) { //noinspection SuspiciousMethodCalls int index = keys.indexOf(key); if (index > -1) { keys.remove(index); if (index < values.size()) { values.remove(index); } } return null; } @Override public void putAll(Map m) { for (Map.Entry entry : m.entrySet()) { this.put(entry.getKey(), entry.getValue()); } } @Override public void clear() { keys.clear(); values.clear(); } @Override public Set keySet() { return new HashSet<>(keys); } @Override public Collection values() { return Collections.unmodifiableList(this.values); } @Override public Set> entrySet() { final Set> hashSet = new LinkedHashSet<>(); for (int i = 0; i < size(); i++) { hashSet.add(new Entry<>(keys.get(i), values.get(i))); } return hashSet; } @Override public Iterator> iterator() { return new Iterator>() { private final Iterator keysIter = keys.iterator(); private final Iterator valuesIter = values.iterator(); @Override public boolean hasNext() { return keysIter.hasNext() && valuesIter.hasNext(); } @Override public Map.Entry next() { return new Entry<>(keysIter.next(), valuesIter.next()); } @Override public void remove() { keysIter.remove(); valuesIter.remove(); } }; } @Override public String toString() { return "TableMap{" + "keys=" + keys + ", values=" + values + '}'; } private static class Entry implements Map.Entry { private final K key; private final V value; public Entry(K key, V value) { this.key = key; this.value = value; } @Override public K getKey() { return key; } @Override public V getValue() { return value; } @Override public V setValue(V value) { throw new UnsupportedOperationException("setValue not supported."); } @Override public final boolean equals(Object o) { if (o == this) return true; if (o instanceof Map.Entry) { Map.Entry e = (Map.Entry) o; return Objects.equals(key, e.getKey()) && Objects.equals(value, e.getValue()); } return false; } @Override public int hashCode() { //copy from 1.8 HashMap.Node return Objects.hashCode(key) ^ Objects.hashCode(value); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy