自定义有序Map

java 复制代码
package cn.ziqirj.common.utils;

import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * 模拟Map集合,key不可重复,按插入顺序排序
 * @author zhangji
 *
 * @param <T>
 */
public class CustomOrderlyMap<T> {
	@Getter
    private List<String> keys = new ArrayList<>();
	@Getter
    private List<T> values = new ArrayList<>();
	private List<map<T>> kv = new ArrayList<>();
	
	public CustomOrderlyMap() {}

	/**
	 * 添加元素
	 * @param key 键
	 * @param value	值
	 */
	public void add(String key, T value) {
		Integer index = getIndex(key);
		if(null != index && index != -1) {
			values.set(index, value);
			kv.get(index).setValue(value);
			return ;
		}
		keys.add(key);
		values.add(value);
		kv.add(new map<T>(key, value));
	}

	/**
	 * 批量添加元素
	 * @param customMap 集合
	 */
	public void addAll(CustomOrderlyMap<T> customMap) {
		if(null == customMap || customMap.size() == 0) {
            return ;
        }
		List<map<T>> kvl = customMap.kv;
		for(map<T> map : kvl) {
            this.add(map.getKey(), map.getValue());
        }
	}

	/**
	 * 移除元素
	 * @param key 键
	 */
	public void remove(String key) {
		Integer index = getIndex(key);
		if(null == index || index == -1 ) {
            return ;
        }
		keys.remove(index);
		values.remove(index);
		kv.remove(index);
	}
	
	/**
	 * 移除元素
	 * @param obj 值
	 */
	public void remove(T obj) {
		Integer index = getIndex(obj);
		if(null == index || index == -1 ) {
            return ;
        }
		keys.remove(index);
		values.remove(index);
		kv.remove(index);
	}

	/**
	 * 清空集合
	 */
	public void removeAll() {
		this.keys = new ArrayList<>();
		this.values = new ArrayList<>();
		this.kv = new ArrayList<>();
	}
	
	/**
	 * 关闭集合
	 */
	public void close() {
		this.keys.clear();
		this.values.clear();
		this.kv.clear();
	}

	/**
	 * 设置元素
	 * @param key 键
	 * @param value 值
	 */
	public void set(String key, T value) {
		Integer index = getIndex(key);
		if(null == index || index.intValue() == -1)
			return ;
		values.set(index, value);
		kv.get(index).setValue(value);
	}

	/**
	 * 获取元素
	 * @param key 键
	 * @return T
	 */
	public T get(String key) {
		Integer index = getIndex(key);
		if (null == index || index == -1)
			return null;
		return kv.get(index).getValue();
	}

	/**
	 * 获取集合大小
	 * @return Integer
	 */
    public Integer size() {
		return this.kv.size();
	}

	/**
	 * 判断集合是否为空
	 * @return boolean
	 */
	public boolean isEmpty() {
		return this.kv.isEmpty();
	}
	
	/**
	 * 打印集合
	 */
	public void print() {
		if(isEmpty()) {
            System.out.println("null");
        }
		System.out.print("CustomMap : [");
		for(map<T> m : this.kv) {
			System.out.print(m.toString());
		}
		System.out.println("]");
	}

	/**
	 * 获取key的索引
	 * @param key 键
	 * @return Integer
	 */
	private Integer getIndex(String key) {
		int length = this.keys.size();
		if (length == 0) {
            return -1;
        }
		for (int i = 0; i < length; i++) {
            if (key.equals(keys.get(i))) {
                return i;
            }
        }
		return null;
	}
	
	/**
	 * 获取value的索引
	 * @param t 值
	 * @return Integer
	 */
	private Integer getIndex(T t) {
		int length = this.values.size();
		if (length == 0) {
            return -1;
        }
		for (int i = 0; i < length; i++) {
            if (t.equals(values.get(i))) {
                return i;
            }
        }
		return null;
	}

}

@Setter
@Getter
class map<T> {
	private String key;
	private T value;

	public map(String key, T value) {
		super();
		this.key = key;
		this.value = value;
	}

    @Override
	public String toString() {
		return "[K:" + key + " --> V:" + value + "]";
	}

}

个人博客:紫琪软件工作室

相关推荐
isysc15 小时前
面了一个校招生,竟然说我是老古董
java·后端·面试
道可到9 小时前
Java 反射现代实践速查表(JDK 11+/17+)
java
道可到9 小时前
Java 反射现代实践指南(JDK 11+ / 17+ 适用)
java
玉衡子9 小时前
九、MySQL配置参数优化总结
java·mysql
叽哥9 小时前
Kotlin学习第 8 课:Kotlin 进阶特性:简化代码与提升效率
android·java·kotlin
麦兜*9 小时前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
带刺的坐椅9 小时前
DamiBus v1.1.0 发布(给单体多模块解耦)
java·事件总线·damibus
葡萄城技术团队9 小时前
用 Java 构建健壮 REST API 的 4 个关键技巧
java
杨杨杨大侠9 小时前
解密 atlas-mapper 框架 (9/10):故障排查与调试技巧
java·开源·github
Slaughter信仰9 小时前
深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第十章知识点问答(10题)
java·jvm·数据库