lambda表达式实现多线程遍历list存入redis中

@Test
    public void testLambda(){
        List list=Arrays.asList(new String[]{"Ni000","Hao000","Lambda000"});
        list.parallelStream().map(x->redisUtil.set(x,x)).forEach(x-> System.out.println(x));
        System.out.println(redisTemplate.opsForValue().get("Ni000"));
    }

定义一个RedisUtil包装一下,因为stream.map()方法需要参数是Object类型
而redisTemplate.opsForValue().set(x,x)是void没有返回值

public class RedisUtil {
    @Autowired
    private RedisTemplate redisTemplate;
    public boolean set(Object o,Object o1){
            boolean flag=true;
            try {
                redisTemplate.opsForValue().set(o,o1);
            }catch (Exception e){
                e.printStackTrace();
                flag=false;
                return false;
            }
            return flag;
    }
}

你可能感兴趣的:(lambda表达式实现多线程遍历list存入redis中)