List类型的操作命令
List类型的操作命令
一:lpush、rpush:添加值,格式是:lpush list的key item项的值(值可以有多个)
操作如下:(注意看从左、从右添加值后,元素在list中的位置)
从左插入
1 127.0.0.1:6379> lpush list 1 2 3 2 (integer) 3 3 127.0.0.1:6379> lrange list 0 -1 4 1) "3" 5 2) "2" 6 3) "1" 7 127.0.0.1:6379> lpush list adaxi 8 (integer) 4 9 127.0.0.1:6379> lrange list 0 -1 10 1) "adaxi" 11 2) "3" 12 3) "2" 13 4) "1"
从右插入 14 127.0.0.1:6379> rpush list nihao 15 (integer) 5 16 127.0.0.1:6379> lrange list 0 -1 17 1) "adaxi" 18 2) "3" 19 3) "2" 20 4) "1" 21 5) "nihao" 22 127.0.0.1:6379>
Java代码示例如下:
1 public static void main(String[] args) { 2 Jedis jedis = new Jedis("127.0.0.1",6379); 3 /** 4 * 相当于执行 lpush list 1 2 3 5 */ 6 Long lpush = jedis.lpush("list", "1", "2", "3"); 7 System.out.println("lpush = " + lpush); 8 System.out.println("=========lpush============="); 9 /** 10 * 相当于执行 rpush list nihao 11 */ 12 Long rpush = jedis.rpush("list", "nihao"); 13 System.out.println("rpush = " + rpush); 14 System.out.println("=========rpush============="); 15 16 17 }
二:lrange:按索引范围获取值,格式是:lrange list的key 起始索引 终止索引 (-1表示最后一个索引)
1 127.0.0.1:6379> keys * 2 1) "list" 3 127.0.0.1:6379> 4 127.0.0.1:6379> lrange list 0 -1 5 1) "adaxi" 6 2) "3" 7 3) "2" 8 4) "1" 9 5) "nihao" 10 127.0.0.1:6379> lrange list 0 -3 11 1) "adaxi" 12 2) "3" 13 3) "2" 14 127.0.0.1:6379> lrange list 0 3 15 1) "adaxi" 16 2) "3" 17 3) "2" 18 4) "1" 19 127.0.0.1:6379> lrange list 0 2 20 1) "adaxi" 21 2) "3" 22 3) "2" 23 127.0.0.1:6379> lrange list 1 2 24 1) "3" 25 2) "2" 26 127.0.0.1:6379> lrange list 0 0 27 1) "adaxi" 28 127.0.0.1:6379>
Java代码示例如下:
1 /** 2 * 相当于执行 lrange list 0 -1 3 */ 4 List<String> list = jedis.lrange("list", 0, -1); 5 System.out.println(String.join(",", list));
三:lindex:获取指定索引的值,格式是:lindex list的key 索引号
1 127.0.0.1:6379> keys * 2 1) "list" 3 127.0.0.1:6379> lrange list 0 -1 4 1) "adaxi" 5 2) "3" 6 3) "2" 7 4) "1" 8 5) "nihao" 9 127.0.0.1:6379> lindex list 0 10 "adaxi" 11 127.0.0.1:6379> lindex list 4 12 "nihao" 13 127.0.0.1:6379>
Java代码示例如下:
1 /** 2 * 相当于执行 lindex list 0 3 */ 4 String lindex = jedis.lindex("list", 0); 5 System.out.println(lindex);
四:lpop、rpop:弹出值,格式是:lpop list的key
1 127.0.0.1:6379> keys * 2 1) "list" 3 127.0.0.1:6379> lrange list 0 -1 4 1) "adaxi" 5 2) "3" 6 3) "2" 7 4) "1" 8 5) "nihao" 9 127.0.0.1:6379> lpop list 10 "adaxi" 11 127.0.0.1:6379> rpop list 12 "nihao" 13 127.0.0.1:6379>
Java代码示例如下:
1 /*redis中对于List类型的: lpop、rpop 操作示例*/ 2 /** 3 * 相当于执行 lpop list 4 */ 5 String lpop = jedis.lpop("list"); 6 System.out.println("lpop = " + lpop); 7 /** 8 * 相当于执行 rpop list 9 */ 10 String rpop = jedis.rpop("list"); 11 System.out.println("rpop = " + rpop);
五:llen:获取list中元素的个数,格式是:llen list的key
1 127.0.0.1:6379> keys * 2 1) "list" 3 127.0.0.1:6379> lrange list 0 -1 4 1) "3" 5 2) "2" 6 3) "1" 7 127.0.0.1:6379> llen list 8 (integer) 3 9 127.0.0.1:6379>
Java代码示例如下:
/*redis中对于List类型的: llen 操作示例*/ /** * 示例1: 相当于执行 llen mylist */ Long llen = jedis.llen("list"); System.out.println("llen = " + llen);
六:lrem:删除元素,格式是:lrem list的key 数量 item项的值,(数量可正负,表示从左或从右删除;如果数量为0,表示删除全部与给定值相符的项)
1 127.0.0.1:6379> keys * 2 1) "list" 3 127.0.0.1:6379> lrange list 0 -1 4 1) "3" 5 2) "2" 6 3) "1" 7 127.0.0.1:6379> lpush list 1 8 (integer) 4 9 127.0.0.1:6379> 10 127.0.0.1:6379> lrem list -1 1 11 (integer) 1 12 127.0.0.1:6379> lrange list 0 -1 13 1) "1" 14 2) "3" 15 3) "2" 16 127.0.0.1:6379>
Java代码示例如下:
1 /*redis中对于List类型的: lrem 操作示例*/ 2 /** 3 * 示例1: 相当于执行 lrem list 0 1 4 */ 5 Long lrem = jedis.lrem("list",0,"1"); 6 System.out.println("lrem = " + lrem);
七:ltrim:保留指定索引区间的元素,格式是:ltrim list的key 起始索引 结束索引
1 127.0.0.1:6379> lrange list 0 -1 2 1) "1" 3 2) "3" 4 3) "2" 5 127.0.0.1:6379> rpush list 4 5 6 7 8 9 6 (integer) 9 7 127.0.0.1:6379> lrange list 0 -1 8 1) "1" 9 2) "3" 10 3) "2" 11 4) "4" 12 5) "5" 13 6) "6" 14 7) "7" 15 8) "8" 16 9) "9" 17 127.0.0.1:6379> ltrim list 0 5 18 OK 19 127.0.0.1:6379> lrange list 0 -1 20 1) "1" 21 2) "3" 22 3) "2" 23 4) "4" 24 5) "5" 25 6) "6" 26 127.0.0.1:6379> ltrim list 0 3 27 OK 28 127.0.0.1:6379> lrange list 0 -1 29 1) "1" 30 2) "3" 31 3) "2" 32 4) "4" 33 127.0.0.1:6379>
Java代码示例如下:
1 /** 2 * 相当于执行 ltrim list 0 1 3 */ 4 String ltrim = jedis.ltrim("list",0,1); 5 System.out.println("ltrim = " + ltrim);
八:blpop、brpop:弹出值,格式是:blpop list的key值 过期时间。(key可以是多个,如果没有值,会一直等到有值,直到过期)
1 127.0.0.1:6379> lrange list 0 -1 2 1) "1" 3 2) "3" 4 3) "2" 5 4) "4" 6 127.0.0.1:6379> blpop list 3 7 1) "list" 8 2) "1" 9 127.0.0.1:6379> lrange list 0 -1 10 1) "3" 11 2) "2" 12 3) "4" 13 127.0.0.1:6379> lrange list 0 -1 14 1) "3" 15 2) "2" 16 3) "4" 17 127.0.0.1:6379> blpop list 3 18 1) "list" 19 2) "3" 20 127.0.0.1:6379> brpop list 0 1 21 1) "list" 22 2) "4" 23 127.0.0.1:6379> lrange list 0 -1 24 1) "2" 25 127.0.0.1:6379>
Java代码示例如下:
1 /*redis中对于List类型的: blpop、brpop 操作示例*/ 2 /** 3 * 相当于执行 blpop list 5 4 */ 5 List<String> mylist = jedis.blpop(5, "list"); 6 mylist.stream().forEach(System.out::println); 7 8 System.out.println("==========="); 9 /** 10 * 相当于执行 ltrim list 0 1 11 */ 12 List<String> mylist2 = jedis.brpop(5, "list"); 13 mylist2.stream().forEach(System.out::println);
九:rpoplpush:将元素从一个列表转移到另外一个列表,格式是:rpoplpush 源list的key值 目的list的key值
rpoplpush:将元素从一个列表转移到另外一个列表,格式是:rpoplpush 源list的key值 目的list的key值 brpoplpush:将元素从一个列表转移到另外一个列表,格式是:brpoplpush 源list的key值 目的list的key值 过期时间
1 127.0.0.1:6379> lpush list 1 2 3 4 5 6 7 8 9 2 (integer) 9 3 127.0.0.1:6379> lrange list 0 -1 4 1) "9" 5 2) "8" 6 3) "7" 7 4) "6" 8 5) "5" 9 6) "4" 10 7) "3" 11 8) "2" 12 9) "1" 13 127.0.0.1:6379> rpoplpush list list1 14 "1" 15 127.0.0.1:6379> lrange list 0 -1 16 1) "9" 17 2) "8" 18 3) "7" 19 4) "6" 20 5) "5" 21 6) "4" 22 7) "3" 23 8) "2" 24 127.0.0.1:6379> lrange list1 0 -1 25 1) "1" 26 127.0.0.1:6379>
Java代码示例如下:
1 /** 2 * 相当于执行 rpoplpush list list1 3 */ 4 String rpoplpush = jedis.rpoplpush("list", "list1"); 5 System.out.println("rpoplpush = " + rpoplpush); 6
十:brpoplpush:将元素从一个列表转移到另外一个列表,格式是:brpoplpush 源list的key值 目的list的key值 过期时间
127.0.0.1:6379> brpoplpush list list2 2 "2" 127.0.0.1:6379> lrange list1 0 -1 1) "1" 127.0.0.1:6379>
Java代码示例如下:
/** * 相当于执行 brpoplpush list list1 4 */ String brpoplpush = jedis.brpoplpush("list", "list1", 4); System.out.println("brpoplpush = " + brpoplpush);
十一:lset:设置指定索引的值,格式是:lset list的key 索引 新的值
1 127.0.0.1:6379> lrange list 0 -1 2 1) "9" 3 2) "8" 4 3) "7" 5 4) "6" 6 5) "5" 7 6) "4" 8 7) "3" 9 127.0.0.1:6379> lset list 2 22 10 OK 11 127.0.0.1:6379> lrange list 0 -1 12 1) "9" 13 2) "8" 14 3) "22" 15 4) "6" 16 5) "5" 17 6) "4" 18 7) "3" 19 127.0.0.1:6379>
Java代码示例如下:
1 /** 2 * redis中对于List类型的: lset操作示例 3 * lset:设置指定索引的值,格式是:lset list的key 索引 新的值 4 */ 5 /** 6 * 相当于执行 lset list 2 22 7 */ 8 String lset = jedis.lset("list", 2L, "22"); 9 System.out.println("lset = " + lset);
linsert:插入元素,格式是:linsert list的key before|after 定位查找的值 添加的值
127.0.0.1:6379> lrange list 0 -1
1) "9"
2) "8"
3) "22"
4) "6"
5) "5"
6) "4"
7) "3"
127.0.0.1:6379>
127.0.0.1:6379> linsert list before 22 10
(integer) 8
127.0.0.1:6379> lrange list 0 -1
1) "9"
2) "8"
3) "10"
4) "22"
5) "6"
6) "5"
7) "4"
8) "3"
127.0.0.1:6379> linsert list after 9 11
(integer) 9
127.0.0.1:6379> lrange list 0 -1
1) "9"
2) "11"
3) "8"
4) "10"
5) "22"
6) "6"
7) "5"
8) "4"
9) "3"
127.0.0.1:6379>
Java代码示例如下:
1 /** 2 * redis中对于List类型的: linsert操作示例 3 * linsert:插入元素,格式是:linsert list的key before|after 定位查找的值 添加的值 4 */ 5 /** 6 * 相当于执行 linsert list after 22 10 7 */ 8 Long linsertbefore = jedis.linsert("list", BinaryClient.LIST_POSITION.BEFORE, "22", "10"); 9 System.out.println("linsert = " + linsertbefore); 10 Long linsertafter = jedis.linsert("list", BinaryClient.LIST_POSITION.AFTER, "9", "11"); 11 System.out.println("linsert = " + linsertafter);