java map string long_Java HashMap使用String,Long,Integer作为key的性能测试

package com.test;

import java.util.HashMap;

import java.util.Map;

/**@类名 MapKeyTest

* @说明

* @作者 Fabuler

* @日期 2017年2月16日-下午6:18:09

*/

public class MapKeyTest {

public static void main(String[] args) {

Mapm=new HashMap<>();

Mapm2=new HashMap<>();

Mapm3=new HashMap<>();

long limit=10000000;

long t;

t=System.currentTimeMillis();

for(long i=0;i

m.put("address", "name");

}

System.out.println("耗时"+(System.currentTimeMillis()-t)+"ms");

t=System.currentTimeMillis();

for(long i=0;i

m2.put(123165465l, "name");

}

System.out.println("耗时"+(System.currentTimeMillis()-t)+"ms");

t=System.currentTimeMillis();

for(long i=0;i

m3.put(123165465, "name");

}

System.out.println("耗时"+(System.currentTimeMillis()-t)+"ms");

t=System.currentTimeMillis();

for(long i=0;i

m.put("address"+"-"+i, "name");

}

System.out.println("耗时"+(System.currentTimeMillis()-t)+"ms");

t=System.currentTimeMillis();

for(long i=0;i

m2.put(123165465l|i, "name");

}

System.out.println("耗时"+(System.currentTimeMillis()-t)+"ms");

t=System.currentTimeMillis();

for(long i=0;i

m3.put(123165465|(int)i, "name");

}

System.out.println("耗时"+(System.currentTimeMillis()-t)+"ms");

}

}

运行结果

耗时33ms

耗时96ms

耗时99ms

耗时12265ms

耗时551ms

耗时230ms

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Redis的Hashes类型来存储这个Map<Long, Map<Integer, Integer>>。Hashes类型可以将多个key-value存储在同一个Redis键下,非常适合存储类似于Map的数据结构。 以下是Java代码示例: ```java // 导入Redis客户端依赖 import redis.clients.jedis.Jedis; // 创建Redis客户端连接,这里使用本地默认端口号6379 Jedis jedis = new Jedis("localhost", 6379); // 定义需要存储的Map Map<Long, Map<Integer, Integer>> map = new HashMap<>(); map.put(1L, new HashMap<Integer, Integer>(){{put(2, 3); put(4, 5);}}); map.put(6L, new HashMap<Integer, Integer>(){{put(7, 8); put(9, 10);}}); // 将Map存储到Redis中 for (Long key : map.keySet()) { // 构造Redis键 String redisKey = "myHash:" + key.toString(); // 获取当前key对应的Map Map<Integer, Integer> subMap = map.get(key); // 将Map转为Redis Hashes类型中的Map Map<String, String> redisSubMap = new HashMap<>(); for (Integer subKey : subMap.keySet()) { redisSubMap.put(subKey.toString(), subMap.get(subKey).toString()); } // 存储到Redis中 jedis.hmset(redisKey, redisSubMap); } // 从Redis中获取Map Map<Long, Map<Integer, Integer>> resultMap = new HashMap<>(); Set<String> keySet = jedis.keys("myHash:*"); for (String redisKey : keySet) { // 解析Redis键中的key Long key = Long.parseLong(redisKey.split(":")[1]); // 获取Redis中存储的Map Map<String, String> redisSubMap = jedis.hgetAll(redisKey); // 将Redis Hashes类型中的Map转为Map<Integer, Integer> Map<Integer, Integer> subMap = new HashMap<>(); for (String subKey : redisSubMap.keySet()) { subMap.put(Integer.parseInt(subKey), Integer.parseInt(redisSubMap.get(subKey))); } resultMap.put(key, subMap); } // 打印结果 System.out.println(resultMap); ``` 输出结果: ``` {1={2=3, 4=5}, 6={9=10, 7=8}} ``` 注意事项: - 在存储Map时,需要将Map中的每个key-value对应到一个Redis键中。这里使用了“myHash:”作为Redis键的前缀,后面接上Map中的Long类型key。 - 在存储Map时,需要将Map中的每个value转为Redis Hashes类型中的Map,这里使用Map<String, String>类型来存储Redis Hashes类型中的Map。 - 在从Redis中获取Map时,需要遍历所有的Redis键,并将Redis Hashes类型中的Map转为Map<Integer, Integer>类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值