「这是我参与2022首次更文挑战的第27天,活动详情查看:2022首次更文挑战」
Redis在Java中Jedis的操作(Hash/List操作)
对List操作的命令
-
添加一个List:
jedis.lpush -
从左侧移除数据:
jedis.lpop -
从右侧移除数据:
jedis.rpop -
查看指定范围的list内容:
jedis.lrange -
移除指定个数的元素:
jedis.lrem -
移除指定范围的内容:
jedis.ltrim -
设置指定索引的内容:
jedis.lset -
显示list长度(数据个数):
jedis.llen -
获取指定下标的元素内容:
jedis.lindex -
list排序:
jedis.sort
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.flushDB();
System.out.println("========添加一个list========" );
jedis.lpush(" collections", "ArrayList", "Vector", "Stack","HashMap","weakHashMap","LinkedHashMap","");
jedis.lpush("collections","HashSet");
jedis.lpush("colllections","TreeSet");
jedis.lpush("collections","TreeMap");
//-1代表倒数第一个元素,0代表第一个元素
System.out.println("collections的内容: "+jedis.lrange("collections",0,-1));
System.out . println("collections区间0-3的元素:"+jedis.lrange("collections" ,0,3));
System.out.println( "=============================" );
//删除列表指定的值,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈
System.out .println("删除指定元素个数:"+jedis.lrem("collections",2,"HashMap"));
System.out.println( " collections的内容: "+jedis.lrange("collections",0,-1));
//截断元素
System.out.println("删除下表0-3区间之外的元素:"+jedis.ltrim("collections",0,3));
System.out.println( " collections的内容: "+jedis.lrange("collections",0,-1));
System.out .println("collections列表出栈(左端): "+jedis.lpop("collections"));
System.out.println( "collections的内容: "+jedis.lrange("collections",0,-1));
System.out .println(" collections添加元素,从列表右端,与lpush相对应:"+jedis.rpush("collections","EnumMap"));
System.out. println(" collections的内容: "+jedis.lrange("collections",0,-1));
System.out.println( " collections列表出栈(右端):"+jedis.rpop("collections"));
System.out.println( " collections的内容: "+jedis.lrange("collections",0,-1));
System.out.println("修改collections指定下标1的内容: "+jedis.lset("collections",1,"LinkedHashMap"));
System.out.println(" collections的内容: "+jedis.lrange("collections",0,-1));
System.out.println("==============================" );
System.out.println("collections的长度:"+jedis.llen("collections"));
System.out.println("获取collections下标为2的元素: "+jedis.lindex("collections",2));
System.out.println( "=============================" );
jedis.lpush("sortedList","3","6","2","0","7","4");
System.out .println( " sortedList排序前:"+jedis.lrange("sortedList",0,-1));
System.out.println(jedis.sort("sortedList"));
System.out.println("sortedList排序后:"+jedis.lrange("sortedList",0,-1));
}
对Hash操作的命令
-
批量添加hash类型的键值对:
jedis.hmset -
添加单个hash键值对:
jedis.hset -
获取所有键值对:
jedis.hgetAll -
获取多个指定键值对:
jedis.hmget -
获取所有hash的key:
jedis.hkeys -
获取所有hash的value:
jedis.hvals -
为指定的hash添加一个内容,若没有该hash则创建:
jedis.hincrBy -
删除指定hash:
jedis.hdel -
获取hash的长度(数据量):
jedis.hlen -
判断hash中是否存在指定值:
jedis.hexists
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");
//添加名称为hash (key) 的hash元素
jedis.hmset("hash", map);
//向名称为hash的hash中添加key为key5,value 为value5元素
jedis.hset("hash", "key5", "value5");
//返回hash的全部元素
System.out.println("散列hash的所有键值对为: " + jedis.hgetAll("hash"));
//返回hash键值对的全部键
System.out.println("散列hash的所有键为: " + jedis.hkeys("hash"));
//返回hash键值对的全部值
System.out.println("散列hash的所有值为: " + jedis.hvals("hash"));
System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6: " + jedis.hincrBy("hash","key6",6));
System.out.println("散列hash的所有键值对为: " + jedis.hgetAll("hash"));
System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6: " + jedis.hincrBy("hash","key6",9));
System.out.println("散列hash的所有键值对为: " + jedis.hgetAll("hash"));
System.out.println("删除一个或者多个键值对: " + jedis.hdel("hash","key2"));
System.out.println("散列hash的所有键值对为: " + jedis.hgetAll("hash"));
System.out.println("散列hash中键值对的个数: " + jedis.hlen("hash"));
System.out.println("判断hash中是否存在key2: " + jedis.hexists("hash","key2"));
System.out.println("判断hash中是否存在key3: " + jedis.hexists("hash","key3"));
System.out.println("获取hash中的值: " + jedis.hmget("hash","key3"));
System.out.println("获取hash中的值: " + jedis.hmget("hash","key3", "key4"));
}
明天继续加油!