1. Lambda expression
// 1. No parameter is required. The return value is 5 () -> 5 // 2. Receive a parameter (numeric type) and return twice its value x -> 2 * x // 3. Accept 2 parameters (numbers) and return their difference (x, y) -> x – y // 4. Receive two int integers and return their sum (int x, int y) -> x + y // 5. Accept a string object and print it on the console without returning any value (it looks like returning void) (String s) -> System.out.print(s) //Output Hello shawn @Test void test12(){ // Use parentheses GreetingService greetService = (message) -> System.out.println("Hello " + message); greetService.sayMessage("shawn"); } interface GreetingService { void sayMessage(String message); }
2. Functional interface
Function interface is an interface with only one abstract method, which is used as the type of Lambda expression. For a class decorated with @ FunctionalInterface annotation, the compiler will detect whether the class has only one abstract method or interface. Otherwise, an error will be reported. There can be multiple default methods, static methods.
Function interface | Abstract interface | function | parameter | Return type |
---|---|---|---|---|
Predicate | test(T t) | Judge true and false | T | boolean |
Consumer | accept(T t) | Consumption news | T | void |
Function | R apply(T t) | Map T to R | T | R |
Supplier | T get() | Production message | None | T |
UnaryOperator | T apply(T t) | Unary operation | T | T |
BinaryOperator | apply(T t,U u) | Binary operation | (T,U) | T |
Examples of common methods
@SpringBootTest public class Java8FunctionalTests { @Test void test01(){ Predicate<Integer> predicate = x -> x > 170; Student student = new Student("shawn", 175); System.out.println("shawn Is your height over 170?" + predicate.test(student.getHeight())); Consumer<String> consumer = System.out::println; consumer.accept("It's up to me. I don't want to die"); //Student mapped to String Function<Student, String> function = Student::getName; String name = function.apply(student); System.out.println(name); Supplier<Integer> supplier = () -> Integer.valueOf(BigDecimal.TEN.toString()); System.out.println(supplier.get()); UnaryOperator<Boolean> unaryOperator = flag -> !flag; Boolean apply2 = unaryOperator.apply(true); System.out.println(apply2); BinaryOperator<Integer> operator = (x, y) -> x * y; Integer integer = operator.apply(2, 3); System.out.println(integer); test(() -> "I am a demo of the functional interface"); } /** * Demonstrate the use of custom functional interfaces * * @param worker */ public static void test(Worker worker) { String work = worker.work(); System.out.println(work); } @FunctionalInterface public interface Worker { String work(); } }
3. Stream streaming programming
A Stream is a queue of elements from a data source and supports aggregation operations
- Elements are specific types of objects that form a queue. Stream in Java does not store elements, but calculates on demand.
- The source of the data stream. It can be set, array, I/O channel, generator, etc.
- Aggregation operations are similar to SQL statements, such as filter, map, reduce, find, match, sorted, etc.
1. collect(Collectors.toList())
Convert the flow to list. And toSet(), toMap(), etc. Evaluate early.
@Test void test01(){ List<Student> studentList = Stream.of( new Student("shawn",165), new Student("shawn22",170)) .collect(Collectors.toList()); System.out.println(studentList); } //[Student(name=shawn, height=165), Student(name=shawn22, height=170)]
2. forEach
Stream provides a new method forEach to iterate over each data in the stream
@Test void test02(){ Random random = new Random(); random.ints().limit(10).sorted().forEach(System.out::println); }
3. filter
The role of filtering. The internal interface is the Predicate interface. Lazy evaluation.
@Test void test03(){ List<Student> studentList = Stream.of( new Student("shawn",165), new Student("shawn22",170)) .filter(s -> s.getHeight()>165) .collect(Collectors.toList()); System.out.println(studentList); } //[Student(name=shawn22, height=170)]
3. map
The conversion Function is internally the Function interface. Inert evaluation
@Test void test04(){ List<String> studentList = Stream.of( new Student("shawn",165), new Student("shawn22",170)) .filter(s -> s.getHeight() > 165) .map(s-> s.getName()) .collect(Collectors.toList()); System.out.println(studentList); } //[shawn22]
5. flatMap
Merge multiple streams into one Stream. Inert evaluation
@Test void test05(){ List<Student> studentList = Arrays.asList( new Student("shawn",165), new Student("shawn22",170)); Stream.of(studentList, Collections.singletonList( new Student("shawn222", 180))) .flatMap(Collection::stream) .map(s->s.getName()) .forEach(System.out::println); } //shawn //shawn22 //shawn222
6. max and min
Find the maximum and minimum values in the set and evaluate as soon as possible. maxBy or minBy is to find the maximum and minimum values.
@Test void test06(){ List<Student> studentList = Arrays.asList( new Student("shawn",165), new Student("shawn22",170)); Optional<Student> student = studentList.stream() .min(Comparator.comparing(Student::getHeight)); if(student.isPresent()){ System.out.println(student.get()); } } //Student(name=shawn, height=165)
7. count
The statistics function is generally used in combination with filter, because we can filter out what we need first and then make statistics. Eager evaluation
@Test void test07(){ List<Student> studentList = Arrays.asList( new Student("shawn",165), new Student("shawn22",170)); long count = studentList.stream().filter(s -> s.getHeight() > 165).count(); System.out.println(count); } //1
8. reduce
The reduce operation can generate a value from a set of values
@Test void test08(){ System.out.println(Stream.of(1, 2, 3, 4, 5).reduce(10, Integer::sum)); } //25
9. collect advanced usage
//It is divided into two sets: true and false @Test void test09(){ List<Student> studentList = Arrays.asList( new Student("shawn",165), new Student("shawn22",170)); System.out.println(studentList.stream() .collect(Collectors.partitioningBy(s -> s.getName().contains("shawn")))); } //{false=[], true=[Student(name=shawn, height=165), Student(name=shawn22, height=170)]} //Collectors.groupingBy is the same as the group by operation in SQL. @Test void test010(){ List<Student> studentList = Arrays.asList( new Student("shawn",165), new Student("shawn22",170)); System.out.println(studentList.stream() .collect(Collectors.groupingBy(Student::getName))); } //String splicing @Test void test011(){ List<Student> studentList = Arrays.asList( new Student("shawn",165), new Student("shawn22",170)); System.out.println(studentList.stream().map(Student::getName) .collect(Collectors.joining(",","[","]"))); } //[shawn,shawn22]
4. Optional class
The Optional class is a nullable container object. The purpose is to resolve null pointer exceptions.
1. empty()
Returns an Optional container object instead of null. Recommended common ⭐⭐⭐⭐
2. get()
If there is a value in the created option, this value will be returned; otherwise, NoSuchElementException will be thrown. Never use it directly before judging the air! Try not to use it! ⭐
//Will report an error Java util. NoSuchElementException: No value present @Test void test01(){ Optional<User> opt = Optional.empty(); System.out.println(opt.get()); }
3. of(T value)
Create an Optional object, and throw NPE if value is null. Not recommended ⭐⭐
4. ofNullable(T value)
As above, create an Optional object, but when value is null, it returns Optional empty(). Recommended use ⭐⭐⭐⭐⭐
@Test void test02(){ User user = null; //Output optional Empty, if any, output the value System.out.println(Optional.ofNullable(user)); //Will report an error Java lang.NullPointerException System.out.println(Optional.of(user)); }
5. orElse(T other)
It also returns the value wrapped in Optional, but the difference is that when the value cannot be obtained, it returns the default you specify. Can use ⭐⭐⭐
6. orElseGet(Supplier<? extends T> other)
If there is a value in the created option, this value is returned; otherwise, a value generated by the Supplier interface is returned. Recommended use ⭐⭐⭐⭐⭐
private User createNewUser() { System.out.println("user Method creation"); return new User("shawn", "male"); } @Test public void test03() { User user = null; //If the following two are empty, they will call the local method to create a new one, but orElse will execute the method regardless of whether there is a value or not, but the other will not User result = Optional.ofNullable(user).orElse(createNewUser()); User result2 = Optional.ofNullable(user).orElseGet(this::createNewUser); System.out.println(result); System.out.println(result2); } /*The results show that user Method creation user Method creation User(name=shawn, sex=(male) User(name=shawn, sex=(male) */
7. orElseThrow(Supplier<? extends X> exceptionSupplier)
If there is a value in the created option, this value will be returned. Otherwise, an exception generated by the specified Supplier interface will be thrown. Recommended for blocking business scenarios ⭐⭐⭐⭐
@Test public void test04() { User user = null; User result = Optional.ofNullable(user).orElseThrow(NullPointerException::new); } //Output Java lang.NullPointerException
8. isPresent()
If the value in the created option exists, return true; otherwise, return false. It is useful in some cases, but try not to use it in if judgment. Can use ⭐⭐⭐
9. ifPresent(Consumer<? super T> consumer)
Judge whether there is a value in Optional. If there is a value, execute consumer, otherwise nothing will be done. Use this on a daily basis ⭐⭐⭐⭐
10. filter(Predicate<? super T> predicate)
If the value in the created Optional satisfies the conditions in the filter, the Optional object containing the value is returned; otherwise, an empty Optional object is returned
11. map
If the value in the created option exists, the provided Function function call is performed on the value
@Test public void test05() { User user = new User("shawn", "male"); String sex = Optional.ofNullable(user) .map(User::getSex).orElse("female"); System.out.println(sex); } //Output male
12. flagMap
If the value in the created option exists, the provided Function function call will be executed on the value to return a value of option type; otherwise, an empty option object will be returned, and the returned value is the unpacked value
//This method is defined in the User class public Optional<String> getPosition() { return Optional.ofNullable(name); } @Test public void test06() { User user = new User("shawn", "male"); String sex = Optional.ofNullable(user) .flatMap(User::getPosition).orElse("female"); System.out.println(sex); } //Output male
13. filter
filter() accepts a Predicate parameter and returns a value with the test result of true. If the test result is false, an empty Optional is returned.
@Test public void test07() { User user = new User("shawn", "male"); Optional<User> result = Optional.ofNullable(user) .filter(u -> u.getName() != null && u.getSex().contains("female")); System.out.println(result); } //Return to optional empty
14. Others
Java 9 adds three methods to the Optional class: or(), ifpresentoelse (), and stream(). The or() method is similar to orElse() and orElseGet(), both of which provide alternatives when the object is empty. The return value of or() is another Optional object generated by the Supplier parameter.
5,Base64
The base64 tool class provides a set of static methods to obtain the following three Base64 codecs:
- **Basic: * * the output is mapped to a group of characters A-Za-z0-9 + /, no line mark is added to the encoding, and the decoding of the output only supports A-Za-z0-9 + /.
- **URL: * * the output is mapped to a set of characters A-Za-z0-9 +, The output is URL and file.
- **MIME: * * output is insinuated to MIME friendly format. Output no more than 76 characters per line, and use '\ r' followed by '\ n' as segmentation. There is no line segmentation at the end of the encoded output.
@Test void test01() throws Exception { // code String B64 = Base64.getEncoder().encodeToString("hello?world".getBytes(StandardCharsets.UTF_8)); System.out.println(B64); // Output: aGVsbG8/d29ybGQ= // decode byte[] baseBytes = Base64.getDecoder().decode("aGVsbG8/d29ybGQ="); System.out.println(new String(baseBytes, StandardCharsets.UTF_8)); // The output is: hello?world String urlB64 = Base64.getUrlEncoder().encodeToString("hello?world".getBytes(StandardCharsets.UTF_8)); System.out.println(urlB64); // Output: aGVsbG8_d29ybGQ= String mineB64 = Base64.getMimeEncoder().encodeToString("hello?world".getBytes(StandardCharsets.UTF_8)); System.out.println(mineB64); // Output: aGVsbG8/d29ybGQ= }
6. Java 8 datetime
New Java Time covers all operations dealing with date, time, date / time, time zone, instances, process and clock.
@Test void test(){ // Gets the current date and time LocalDateTime currentTime = LocalDateTime.now(); //Current time: 2021-08-05T12:06:15.590185 System.out.println("current time : " + currentTime); LocalDate date1 = currentTime.toLocalDate(); //Current date: August 5, 2021 System.out.println("current date: " + date1); Month month = currentTime.getMonth(); int month1 = currentTime.get(ChronoField.MONTH_OF_YEAR); int day = currentTime.getDayOfMonth(); int seconds = currentTime.getSecond(); //Month: 8, day: 5, second: 15 System.out.println("month: " + month1 +", day: " + day +", second: " + seconds); LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2022); //date2: 2022-08-10T12:06:15.590185 System.out.println("date2: " + date2); LocalDate date3 = LocalDate.of(2022, Month.DECEMBER, 12); //date3: 2022-12-12 System.out.println("date3: " + date3); LocalTime date4 = LocalTime.of(22, 15); //date4: 22:15 System.out.println("date4: " + date4); // Parse string LocalTime date5 = LocalTime.parse("20:15:30"); //date5: 20:15:30 System.out.println("date5: " + date5); Instant instant = Instant.now(); long currentMilli = instant.toEpochMilli(); //Current milliseconds: 1628136375597 System.out.println("Current milliseconds:"+currentMilli); // Get current time and date ZonedDateTime date6 = ZonedDateTime.parse("2015-12-03T10:15:30+05:30[Asia/Shanghai]"); //date6: 2021-08-05 System.out.println("date6: " + date1); ZoneId id = ZoneId.of("Europe/Paris"); //ZoneId: Europe/Paris System.out.println("ZoneId: " + id); ZoneId currentZone = ZoneId.systemDefault(); //Current time zone: Asia/Shanghai System.out.println("Current time zone: " + currentZone); }
Reference article:
https://www.matools.com/api/java8
https://www.runoob.com/java/java8-new-features.html
https://mp.weixin.qq.com/s/8n_3VaAcwauGHgoSG1K14g