preface

This article focuses on the use of some of the new syntax features in Java, including Lambda, Stream, and LocalDate.

Lambda

  • Lambda expressions, also known as closures, are the most important new feature driving the release of Java 8.
  • Lambda allows functions to be passed as arguments to a method.
  • Using Lambda expressions can make the code more compact.

Lambda expression is an anonymous function. Lambda expression is named after the Lambda calculus in mathematics. It directly corresponds to the Lambda abstraction.

1. The structure of Lambda expressions

(parameters) -> expression 或 (parameters) ->{ statements; }
Copy the code

Using Lambda to iterate:

System.out.println(" Map Ramda expression traversal :"); Map. ForEach ((k, v) - > {System. Out. Println (" k = "+ k +", v = "+ v); });Copy the code

2.Stream

Stream provides a high-level abstraction of the operation and representation of Java collections in an intuitive way similar to querying data from a database in SQL statements. The Stream API can greatly increase the productivity of Java programmers, allowing them to write efficient, clean, and concise code. In this style, the collection of elements to be processed is treated as a stream, which is transported in a pipeline and can be processed at the nodes of the pipeline, such as filtering, sorting, aggregation, etc.

Java. Util. Stream. Collectors, starts the new JDK1.8 into a class. From the class annotations in the source code, we know that Collectors implement a variety of useful reduction operations, such as categorizing types to new collections, summarizing elements according to different standards, and so on. The example shows how a single line of code can handle such powerful and complex functions: summary, concatenation, cumulative calculation, grouping, etc.

Collectors use:

 *     Set<String> set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new));
 *
 *     // Convert elements to strings and concatenate them, separated by commas
 *     String joined = things.stream()
 *                           .map(Object::toString)
 *                           .collect(Collectors.joining(", "));
Copy the code

The filter using:

List<String> List = arrays. asList(" Arrays ", "li "," Wang ", "Xuwujing "); String result3 = list. Stream (). The filter (STR - > "bill" equals (STR)). FindAny (). OrElse (" can't find!" ); String result4 = list.stream().filter(STR -> "li2 ".equals(STR)).findany ().orelse (" not found!") ); // The stream is not found! // The stream is not found!Copy the code

Limit the use of:

Random rd = new Random(); System.out.println(" first three data retrieved :"); rd.ints().limit(3).forEach(System.out::println);Copy the code

Grouping: groupingBy/groupingByConcurrent

Grouping elements by condition is similar to using group by in SQL, and it is generally recommended to use Java code for grouping to relieve database SQL stress.

Grouping involves the following methods:

  • Collectors. GroupingBy (…). : Indicates a common group.
  • Collectors. GroupingByConcurrent (…). : A thread-safe group.

After grouping, a Map set is returned with key as the grouping object and value as the corresponding grouping result.

For example, given that there may be peers in the People set, group the set by age:

Map<Integer, List<People>> groupingByAge = allPeoples.stream().collect(Collectors.groupingBy(People::getAge));
Copy the code

3.LocalDate

  • Instant: Indicates the instantaneous time.
  • LocalDate: indicates the LocalDate in the format yyyy-mm-dd without the specific time.
  • LocalTime: indicates the LocalTime, excluding the date. The format is YYYY-MM-DD HH: MM: ss.sss.
  • LocalDateTime: Combines the date and time, but does not contain the time difference and time zone information.
  • ZonedDateTime: The most complete datetime, including the time zone and the time difference relative to UTC or Greenwich Mean Time.
LocalDateTime LDT = localDatetime.now (); System.out.println(" current year :"+ldt.getYear()); //2021 system.out.println (" number of days in the current year :"+ldt.getDayOfYear()); //172 system.out.println (" current month :"+ldt.getMonthValue()); //172 system.out.println (" current month :"+ldt.getMonthValue())); System.out.println(" current :"+ ldt.gethour ()); System.out.println(" current minute :"+ ldt.getminute ()); System.out.println(" current time :"+ ldt.tostring ()); // Current year :2021 // Current year Days :365 // Current month :8 // Current time :10 // Current minute :24 // Current time :2021-8-1T15:24:35.833Copy the code

The JVM features

JDK8 modifs in the JVM are:

  • New JVM tools: JDEps provides command line tools for analyzing class files.
  • Use metaSpace instead of a permanent partition
  • Native Memeory Trace (NMT) is added

A HashMap change

JDK8 optimizes the implementation of HashMap. The main optimization points include:

  • Change the list mode to a linked list or a red-black tree
  • This section describes how to modify the REsize to resolve the potential deadlock of JDK7 in the resize concurrent scenario
  • JDK1.7 uses an Entry array for storage, and JDK8 uses a Node or TreeNode array for storage

When the list is longer than 8, the memory structure of the list will be modified to a red-black tree.

The query efficiency is increased from O(N) to O(logN). When the length of the list is less than 6, the red-black tree mode degrades into a linked list.

The JDK7 list insert is inserted from the list head. When resize, the original list will be reversed.

The JDK8 inserts are inserted from the end of the list, so they are resized in the same order.