0
点赞
收藏
分享

微信扫一扫

Zset如何操作Redis--基于Jedis,Java版

八怪不姓丑 2022-03-21 阅读 41

文章目录

1、Zset概念

2、Zset语法

3、Zset在Jedis的示例


public class TestZset {
	public static void main(String[] args) {
		// 创建jedis对象
		Jedis jedis = new Jedis("localhost",6379);  // 如果你是用服务器,localhost改为服务器ip即可
		jedis.auth("12345688"); // 如果redis设置了密码验证,反之,则不需要该代码

		HashMap<String, Double> scoreMap = new HashMap<String, Double>();
		scoreMap.put("xiaoming",70.0);
		scoreMap.put("xiaowang",100.0);
		scoreMap.put("xiaohong",88.0);
		scoreMap.put("xiaoli",60.0);
		scoreMap.put("zhangsan",58.0);
		jedis.zadd("math", scoreMap);
		// 查看scoreMap的形式
		System.out.println("查看scoreMap的形式:"+scoreMap.toString());
		// 0 第0个元素,-1最后一个元素
		System.out.println("返回math全部元素:"+jedis.zrange("math", 0, -1));
		System.out.println("查看key有多少个元素:"+jedis.zcard("math"));
		// 移除 xiaoli 这个元素
		System.out.println("移除xiaoli 这个元素");
		jedis.zrem("math", "xiaoli");
		// -inf 负无穷  inf 正无穷,即从小到大排序
		System.out.println("按照递增顺序,返回math全部的元素(含成绩):"+jedis.zrangeByScoreWithScores("math", "-inf", "inf"));
		System.out.println("统计math集合,成绩在[80,100]之间的元素个数:"+jedis.zcount("math", 80,100 ));
	}
}

结果演示:在这里插入图片描述

举报

相关推荐

0 条评论