myCollection.stream().map(new Mapper<String,String>() { public String map(String input) { return new StringBuilder(input).reverse().toString(); } }); Predicate<String> isEmpty = new Predicate<String> { boolean test(String myString) { return myString.isEmpty(); } } myCollection.stream().map(input -> new StringBuilder(input).reverse().toString()); Predicate<String> isEmpty = myString -> myString.isEmpty();
例子:
public static List<Integer> getDiffNum(int i) { List<Integer> rs = new ArrayList<>(); Integer[] numsTmp = nums.clone(); for (int j = 0; j < i; j++) { int len = rand.nextInt(numsTmp.length); int tN = numsTmp[len]; rs.add(tN); List<Integer> asList = transferArrayToList(numsTmp); asList.remove(len); Integer[] intes =new Integer[asList.size()]; numsTmp = asList.toArray(intes); } rs.sort(new Comparator<Integer>() { @Override public int compare(Integer a, Integer b) { return a.compareTo(b); } }); return rs; }
转换后:
public static List<Integer> getDiffNum(int i) { List<Integer> rs = new ArrayList<>(); Integer[] numsTmp = nums.clone(); for (int j = 0; j < i; j++) { int len = rand.nextInt(numsTmp.length); int tN = numsTmp[len]; rs.add(tN); List<Integer> asList = transferArrayToList(numsTmp); asList.remove(len); Integer[] intes =new Integer[asList.size()]; numsTmp = asList.toArray(intes); } rs.sort((a,b) -> a.compareTo(b)); return rs; }