在Java中使用ZSet(有序集合)進行范圍查詢可以通過以下步驟實現:
Jedis jedis = new Jedis("localhost", 6379);
String key = "myZSet";
jedis.zadd(key, 1.0, "value1");
jedis.zadd(key, 2.0, "value2");
jedis.zadd(key, 3.0, "value3");
Set<String> result = jedis.zrangeByScore(key, 1.0, 2.0);
System.out.println(result);
Set<Tuple> resultWithScores = jedis.zrangeByScoreWithScores(key, 1.0, 2.0);
for (Tuple tuple : resultWithScores) {
System.out.println(tuple.getElement() + " : " + tuple.getScore());
}
jedis.close();
通過以上步驟,可以使用Java的Jedis庫進行ZSet的范圍查詢操作。