当前位置: 首页 > news >正文

自定义有序Map

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> {@Getterprivate List<String> keys = new ArrayList<>();@Getterprivate 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;}@Overridepublic String toString() {return "[K:" + key + " --> V:" + value + "]";}}

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

http://www.lryc.cn/news/515589.html

相关文章:

  • Jenkins(持续集成与自动化部署)
  • redis7基础篇2 redis的哨兵模式2
  • windows终端conda activate命令行不显示环境名
  • SpringBoot 2.6 集成es 7.17
  • 加固服务器有什么用?
  • Personal APP
  • 探索最新的编程技术趋势:AI 编程助手和未来的编程方式
  • Android:文件管理:打开文件意图
  • 从纯虚类到普通类:提升C++ ABI兼容性的策略
  • QT中如何限制 限制QLineEdit只能输入字母,或数字,或某个范围内数字等限制约束?
  • Tailwind CSS 使用简介
  • iOS 逆向学习 - iOS Architecture Cocoa Touch Layer
  • C语言实现库函数strlen
  • 050_小驰私房菜_MTK Camera debug, data rate 、mipi_pixel_rate 确认
  • (六)vForm 动态表单(数据量大,下拉选卡顿问题)
  • 【mybatis-plus问题集锦系列】mybatis使用xml配置文件实现数据的基础增删改查
  • 投稿指南【NO.12_14】【极易投中】期刊投稿(毛纺科技)
  • 机器学习算法的分类
  • Linux操作系统下,挂ILA
  • HTML——26.像素单位
  • 【HTML】Day02
  • AI 自动化编程对编程教育的影响
  • Java100道面试题
  • 解密人工智能:如何改变我们的工作与生活
  • Linux postgresql-15部署文档
  • visual studio 安全模式
  • Pandas-timestamp和datetime64的区别
  • @MapperScan
  • SQL中聚类后字段数据串联字符串方法研究
  • 【嵌入式硬件】直流电机驱动相关