list 双向链表
1 lpush 方式添加 从左往右添加
127.0.0.1:6379> lpush list1 a b c d r
(integer) 5
127.0.0.1:6379>
查询list1 从左到右0 -1
语法 :lrange key start stop
127.0.0.1:6379> lrange list1 0 -1
1) "r"
2) "d"
3) "c"
4) "b"
5) "a"
127.0.0.1:6379>
2 rpush方式
127.0.0.1:6379> rpush list2 zhangsan lisi wangmazi zhaoqian sunli
(integer) 5
127.0.0.1:6379> lrange list2 0 3
1) "zhangsan"
2) "lisi"
3) "wangmazi"
4) "zhaoqian"
127.0.0.1:6379>
3 lpop 左侧弹出集合元素
127.0.0.1:6379> lpop list2
"zhangsan"
127.0.0.1:6379>
4 rpop 从右侧弹出list集合元素
127.0.0.1:6379> rpop list2
"sunli"
127.0.0.1:6379>
5 查看list集合中元素个数 llen key
127.0.0.1:6379> llen list2
(integer) 3
127.0.0.1:6379> llen list1
(integer) 5
6 集合头部插入元素 lpushx key value
127.0.0.1:6379> lpushx list2 nihao
(integer) 10
127.0.0.1:6379> lrange list2 0 -1
1) "nihao"
2) "xigua"
3) "caigua"
4) "caiyu"
5) "admin"
6) "wangsanmazi"
7) "licheng"
8) "wangmazi"
9) "xiaoli"
10) "zhaoqian"
127.0.0.1:6379>
7 集合尾部插入元素rpushx key value
127.0.0.1:6379> rpush list2 sunli
(integer) 5
127.0.0.1:6379> lrange list2 0 -1
1) "admin"
2) "lisi"
3) "wangmazi"
4) "zhaoqian"
5) "sunli"
127.0.0.1:6379>
8 集合头部插入多个元素
127.0.0.1:6379> lpush list2 caiyu caigua xigua guazi
(integer) 9
127.0.0.1:6379> lrange list2 0 -1
1) "guazi"
2) "xigua"
3) "caigua"
4) "caiyu"
5) "admin"
6) "lisi"
7) "wangmazi"
8) "zhaoqian"
9) "sunli"
9 集合尾部插入多个元素
127.0.0.1:6379> rpush list2 nangua huanggua jiezi
(integer) 12
127.0.0.1:6379> lrange list2 0 -1
1) "guazi"
2) "xigua"
3) "caigua"
4) "caiyu"
5) "admin"
6) "lisi"
7) "wangmazi"
8) "zhaoqian"
9) "sunli"
10) "nangua"
11) "huanggua"
12) "jiezi"
127.0.0.1:6379>
10 指定方向删除指定个元素 lrem
127.0.0.1:6379> lrem list2 1 guazi
(integer) 1
127.0.0.1:6379> lrange list2 0 -1
1) "xigua"
2) "caigua"
3) "caiyu"
4) "admin"
5) "lisi"
6) "wangmazi"
7) "zhaoqian"
8) "sunli"
9) "nangua"
10) "huanggua"
11) "jiezi"
127.0.0.1:6379> rpush list2 jiezi jiezi jiezi jiezi
(integer) 15
127.0.0.1:6379> lrange list2 0 -1
1) "xigua"
2) "caigua"
3) "caiyu"
4) "admin"
5) "lisi"
6) "wangmazi"
7) "zhaoqian"
8) "sunli"
9) "nangua"
10) "huanggua"
11) "jiezi"
12) "jiezi"
13) "jiezi"
14) "jiezi"
15) "jiezi"
127.0.0.1:6379> lrem list2 -5 jiezi - 代表从右侧开始删除5个 jiezi
(integer) 5
127.0.0.1:6379> lrange list2 0 -1
1) "xigua"
2) "caigua"
3) "caiyu"
4) "admin"
5) "lisi"
6) "wangmazi"
7) "zhaoqian"
8) "sunli"
9) "nangua"
10) "huanggua"
127.0.0.1:6379>
11 删除集合中所有的张三
127.0.0.1:6379> lpush list2 zhangsan zhangsan
(integer) 12
127.0.0.1:6379> rpush list2 zhangsan zhangsan
(integer) 14
127.0.0.1:6379> lrange list2 0 -1
1) "zhangsan"
2) "zhangsan"
3) "xigua"
4) "caigua"
5) "caiyu"
6) "admin"
7) "lisi"
8) "wangmazi"
9) "zhaoqian"
10) "sunli"
11) "nangua"
12) "huanggua"
13) "zhangsan"
14) "zhangsan"
127.0.0.1:6379> lrem list2 0 zhangsan
(integer) 4
127.0.0.1:6379> lrange list2 0 -1
1) "xigua"
2) "caigua"
3) "caiyu"
4) "admin"
5) "lisi"
6) "wangmazi"
7) "zhaoqian"
8) "sunli"
9) "nangua"
10) "huanggua"
127.0.0.1:6379>
12 设置指定索引的值 lset
127.0.0.1:6379> lset list2 4 "wangsanmazi" 设置索引为4 的元素的值为王三麻子 索引下标从0开始
OK
127.0.0.1:6379> lrange list2 0 -1
1) "xigua"
2) "caigua"
3) "caiyu"
4) "admin"
5) "wangsanmazi"
6) "wangmazi"
7) "zhaoqian"
8) "sunli"
9) "nangua"
10) "huanggua"
127.0.0.1:6379>
13 在集合某元素中插入指定元素 linsert key BEFORE|AFTER pivot value BEFORE 后 AFTER 前
127.0.0.1:6379> linsert list2 before wangmazi licheng 在wangmazi之前插入licheng
(integer) 11
127.0.0.1:6379> linsert list2 after wangmazi xiaoli 在wangmazi之后插入xiaoli
(integer) 12
127.0.0.1:6379> lrange list2 0 -1
1) "xigua"
2) "caigua"
3) "caiyu"
4) "admin"
5) "wangsanmazi"
6) "licheng"
7) "wangmazi"
8) "xiaoli"
9) "zhaoqian"
10) "sunli"
11) "nangua"
12) "huanggua"
127.0.0.1:6379>
14 将集合中的元素弹出并插入到另一个集合 rpoplpush source destination source 源集合 destination 目标集合
127.0.0.1:6379> rpoplpush list2 list3
"huanggua"
127.0.0.1:6379> rpoplpush list2 list3
"nangua"
127.0.0.1:6379> rpoplpush list2 list3
"sunli"
127.0.0.1:6379> lrange list3 0 -1
1) "sunli"
2) "nangua"
3) "huanggua"
127.0.0.1:6379>
15 删除指定区间外的值
127.0.0.1:6379> ltrim list2 0 1
OK
127.0.0.1:6379> lrange list2 0 -1
1) "nihao"
2) "xigua"
127.0.0.1:6379>
16 数组排序
127.0.0.1:6379> lpush sort1 1 2 8 9 5 4 89 25 23 6 4
(integer) 11
127.0.0.1:6379> lrange sort1 0 -1
1) "4"
2) "6"
3) "23"
4) "25"
5) "89"
6) "4"
7) "5"
8) "9"
9) "8"
10) "2"
11) "1"
127.0.0.1:6379> sort sort1
1) "1"
2) "2"
3) "4"
4) "4"
5) "5"
6) "6"
7) "8"
8) "9"
9) "23"
10) "25"
11) "89"
127.0.0.1:6379>
17 通过java代码操作list集合
package redisUtils;
import java.util.List;
import org.junit.Test;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.Jedis;
/**
* 操作list集合
* @Description: TODO(用一句话描述该文件做什么)
* @author guochao
* @date 2018年10月2日
*
*/
public class RedisList {
@Test
public void add(){ //从左添加和从右添加
Jedis jedis = RedisUtils.getJedis();
// 从右往左
jedis.rpush("nameList", "zhangsan");
jedis.rpush("nameList", "lisi");
jedis.rpush("nameList", "wangmazi");
// 从左往右
jedis.lpush("nameList", "zhangsan1");
jedis.lpush("nameList", "zhangsan2");
jedis.lpush("nameList", "zhangsan3");
List nameList = jedis.lrange("nameList", 0, -1);// 查询出nameList集合中的素有元素
for (String name : nameList) {
System.out.println(name);
/**
* zhangsan3
zhangsan2
zhangsan1
zhangsan
lisi
wangmazi
*/
}
jedis.close();
RedisUtils.close();
}
@Test
public void addIndex(){ // 从指定位置添加元素
Jedis jedis = RedisUtils.getJedis();
jedis.linsert("nameList", LIST_POSITION.AFTER, "zhangsan1", "zhangsan4");//在zhangsan1后增加一个zhangsan4
jedis.linsert("nameList", LIST_POSITION.BEFORE, "zhangsan1", "zhangsan0");//在zhangsan1前增加一个zhangsan0
List nameList = jedis.lrange("nameList", 0, -1);// 查询出nameList集合中的素有元素
for (String name : nameList) {
System.out.println(name);
/**
* zhangsan3
zhangsan2
zhangsan0
zhangsan1
zhangsan4
zhangsan
lisi
wangmazi
*/
}
jedis.close();
RedisUtils.close();
}
@Test
public void setIndex(){
Jedis jedis = RedisUtils.getJedis();
jedis.lset("nameList",1, "wangmazi"); // 设置从左往右数第二个元素的值为wangmazi
jedis.lset("nameList",-1, "pig"); // 设置从右往左第一个元素的值为pig
jedis.lset("nameList",-2, "pig"); // 设置从右往左 第二个元素的值为pig
List nameList = jedis.lrange("nameList", 0, -1);// 查询出nameList集合中的素有元素
for (String name : nameList) {
System.out.println(name);
/**
* zhangsan3
wangmazi
zhangsan0
zhangsan1
zhangsan4
zhangsan
pig
pig
*/
}
jedis.close();
RedisUtils.close();
}
@Test
public void delList(){
Jedis jedis = RedisUtils.getJedis();
Long lrem = jedis.lrem("nameList", 0, "pig");// 删除集合中所有的pig
System.out.println(lrem); //2
List nameList = jedis.lrange("nameList", 0, -1);// 查询出nameList集合中的素有元素
for (String name : nameList) {
System.out.println(name);
/**
*
zhangsan3
wangmazi
zhangsan0
zhangsan1
zhangsan4
zhangsan
*/
}
jedis.close();
RedisUtils.close();
}
/**
* 从左侧弹出元素
* 从右侧弹出元素
* @Description: TODO(用一句话描述该文件做什么)
* @author guochao
* @date 2018年10月2日
*
*/
@Test
public void testPop(){
Jedis jedis = RedisUtils.getJedis();
String lpop = jedis.lpop("nameList");
System.out.println("左侧弹出:"+lpop);
String rpop = jedis.rpop("nameList");
System.out.println("右侧弹出:"+rpop);
/**
* 左侧弹出:zhangsan3
右侧弹出:zhangsan
*/
jedis.close();
RedisUtils.close();
}
/**
* 查看集合中元素的个数
* @Description: TODO(用一句话描述该文件做什么)
* @author guochao
* @date 2018年10月2日
*
*/
@Test
public void llenList(){
Jedis jedis = RedisUtils.getJedis();
Long llen = jedis.llen("nameList");
List nameList = jedis.lrange("nameList", 0, -1);// 查询出nameList集合中的素有元素
System.out.println(llen);
for (String name : nameList) {
System.out.println(name);
/**
* 4
wangmazi
zhangsan0
zhangsan1
zhangsan4
*/
}
jedis.close();
RedisUtils.close();
}
/**
* 集合头部插入元素
* @Description: TODO(用一句话描述该文件做什么)
* @author guochao
* @date 2018年10月2日
*
*/
@Test
public void lpushX(){
Jedis jedis = RedisUtils.getJedis();
jedis.lpushx("nameList", "找钱");
List nameList = jedis.lrange("nameList", 0, -1);// 查询出nameList集合中的素有元素
for (String name : nameList) {
System.out.println(name);
/**
* 找钱
wangmazi
zhangsan0
zhangsan1
zhangsan4
*/
}
jedis.close();
RedisUtils.close();
}
/**
* 集合尾部插入元素
* @Description: TODO(用一句话描述该文件做什么)
* @author guochao
* @date 2018年10月2日
*
*/
@Test
public void rpushX(){
Jedis jedis = RedisUtils.getJedis();
jedis.rpushx("nameList", "王麻子");
List nameList = jedis.lrange("nameList", 0, -1);// 查询出nameList集合中的素有元素
for (String name : nameList) {
System.out.println(name);
/**
* 找钱
wangmazi
zhangsan0
zhangsan1
zhangsan4
王麻子
*/
}
jedis.close();
RedisUtils.close();
}
/**
* 弹出元素 存入新的集合
* @Description: TODO(用一句话描述该文件做什么)
* @author guochao
* @date 2018年10月2日
*
*/
@Test
public void rpopLpushDesc(){
Jedis jedis = RedisUtils.getJedis();
jedis.brpoplpush("nameList", "newList",1);
List newList = jedis.lrange("newList", 0, -1);// 查询出newList集合中的素有元素
for (String name : newList) {
System.out.println("newList"+name);
/**
*
newList王麻子
*/
}
jedis.close();
RedisUtils.close();
}
/**
* 数组排序
*/
@Test
public void sortList(){
Jedis jedis = RedisUtils.getJedis();
List sort1 = jedis.lrange("sort1", 0, -1);// 查询出sort1集合中的素有元素
System.out.println("未排序:");
for (String name : sort1) {
System.out.print(name+" ");
}
System.out.println("");
System.out.println("排序后:");
List sort = jedis.sort("sort1");
for (String name : sort) {
System.out.print(name+" ");
}
/**
* 未排序:
4 6 23 25 89 4 5 9 8 2 1
排序后:
1 2 4 4 5 6 8 9 23 25 89
*/
jedis.close();
RedisUtils.close();
}
/**
* 删除指定区间外的值
* ltirm
* @Description: TODO(用一句话描述该文件做什么)
* @author guochao
* @date 2018年10月2日
*
*/
@Test
public void delLtirm(){
Jedis jedis = RedisUtils.getJedis();
jedis.ltrim("nameList", 0, 0); // 保留第一个元素 其他的全部删除
List nameList = jedis.lrange("nameList", 0, -1);// 查询出nameList集合中的素有元素
for (String name : nameList) {
System.out.println(name); //zhangsan3
}
jedis.close();
RedisUtils.close();
}
@Test
public void testDel(){
Jedis jedis = RedisUtils.getJedis();
jedis.lrem("nameList", 0, "zhangsan3");
List nameList = jedis.lrange("nameList", 0, -1);// 查询出nameList集合中的素有元素
for (String name : nameList) {
System.out.println(name); //
}
jedis.close();
RedisUtils.close();
}
}