Java Regular Expression Examples

Java 8 stream and regular expression examples. Note Learn the basic regular expression at Wikipedia 1. String.matches(regex) 1.1 This example, check if the string is a number. JavaRegEx1.java package com.mkyong.regex; import java.util.Arrays; import java.util.List; public class JavaRegEx1 { public static void main(String[] args) { List<String> numbers = Arrays.asList("1", "20", "A1", "333", "A2A211"); for (String number …

Read more

Java 8 Stream – Convert List<List<String>> to List<String>

As title, we can use flatMap to convert it. Java9Example1.java package com.mkyong.test; import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class Java9Example1 { public static void main(String[] args) { List<String> numbers = Arrays.asList("1", "2", "A", "B", "C1D2E3"); List<List<String>> collect = numbers.stream() .map(x -> new Scanner(x).findAll("\\D+") .map(m -> m.group()) .collect(Collectors.toList()) ) .collect(Collectors.toList()); collect.forEach(x -> System.out.println(x)); …

Read more

Java 8 flatMap example

This article explains the Java 8 Stream.flatMap and how to use it. Topic What is flatMap? Why flat a Stream? flatMap example – Find a set of books. flatMap example – Order and LineItems. flatMap example – Splits the line by spaces. flatMap and Primitive type 1. What is flatMap()? 1.1 Review the below structure. …

Read more

How to print an Array in Java

In this article, we will show you a few ways to print a Java Array. Table of contents. 1. JDK 1.5 Arrays.toString 2. Java 8 Stream APIs 3. Java 8 Stream APIs – Collectors.joining 4. Jackson APIs 5. References 1. JDK 1.5 Arrays.toString We can use Arrays.toString to print a simple array and Arrays.deepToString for …

Read more