Runnable incrementer = () -> i++; // Will not compile: // "Variable used in lambda should be final or effectively final" List<String> effectivelyFinalList = new ArrayList<>(); Runnable runnable = () -> effectivelyFinalList.add("add"); // Will compile List<String> nonFinalList = new ArrayList<>(); Runnable otherRunnable = () -> nonFinalList.add("add"); // Will not compile nonFinalList = new ArrayList<>();
remove the @Ignore tag. Then run the test. */ public class LambdaLab { // Exercise 1: Use an anonymous inner class to implement a Comparator<String> that sorts // strings by length (don't bother making it null safe) @Ignore @Test public void writeClassicComparator() { Comparator<String> comparator = null; assertEquals(6, comparator.compare("13 chars long", "7 chars")); assertEquals(-6, comparator.compare("7 chars", "13 chars long")); assertEquals(0, comparator.compare("same", "same"));
predicate on the given argument. * * @param t the input argument * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} */ boolean test(T t); ... }
remove the @Ignore tag. Then run the test. */ public class PredicatesAndFunctionsLab { // Exercise 1: // * Write a function that doubles the value of a given integer // * Write a function that increases the value of a given integer by 5 @Test @Ignore public void basicIntegerFunctions() throws Exception { Function<Integer, Integer> doubler = null; // TODO Function<Integer, Integer> addFive = null; // TODO Assert.assertThat(doubler.apply(2), is(4));
på equals() .sorted() // returnera en naturligt sorterad Stream .sorted(Comparator) // returnera en Stream som är sorterad enligt given Comparator .limit(long) // begränsa till angivet antal element .skip(long) // hoppa över angivet antal element
new Person("Anna", 30), new Person("Lars", 25), new Person("Lars", 50)); Stream<Person> sortedByAge = persons.stream() .sorted(Comparator.comparing(Person::getAge)); // [Person: Lars 25, Person: Anna 30, Person: Anna 35, Person: Anna 40, Person: Lars 50] Stream<Person> sortedByNameThenAge = persons.stream() .sorted(Comparator.comparing(Person::getName) .thenComparing(Person::getAge)); // [Person: Anna 30, Person: Anna 35, Person: Anna 40, Person: Lars 25, Person: Lars 50]
Java SE 8 Lambda/Streams * and remove the @Ignore tag. Then run the test. */ public class StreamsLab1 { // Exercise 1: Create a Stream of the words in the WORD_LIST constant that are shorter // than 4 characters @Test @Ignore public void filter() throws Exception { Stream<String> shorterThanFour = null; Assert.assertEquals("in, can, be, by, of", shorterThanFour.collect(Collectors.joining(", "))); }
= range.average(); OptionalInt max = range.max(); OptionalInt min = range.min(); int sum = range.sum(); IntSummaryStatistics statistics = range.summaryStatistics();
karl = new Person("Karl", 30, Arrays.asList("Kalle", "Kakan")); Person anna = new Person("Anna", 30, new ArrayList<>()); Person lars = new Person("Lars", 40, Arrays.asList("Lasse")); Person sara = new Person("Sara", 40, Arrays.asList("Sasha")); List<Person> persons = Arrays.asList(kristina, karl, anna, lars, sara); // { // 40={1=[Person: Lars, Person: Sara], 2=[Person: Kristina]}, // 30={0=[Person: Anna], 2=[Person: Karl]} // } Map<Integer, Map<Integer, List<Person>>> mapByAgeAndNumberOfNicknames = persons.stream() .collect(Collectors.groupingBy(Person::getAge, Collectors.groupingBy( person -> person.getNickNames().size())));
Java SE 8 Lambda/Streams * and remove the @Ignore tag. Then run the test. */ public class StreamsLab2 { // Exercise 1: Create a Stream of the words in the WORD_LIST constant that are shorter // than 4 characters @Test @Ignore public void filter() throws Exception { Stream<String> shorterThanFour = null; Assert.assertEquals("in, can, be, by, of", shorterThanFour.collect(Collectors.joining(", "))); }
do this List<String> usingSideEffect = new ArrayList<>(); stream.filter(s -> s.startsWith("b")) .forEach(usingSideEffect::add); // Do this instead List<String> usingReduce = stream.filter(s -> s.startsWith("b")) .collect(Collectors.toList());
Java SE 8 Lambda/Streams * and remove the @Ignore tag. Then run the test. */ public class TimeLab { // Exercise 1: Print the current date and time in ISO 8601 format // (https://en.wikipedia.org/wiki/ISO_8601) @Test @Ignore public void printNow() { ZonedDateTime now = null; // TODO System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); System.out.println(now.format(DateTimeFormatter.ISO_DATE_TIME)); }
lambdauttryck • IntelliJ har ibland svårt när man mappar om typer i lambdauttryck • Terminera inte din Stream innan du är klar! • IntelliJ är pigg på att föreslå att traditionell kod kan ersättas med lambdauttryck
of this stream that match * the given predicate. * * This is an intermediate operation. * * @param predicate a non-interfering, stateless predicate to apply to each * element to determine if it should be included * @return the new stream */ Stream<T> filter(Predicate<? super T> predicate);
of applying the given * function to the elements of this stream. * * This is an intermediate operation. * * @param <R> The element type of the new stream * @param mapper a non-interfering, stateless function to apply to each element * @return the new stream */ <R> Stream<R> map(Function<? super T, ? extends R> mapper);