import com.alibaba.fastjson.JSON; import com.ctrip.car.osd.util.LogHelper; import org.apache.commons.collections.CollectionUtils; import java.util.ArrayList; import java.util.List; import java.util.function.Supplier; public class CacheHelper { private static final int EXPIREDSECOND = 60 * 10;//10 MINUTES /** * * @param key 缓存key * @param supplier 需缓存结果的方法 * @param classz 缓存数据的类型 * @param <T> * @return */ public static <T> List<T> getOrSetData(String key, Supplier<List<T>> supplier, Class classz) { List<T> list; try { //修改成自己的缓存实现 //存在缓存则使用fastjson对缓存中的数据进行转换 if (RedisManager.exists(key)) { list = JSON.parseArray(RedisManager.get(key), classz); } else { //不存在缓存的则调用supplier,并且将结果存入缓存 list = supplier.get(); if (CollectionUtils.isNotEmpty(list)) { RedisManager.set(key, JSON.toJSONString(list), EXPIREDSECOND); } } } catch (Exception ex) { LogHelper.error("CacheHelper.getOrSetData", ex); list = new ArrayList<>(); } return list; } }
List<DataType> allList = CacheHelper.getOrSetData(CACHE_KEY, () -> getAllData(), Advertisement.class);
List<Person> lisiList = new ArrayList<>(); Consumer <Person> consumer = x -> { if (x.name.equals("lisi")){ lisiList.add(x); } }; consumer = consumer.andThen( x -> lisiList.removeIf(y -> y.age < 23) ); Stream.of( new Person(21,"zhangsan"), new Person(22,"lisi"), new Person(23,"wangwu"), new Person(24,"wangwu"), new Person(23,"lisi"), new Person(26,"lisi"), new Person(26,"zhangsan") ).forEach(consumer); System.out.println(JSON.toJSONString(lisiList));
/** * 1. 必须为女生; * 2. 姓名为4个字。 */ public static void main(String[] args) { String[] array = { "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男", "赵丽颖,女" }; getFemaleAndname((s) -> s.split("\\,")[0].length() == 4, (s) -> s.split("\\,")[1].equals("女"), array); } private static void getFemaleAndname(Predicate<String> one, Predicate<String> two, String[] arr) { for (String string : arr) { if (one.and(two).test(string)) { System.out.println(string); } } }
public class CheckUtils { private static final Function<String, String> lengthCheck = params -> { if (params.length() > 100) { throw new RuntimeException("Length exceed max limit."); } return params; }; private static final Function<String, String> invalidCharacterCheck = str -> { if (!str.matches("^[a-f,A-F]$")) { throw new RuntimeException("Contains invalid character."); } return str; }; /** * 这里的公共方法组合了该类中的基本校验逻辑构成一个复合的逻辑 */ public static void checkStringLengthAndPhoneNumber(String string) { invalidCharacterCheck.compose(lengthCheck).apply(string); } }
标签:缓存,java,函数,编程,list,Person,static,new,String From: https://www.cnblogs.com/goPush/p/16784043.html