jdk 8 下载:

解析字符串为日期

ISO8601 格式转换

LocalDate.parse() or LocalDateTime.parse()

  1. String armisticeDate = "2019-0510";
  2. LocalDate aLD = LocalDate.parse(armisticeDate);
  3. System.out.println("Date: " + aLD);
  4. String armisticeDateTime = "2019-0510T11:50";
  5. LocalDateTime aLDT = LocalDateTime.parse(armisticeDateTime);
  6. System.out.println("Date/Time: " + aLDT);
  7. Output:
  8. Date: 2019-0510
  9. Date/Time: 2019-0510T11:50

自定义格式转换

DateTimeFormatter.ofPattern()

  1. String anotherDate = "10 Apr 2019";
  2. DateTimeFormatter df = DateTimeFormatter.ofPattern("dd MMM yyyy");
  3. LocalDate random = LocalDate.parse(anotherDate, df);
  4. System.out.println(anotherDate + " parses as " + random);

连接字符串或列表

加入多个字符串参数

  1. String joinedString = String.join(", ", "How", "To", "Do", "In", "Java");
  2. System.out.println(joinedString);
  3. Output:
  4. How, To, Do, In, Java

加入数组或字符串列表

  1. List<String> strList = Arrays.asList("How", "To", "Do", "In", "Java");
  2. String joinedString = String.join(", ", strList);
  3. System.out.println(joinedString);
  4. Output:
  5. How, To, Do, In, Java

格式化输出

使用StringJoiner类,我们可以生成连接字符串的格式化输出。这在使用lambda收集器时特别有用。

  1. StringJoiner joiner = new StringJoiner(", ", "[", "]");
  2. joiner.add("How")
  3. .add("To")
  4. .add("Do")
  5. .add("In")
  6. .add("Java");
  7. Output:
  8. [How, To, Do, In, Java]

带有Collectors.joining()的字符串的字符串列表

  1. List<String> numbers = Arrays.asList("How", "To", "Do", "In", "Java");
  2. String joinedString = numbers
  3. .stream()
  4. .collect(Collectors.joining(", ","[","]"));
  5. System.out.println(joinedString);
  6. Output:
  7. [How, To, Do, In, Java]

Base64

Base 64编码将二进制数据转换为64个可打印ASCII字符。通常,它用于电子邮件消息和"basic"HTTP身份验证中的二进制数据。这64个可打印的字符是:

  • 26个大写字母[A … Z]
  • 26个小写字母[a … z]
  • 10位[0 … 9]
  • 2个符号

ava 8为Base 64编码和解码目的添加了一个类,即java.util.Base64。我们将使用下面的代码示例来使用它。

1. 将字符串编码到base 64

这就像获取编码器的实例一样简单,并将字符串作为字节输入以对其进行编码。

  1. Base64.Encoder encoder = Base64.getEncoder();
  2. String normalString = "username:password";
  3. String encodedString = encoder.encodeToString(
  4. normalString.getBytes(StandardCharsets.UTF_8) );
  5. Output:
  6. dXNlcm5hbWU6cGFzc3dvcmQ=

2. 解码基数为64的编码字符串

这也很简单。只需获取Base64.Decoder的实例并使用它来解码基本64位编码的字符串。

  1. String encodedString = "dXNlcm5hbWU6cGFzc3dvcmQ=";
  2. Base64.Decoder decoder = Base64.getDecoder();
  3. byte[] decodedByteArray = decoder.decode(encodedString);
  4. //Verify the decoded string
  5. System.out.println(new String(decodedByteArray));
  6. Output:
  7. username:password

3. 换行到基本64编码的输出流

如果您不想直接使用数据而宁愿使用流,则可以包装输出流,以便写入此输出流的所有数据将自动进行64位编码。

  1. Path originalPath = Paths.get("c:/temp", "mail.txt");
  2. Path targetPath = Paths.get("c:/temp", "encoded.txt");
  3. Base64.Encoder mimeEncoder = Base64.getMimeEncoder();
  4. try(OutputStream output = Files.newOutputStream(targetPath)){
  5. //Copy the encoded file content to target file
  6. Files.copy(originalPath, mimeEncoder.wrap(output));
  7. //Or simply use the encoded output stream
  8. OutputStream encodedStrem = mimeEncoder.wrap(output);
  9. }

精确算术支持

  • Math.multiplyExact
  • Math.addExact
  • Math.floorMod(``10 , ``2``)
  • Math.nextDown(``100``)

参考:https://howtodoinjava.com/java8/java-8-exact-airthmetic-operations-supported-in-math-class/

比较器

快速参考:

  1. //Compare by Id
  2. Comparator<Employee> compareById_1 = Comparator.comparing(e -> e.getId());
  3. Comparator<Employee> compareById_2 = (Employee o1, Employee o2) -> o1.getId().compareTo( o2.getId() );
  4. //Compare by firstname
  5. Comparator<Employee> compareByFirstName = Comparator.comparing(e -> e.getFirstName());
  6. //how to use comparator
  7. Collections.sort(employees, compareById);

按名字排序-顺序

  1. List<Employee> employees = getEmployees();
  2. //Sort all employees by first name
  3. employees.sort(Comparator.comparing(e -> e.getFirstName()));
  4. //OR you can use below
  5. employees.sort(Comparator.comparing(Employee::getFirstName));

按名字排序-逆序

  1. List<Employee> employees = getEmployees();
  2. //Sort all employees by first name; And then reversed
  3. Comparator<Employee> comparator = Comparator.comparing(e -> e.getFirstName());
  4. employees.sort(comparator.reversed());

多字段排序

  1. List<Employee> employees = getEmployees();
  2. //Sorting on multiple fields; Group by.
  3. Comparator<Employee> groupByComparator = Comparator.comparing(Employee::getFirstName)
  4. .thenComparing(Employee::getLastName);
  5. employees.sort(groupByComparator);
  6. System.out.println(employees);

并行排序(多线程)

上千会很快。

  1. //Parallel Sorting
  2. Employee[] employeesArray = employees.toArray(new Employee[employees.size()]);
  3. //Parallel sorting
  4. Arrays.parallelSort(employeesArray, groupByComparator);
  5. System.out.println(employeesArray);

正则表达式做谓词

将Regex转换为谓词

我有不同域的电子邮件列表,我想只对域名为“example.com”的电子邮件ID执行某些操作。
现在使用[Pattern.compile().asPredicate()](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#asPredicate--)方法从编译的正则表达式中获取谓词。此谓词可以与lambda流一起使用,以将每个标记应用到流中。

  1. public class RegexPredicateExample {
  2. public static void main(String[] args) {
  3. // Compile regex as predicate
  4. Predicate<String> emailFilter = Pattern
  5. .compile("^(.+)@example.com$")
  6. .asPredicate();
  7. // Input list
  8. List<String> emails = Arrays.asList("alex@example.com", "bob@yahoo.com",
  9. "cat@google.com", "david@example.com");
  10. // Apply predicate filter
  11. List<String> desiredEmails = emails
  12. .stream()
  13. .filter(emailFilter)
  14. .collect(Collectors.<String>toList());
  15. // Now perform desired operation
  16. desiredEmails.forEach(System.out::println);
  17. }
  18. }

输出:
alex@example.com
david@example.com

老方式是使用:Pattern.matcher()