RedisTemplate map集合使用说明-opsForHash(三)

1、put([H](http://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/HashOperations.html)?key,``[HK](http://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/HashOperations.html)?hashKey,``[HV](http://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/HashOperations.html)?value)

新增hashMap值。

Java代码收藏代码

  1. redisTemplate.opsForHash().put(“hashValue”,“map1”,“map1-1”);
  2. redisTemplate.opsForHash().put(“hashValue”,“map2”,“map2-2”);

2、values(Hkey)

获取指定变量中的hashMap值。

Java代码收藏代码

  1. ListhashList=redisTemplate.opsForHash().values(“hashValue”);
  2. System.out.println(“通过values(Hkey)方法获取变量中的hashMap值:”+hashList);

3、entries(Hkey)

获取变量中的键值对。

Java代码收藏代码

  1. Map<Object,Object>map=redisTemplate.opsForHash().entries(“hashValue”);
  2. System.out.println(“通过entries(Hkey)方法获取变量中的键值对:”+map);

4、get(Hkey,ObjecthashKey)

获取变量中的指定map键是否有值,如果存在该map键则获取值,没有则返回null。

Java代码收藏代码

  1. ObjectmapValue=redisTemplate.opsForHash().get(“hashValue”,“map1”);
  2. System.out.println(“通过get(Hkey,ObjecthashKey)方法获取map键的值:”+mapValue);

5、hasKey([H](http://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/HashOperations.html)?key,``[Object](https://docs.oracle.com/javase/6/docs/api/java/lang/Object.htmlis-external=true)?hashKey)

判断变量中是否有指定的map键。

Java代码收藏代码

  1. booleanhashKeyBoolean=redisTemplate.opsForHash().hasKey(“hashValue”,“map3”);
  2. System.out.println(“通过hasKey(Hkey,ObjecthashKey)方法判断变量中是否存在map键:”+hashKeyBoolean);

6、keys(Hkey)

获取变量中的键。

Java代码收藏代码

  1. SetkeySet=redisTemplate.opsForHash().keys(“hashValue”);
  2. System.out.println(“通过keys(Hkey)方法获取变量中的键:”+keySet);

7、size(Hkey)

获取变量的长度。

Java代码收藏代码

  1. longhashLength=redisTemplate.opsForHash().size(“hashValue”);
  2. System.out.println(“通过size(Hkey)方法获取变量的长度:”+hashLength);

8、increment(Hkey,HKhashKey, doubledelta)

使变量中的键以double值的大小进行自增长。

Java代码收藏代码

  1. doublehashIncDouble=redisTemplate.opsForHash().increment(“hashInc”,“map1”,3);
  2. System.out.println(“通过increment(Hkey,HKhashKey,doubledelta)方法使变量中的键以值的大小进行自增长:”+hashIncDouble);

9、increment(Hkey,HKhashKey, longdelta)

使变量中的键以long值的大小进行自增长。

Java代码收藏代码

  1. longhashIncLong=redisTemplate.opsForHash().increment(“hashInc”,“map2”,6);
  2. System.out.println(“通过increment(Hkey,HKhashKey,longdelta)方法使变量中的键以值的大小进行自增长:”+hashIncLong);

10、multiGet([H](http://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/HashOperations.html)?key,``[Collection](https://docs.oracle.com/javase/6/docs/api/java/util/Collection.htmlis-external=true)<[HK](http://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/HashOperations.html)>?hashKeys)

以集合的方式获取变量中的值。

Java代码收藏代码

  1. Listlist=newArrayList();
  2. list.add(“map1”);
  3. list.add(“map2”);
  4. ListmapValueList=redisTemplate.opsForHash().multiGet(“hashValue”,list);
  5. System.out.println(“通过multiGet(Hkey,CollectionhashKeys)方法以集合的方式获取变量中的值:”+mapValueList);

11、putAll(Hkey,Map< extendsHK, extendsHV>m)

以map集合的形式添加键值对。

Java代码收藏代码

  1. MapnewMap=newHashMap();
  2. newMap.put(“map3”,“map3-3”);
  3. newMap.put(“map5”,“map5-5”);
  4. redisTemplate.opsForHash().putAll(“hashValue”,newMap);
  5. map=redisTemplate.opsForHash().entries(“hashValue”);
  6. System.out.println(“通过putAll(Hkey,Map<extendsHK,extendsHV>m)方法以map集合的形式添加键值对:”+map);

12、putIfAbsent(Hkey,HKhashKey,HVvalue)

如果变量值存在,在变量中可以添加不存在的的键值对,如果变量不存在,则新增一个变量,同时将键值对添加到该变量。

Java代码收藏代码

  1. redisTemplate.opsForHash().putIfAbsent(“hashValue”,“map6”,“map6-6”);
  2. map=redisTemplate.opsForHash().entries(“hashValue”);
  3. System.out.println(“通过putIfAbsent(Hkey,HKhashKey,HVvalue)方法添加不存在于变量中的键值对:”+map);

13、scan(Hkey,ScanOptionsoptions)

匹配获取键值对,ScanOptions.NONE为获取全部键对,ScanOptions.scanOptions().match(“map1”).build() 匹配获取键位map1的键值对,不能模糊匹配。

Java代码收藏代码

  1. Cursor<Map.Entry<Object,Object>>cursor=redisTemplate.opsForHash().scan(“hashValue”,ScanOptions.scanOptions().match(“map1”).build());
  2. //Cursor<Map.Entry<Object,Object>>cursor=redisTemplate.opsForHash().scan(“hashValue”,ScanOptions.NONE);
  3. while(cursor.hasNext()){
  4. Map.Entry<Object,Object>entry=cursor.next();
  5. System.out.println(“通过scan(Hkey,ScanOptionsoptions)方法获取匹配键值对:”+entry.getKey()+“---->”+entry.getValue());
  6. }

14、delete(Hkey,Object…hashKeys)

删除变量中的键值对,可以传入多个参数,删除多个键值对。

Java代码收藏代码

  1. redisTemplate.opsForHash().delete(“hashValue”,“map1”,“map2”);
  2. map=redisTemplate.opsForHash().entries(“hashValue”);
  3. System.out.println(“通过delete(Hkey,Object…hashKeys)方法删除变量中的键值对后剩余的:”+map);

;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值