| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
0 parents commit ef919c8
17 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,4 @@ | |||
| 1 | + .DS_Store | ||
| 2 | + .idea | ||
| 3 | + *.iml | ||
| 4 | + out | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,47 @@ | |||
| 1 | + package com.winterbe.java8; | ||
| 2 | + | ||
| 3 | + import java.lang.annotation.ElementType; | ||
| 4 | + import java.lang.annotation.Repeatable; | ||
| 5 | + import java.lang.annotation.Retention; | ||
| 6 | + import java.lang.annotation.RetentionPolicy; | ||
| 7 | + import java.lang.annotation.Target; | ||
| 8 | + | ||
| 9 | + /** | ||
| 10 | + * @author Benjamin Winterberg | ||
| 11 | + */ | ||
| 12 | + public class Annotations1 { | ||
| 13 | + | ||
| 14 | + @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE}) | ||
| 15 | + @interface MyAnnotation { | ||
| 16 | + | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + @Retention(RetentionPolicy.RUNTIME) | ||
| 20 | + @interface Hints { | ||
| 21 | + Hint[] value(); | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + @Repeatable(Hints.class) | ||
| 25 | + @Retention(RetentionPolicy.RUNTIME) | ||
| 26 | + @interface Hint { | ||
| 27 | + String value(); | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + @Hint("hint1") | ||
| 31 | + @Hint("hint2") | ||
| 32 | + class Person { | ||
| 33 | + | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + public static void main(String[] args) { | ||
| 37 | + Hint hint = Person.class.getAnnotation(Hint.class); | ||
| 38 | + System.out.println(hint); // null | ||
| 39 | + | ||
| 40 | + Hints hints1 = Person.class.getAnnotation(Hints.class); | ||
| 41 | + System.out.println(hints1.value().length); // 2 | ||
| 42 | + | ||
| 43 | + Hint[] hints2 = Person.class.getAnnotationsByType(Hint.class); | ||
| 44 | + System.out.println(hints2.length); // 2 | ||
| 45 | + | ||
| 46 | + } | ||
| 47 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,37 @@ | |||
| 1 | + package com.winterbe.java8; | ||
| 2 | + | ||
| 3 | + import java.util.UUID; | ||
| 4 | + import java.util.concurrent.ConcurrentHashMap; | ||
| 5 | + | ||
| 6 | + /** | ||
| 7 | + * @author Benjamin Winterberg | ||
| 8 | + */ | ||
| 9 | + public class Concurrency1 { | ||
| 10 | + | ||
| 11 | + public static void main(String[] args) { | ||
| 12 | + ConcurrentHashMap<Integer, UUID> concurrentHashMap = new ConcurrentHashMap<>(); | ||
| 13 | + | ||
| 14 | + for (int i = 0; i < 100; i++) { | ||
| 15 | + concurrentHashMap.put(i, UUID.randomUUID()); | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + int threshold = 1; | ||
| 19 | + | ||
| 20 | + concurrentHashMap.forEachValue(threshold, System.out::println); | ||
| 21 | + | ||
| 22 | + concurrentHashMap.forEach((id, uuid) -> { | ||
| 23 | + if (id % 10 == 0) { | ||
| 24 | + System.out.println(String.format("%s: %s", id, uuid)); | ||
| 25 | + } | ||
| 26 | + }); | ||
| 27 | + | ||
| 28 | + UUID searchResult = concurrentHashMap.search(threshold, (id, uuid) -> { | ||
| 29 | + if (String.valueOf(uuid).startsWith(String.valueOf(id))) { | ||
| 30 | + return uuid; | ||
| 31 | + } | ||
| 32 | + return null; | ||
| 33 | + }); | ||
| 34 | + | ||
| 35 | + System.out.println(searchResult); | ||
| 36 | + } | ||
| 37 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,35 @@ | |||
| 1 | + package com.winterbe.java8; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * @author Benjamin Winterberg | ||
| 5 | + */ | ||
| 6 | + public class Interface1 { | ||
| 7 | + | ||
| 8 | + interface Formula { | ||
| 9 | + double calculate(int a); | ||
| 10 | + | ||
| 11 | + default double sqrt(int a) { | ||
| 12 | + return Math.sqrt(positive(a)); | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + static int positive(int a) { | ||
| 16 | + return a > 0 ? a : 0; | ||
| 17 | + } | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + public static void main(String[] args) { | ||
| 21 | + Formula formula1 = new Formula() { | ||
| 22 | + @Override | ||
| 23 | + public double calculate(int a) { | ||
| 24 | + return sqrt(a * 100); | ||
| 25 | + } | ||
| 26 | + }; | ||
| 27 | + | ||
| 28 | + formula1.calculate(100); // 100.0 | ||
| 29 | + formula1.sqrt(-23); // 0.0 | ||
| 30 | + Formula.positive(-4); // 0.0 | ||
| 31 | + | ||
| 32 | + // Formula formula2 = (a) -> sqrt( a * 100); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,34 @@ | |||
| 1 | + package com.winterbe.java8; | ||
| 2 | + | ||
| 3 | + import java.util.Arrays; | ||
| 4 | + import java.util.Collections; | ||
| 5 | + import java.util.Comparator; | ||
| 6 | + import java.util.List; | ||
| 7 | + | ||
| 8 | + /** | ||
| 9 | + * @author Benjamin Winterberg | ||
| 10 | + */ | ||
| 11 | + public class Lambda1 { | ||
| 12 | + | ||
| 13 | + public static void main(String[] args) { | ||
| 14 | + List<String> names = Arrays.asList("peter", "anna", "mike", "xenia"); | ||
| 15 | + | ||
| 16 | + Collections.sort(names, new Comparator<String>() { | ||
| 17 | + @Override | ||
| 18 | + public int compare(String a, String b) { | ||
| 19 | + return b.compareTo(a); | ||
| 20 | + } | ||
| 21 | + }); | ||
| 22 | + | ||
| 23 | + Collections.sort(names, (String a, String b) -> { | ||
| 24 | + return b.compareTo(a); | ||
| 25 | + }); | ||
| 26 | + | ||
| 27 | + Collections.sort(names, (String a, String b) -> b.compareTo(a)); | ||
| 28 | + | ||
| 29 | + Collections.sort(names, (a, b) -> b.compareTo(a)); | ||
| 30 | + | ||
| 31 | + System.out.println(names); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,47 @@ | |||
| 1 | + package com.winterbe.java8; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * @author Benjamin Winterberg | ||
| 5 | + */ | ||
| 6 | + public class Lambda2 { | ||
| 7 | + | ||
| 8 | + @FunctionalInterface | ||
| 9 | + public static interface Converter<F, T> { | ||
| 10 | + T convert(F from); | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + static class Something { | ||
| 14 | + String startsWith(String s) { | ||
| 15 | + return String.valueOf(s.charAt(0)); | ||
| 16 | + } | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + interface PersonFactory<P extends Person> { | ||
| 20 | + P create(String firstName, String lastName); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + public static void main(String[] args) { | ||
| 24 | + Converter<String, Integer> integerConverter1 = (from) -> Integer.valueOf(from); | ||
| 25 | + Integer converted1 = integerConverter1.convert("123"); | ||
| 26 | + System.out.println(converted1); // result: 123 | ||
| 27 | + | ||
| 28 | + | ||
| 29 | + // method reference | ||
| 30 | + | ||
| 31 | + Converter<String, Integer> integerConverter2 = Integer::valueOf; | ||
| 32 | + Integer converted2 = integerConverter2.convert("123"); | ||
| 33 | + System.out.println(converted2); // result: 123 | ||
| 34 | + | ||
| 35 | + | ||
| 36 | + Something something = new Something(); | ||
| 37 | + | ||
| 38 | + Converter<String, String> stringConverter = something::startsWith; | ||
| 39 | + String converted3 = stringConverter.convert("Java"); | ||
| 40 | + System.out.println(converted3); // result J | ||
| 41 | + | ||
| 42 | + // constructor reference | ||
| 43 | + | ||
| 44 | + PersonFactory<Person> personFactory = Person::new; | ||
| 45 | + Person person = personFactory.create("Peter", "Parker"); | ||
| 46 | + } | ||
| 47 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,84 @@ | |||
| 1 | + package com.winterbe.java8; | ||
| 2 | + | ||
| 3 | + import java.util.Comparator; | ||
| 4 | + import java.util.Objects; | ||
| 5 | + import java.util.UUID; | ||
| 6 | + import java.util.concurrent.Callable; | ||
| 7 | + import java.util.function.Consumer; | ||
| 8 | + import java.util.function.Function; | ||
| 9 | + import java.util.function.Predicate; | ||
| 10 | + import java.util.function.Supplier; | ||
| 11 | + | ||
| 12 | + /** | ||
| 13 | + * Common standard functions from the Java API. | ||
| 14 | + * | ||
| 15 | + * @author Benjamin Winterberg | ||
| 16 | + */ | ||
| 17 | + public class Lambda3 { | ||
| 18 | + | ||
| 19 | + @FunctionalInterface | ||
| 20 | + interface Fun { | ||
| 21 | + void foo(); | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + public static void main(String[] args) throws Exception { | ||
| 25 | + | ||
| 26 | + // Predicates | ||
| 27 | + | ||
| 28 | + Predicate<String> predicate = (s) -> s.length() > 0; | ||
| 29 | + | ||
| 30 | + predicate.test("foo"); // true | ||
| 31 | + predicate.negate().test("foo"); // false | ||
| 32 | + | ||
| 33 | + Predicate<Boolean> nonNull = Objects::nonNull; | ||
| 34 | + Predicate<Boolean> isNull = Objects::isNull; | ||
| 35 | + | ||
| 36 | + Predicate<String> isEmpty = String::isEmpty; | ||
| 37 | + Predicate<String> isNotEmpty = isEmpty.negate(); | ||
| 38 | + | ||
| 39 | + | ||
| 40 | + // Functions | ||
| 41 | + | ||
| 42 | + Function<String, Integer> toInteger = Integer::valueOf; | ||
| 43 | + Function<String, String> backToString = toInteger.andThen(String::valueOf); | ||
| 44 | + | ||
| 45 | + backToString.apply("123"); // "123" | ||
| 46 | + | ||
| 47 | + | ||
| 48 | + // Suppliers | ||
| 49 | + | ||
| 50 | + Supplier<Person> personSupplier = Person::new; | ||
| 51 | + personSupplier.get(); // new Person | ||
| 52 | + | ||
| 53 | + | ||
| 54 | + // Consumers | ||
| 55 | + | ||
| 56 | + Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName); | ||
| 57 | + greeter.accept(new Person("Luke", "Skywalker")); | ||
| 58 | + | ||
| 59 | + | ||
| 60 | + | ||
| 61 | + // Comparators | ||
| 62 | + | ||
| 63 | + Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName); | ||
| 64 | + | ||
| 65 | + Person p1 = new Person("John", "Doe"); | ||
| 66 | + Person p2 = new Person("Alice", "Wonderland"); | ||
| 67 | + | ||
| 68 | + comparator.compare(p1, p2); // > 0 | ||
| 69 | + comparator.reversed().compare(p1, p2); // < 0 | ||
| 70 | + | ||
| 71 | + | ||
| 72 | + // Runnables | ||
| 73 | + | ||
| 74 | + Runnable runnable = () -> System.out.println(UUID.randomUUID()); | ||
| 75 | + runnable.run(); | ||
| 76 | + | ||
| 77 | + | ||
| 78 | + // Callables | ||
| 79 | + | ||
| 80 | + Callable<UUID> callable = UUID::randomUUID; | ||
| 81 | + callable.call(); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,41 @@ | |||
| 1 | + package com.winterbe.java8; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * @author Benjamin Winterberg | ||
| 5 | + */ | ||
| 6 | + public class Lambda4 { | ||
| 7 | + | ||
| 8 | + static int outerStaticNum; | ||
| 9 | + | ||
| 10 | + int outerNum; | ||
| 11 | + | ||
| 12 | + void testScopes() { | ||
| 13 | + int num = 1; | ||
| 14 | + | ||
| 15 | + Lambda2.Converter<Integer, String> stringConverter = | ||
| 16 | + (from) -> String.valueOf(from + num); | ||
| 17 | + | ||
| 18 | + String convert = stringConverter.convert(2); | ||
| 19 | + System.out.println(convert); // 3 | ||
| 20 | + | ||
| 21 | + Lambda2.Converter<Integer, String> stringConverter2 = (from) -> { | ||
| 22 | + outerNum = 13; | ||
| 23 | + return String.valueOf(from); | ||
| 24 | + }; | ||
| 25 | + | ||
| 26 | + String[] array = new String[1]; | ||
| 27 | + Lambda2.Converter<Integer, String> stringConverter3 = (from) -> { | ||
| 28 | + array[0] = "Hi there"; | ||
| 29 | + return String.valueOf(from); | ||
| 30 | + }; | ||
| 31 | + | ||
| 32 | + stringConverter3.convert(23); | ||
| 33 | + | ||
| 34 | + System.out.println(array[0]); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + public static void main(String[] args) { | ||
| 38 | + new Lambda4().testScopes(); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,40 @@ | |||
| 1 | + package com.winterbe.java8; | ||
| 2 | + | ||
| 3 | + import java.time.DayOfWeek; | ||
| 4 | + import java.time.LocalDate; | ||
| 5 | + import java.time.Month; | ||
| 6 | + import java.time.format.DateTimeFormatter; | ||
| 7 | + import java.time.format.FormatStyle; | ||
| 8 | + import java.time.temporal.ChronoUnit; | ||
| 9 | + import java.util.Locale; | ||
| 10 | + | ||
| 11 | + /** | ||
| 12 | + * @author Benjamin Winterberg | ||
| 13 | + */ | ||
| 14 | + public class LocalDate1 { | ||
| 15 | + | ||
| 16 | + public static void main(String[] args) { | ||
| 17 | + LocalDate today = LocalDate.now(); | ||
| 18 | + LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS); | ||
| 19 | + LocalDate yesterday = tomorrow.minusDays(2); | ||
| 20 | + | ||
| 21 | + System.out.println(today); | ||
| 22 | + System.out.println(tomorrow); | ||
| 23 | + System.out.println(yesterday); | ||
| 24 | + | ||
| 25 | + LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4); | ||
| 26 | + DayOfWeek dayOfWeek = independenceDay.getDayOfWeek(); | ||
| 27 | + System.out.println(dayOfWeek); // FRIDAY | ||
| 28 | + | ||
| 29 | + DateTimeFormatter germanFormatter = | ||
| 30 | + DateTimeFormatter | ||
| 31 | + .ofLocalizedDate(FormatStyle.MEDIUM) | ||
| 32 | + .withLocale(Locale.GERMAN); | ||
| 33 | + | ||
| 34 | + LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter); | ||
| 35 | + System.out.println(xmas); // 2014-12-24 | ||
| 36 | + | ||
| 37 | + | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments