publicstaticvoidmain(String[] args){ Jedis jedis = new Jedis("192.168.0.153", 6379); jedis.auth("123456"); Fruit fruit1 = new Fruit(1, "苹果", 11); Fruit fruit2 = new Fruit(2, "梨", 6); Fruit fruit3 = new Fruit(3, "油桃", 8); List<Fruit> fruitList = new ArrayList<>(); fruitList.add(fruit1); fruitList.add(fruit2); fruitList.add(fruit3); //转化成json,便于redis存储Java对象 System.out.println("转化成json:"); Gson gson = new Gson(); for (Fruit fruit : fruitList) { jedis.rpush("redisFruitList", gson.toJson(fruit)); } //遍历 List<String> redisFruitList = jedis.lrange("redisFruitList", 0, -1); for (String s : redisFruitList) { System.out.println(s); } //Redis的list转化成Java对象 System.out.println("转化成Java对象:"); for (int i = 0; i < redisFruitList.size(); i++) { Fruit fruit = gson.fromJson(redisFruitList.get(i), Fruit.class); System.out.println(fruit.toString()); } //清空redisFruitList Long length = jedis.llen("redisFruitList"); if (length > 0) { for (int i = 0; i < length; i++) { jedis.rpop("redisFruitList"); } } //关闭连接 jedis.close(); } }