示例#1
2
  /**
   * Exercise 3
   *
   * <p>Replace every word in the list with its upper case equivalent.
   */
  private void exercise3() {
    List<String> list =
        new ArrayList<>(Arrays.asList("alpha", "bravo", "charlie", "delta", "echo", "foxtrot"));

    /* YOUR CODE HERE */
    list.replaceAll(String::toUpperCase);
    list.forEach(System.out::println);
  }
  public static void main(String[] args) {

    List<String> dictionary = Arrays.asList(new String[] {"abcde", "uidsfk"});
    List<String> wordsToTest = Arrays.asList(new String[] {"uierjjfdskfju", "CDABE", "abcde"});

    dictionary.replaceAll(s -> sortString(s));
    wordsToTest.replaceAll(s -> sortString(s));

    dictionary
        .stream()
        .forEach(
            dictEntry ->
                wordsToTest
                    .stream()
                    .filter(word -> dictEntry.equalsIgnoreCase(word))
                    .forEach(System.out::println));
  }
  @Test
  public void testImmutability() {
    System.out.println("immutability");
    final List<?> pks = instance.make(k0, k1, k2, k3, k4, k5, k6, k7, k8);

    isImmutable(pks::clear);
    //        isImmutable(() -> pks.add("Olle"));
    //        isImmutable(() -> pks.addAll(Arrays.asList("Olle", "Sven")));
    isImmutable(() -> pks.remove("Olle"));
    isImmutable(() -> pks.remove(1));
    isImmutable(() -> pks.removeAll(new ArrayList<>()));
    isImmutable(() -> pks.replaceAll(UnaryOperator.identity()));
  }
  public static void main(String[] args) {
    List<String> strings = new ArrayList<>();
    Collections.addAll(strings, "Java", "7", "FTW");

    strings.forEach(x -> System.out.print(x + " ")); // Will output: Java 8 FTW

    System.out.println("\nLets update to Java 8 :) ...");
    strings.replaceAll(x -> x == "7" ? "8" : x); // similar to something we'll see later...
    strings.forEach(x -> System.out.print(x + " ")); // Will output: Java 8 WAT

    System.out.println("\nNow lets remove the version...");
    strings.removeIf(x -> x == "8");
    strings.forEach(x -> System.out.print(x + " ")); // Will output: Java 8 WAT
  }
示例#5
1
  static void collectionTest() {
    // removeIf
    Collection<String> c = new HashSet<>();
    c.add("Content 1");
    c.add("Content 2");
    c.add("Content 3");
    c.add("Content 4");
    c.removeIf(s -> s.contains("2"));
    System.out.println("removeIf : " + c);

    /// 基本操作
    List<Integer> list = new ArrayList(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
    list.removeIf(a -> a % 3 == 0);
    System.out.println("a % 3 == 0 " + list);

    // OR 操作
    Predicate<Integer> predicate2 = a -> a % 3 == 0;
    Predicate<Integer> predicate3 = a -> a % 5 == 0;
    list = new ArrayList(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
    list.removeIf(predicate2.or(predicate3));
    System.out.println("a % 3 == 0 or a % 5 == 0 " + list);

    // AND 操作
    predicate2 = a -> a % 3 == 0;
    predicate3 = a -> a % 5 == 0;
    list = new ArrayList(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
    list.removeIf(predicate2.and(predicate3));
    System.out.println("a % 3 == 0 and a % 5 == 0 " + list);

    List<String> stringList = Arrays.asList("a", "b");
    stringList.forEach(System.out::println);

    stringList = Arrays.asList("a", "b", "c");
    stringList.replaceAll(String::toUpperCase);
    System.out.println(stringList); // [A, B, C]

    stringList = Arrays.asList("a", "b", "c");
    stringList.sort(String::compareTo);

    Map<String, Integer> map = new HashMap<>();
    map.put("A", 10);
    map.put("B", 20);
    map.put("C", 30);
    map.forEach((k, v) -> System.out.println("Item : " + k + " Count : " + v));
    System.out.println(map.getOrDefault("D", 40)); // => 40
  }
 @Override
 public Collection<Tuple> transform(Collection<Tuple> tuples, Object... values) {
   List<Tuple> reverse = new ArrayList<>(tuples);
   reverse.replaceAll(tuple -> new Tuple(reverse(tuple.getBases())));
   return reverse;
 }