redis中遍历List、set集合
public class StartRedis {public static void main(String[] args) {Jedis jedis = new Jedis("localhost", 6379);System.out.println("链接成功!");/**测试redis连接**/// jedis.auth("123456");// jedis.set("student","z
·
public class StartRedis {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost", 6379);
System.out.println("链接成功!");
/**测试redis连接**/
// jedis.auth("123456");
// jedis.set("student","zhangsan");
//System.out.println("redis 字符串输出为"+jedis.get("student"));
//List列表
jedis.lpush("Student","zhangsan");
jedis.lpush("Student","lisi");
jedis.lpush("Student","wangwu");
jedis.lpush("Student","shimisi");
List<String> student = jedis.lrange("Student",0,3);
/**普通for循环遍历List集合(普通for没有办法遍历set集合)**/
for (int i = 0; i<student.size();i++){
System.out.println("列表项为: "+student.get(i));
}
//Set列表
jedis.sadd("xiaoming", "zhangsan", "lisi");
jedis.sadd("xiaoming", "男", "女");
jedis.sadd("xiaoming", "18", "20");
Set<String> tearcher = jedis.smembers("xiaoming");
/**增强for循环遍历**/
System.out.println("增强for循环****");
for (String a : tearcher){
System.out.println(a);
}
/**迭代器遍历set**/
System.out.println("迭代器*****");
Iterator it = tearcher.iterator();
while (it.hasNext()) {
String str = (String) it.next();
System.out.println(str);
}
/**forEach循环遍历set**/
System.out.println("forEach****");
/*第一种写法*/
tearcher.forEach(System.out::println);
/*第二种写法*/
tearcher.forEach(n->System.out.println(n));
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)