Exercises with java 11. For example String class, Files class, Collection interface, Lambdas, Reflection Api, others.
Project execution 🔝
See
Using the isBlank method 🔝
See solution
public class TestClass {
public static void main(String args[]) {
/**
* public boolean isBlank()
Returns true if the string is empty or contains only white space codepoints, otherwise false.
Returns:
true if the string is empty or contains only white space codepoints, otherwise false
Since:
11
See Also:
Character.isWhitespace(int)
*
*/
String firstString = "First String to test";
System.out.println("First String : "+firstString.isBlank());
String secondString = " ";
System.out.println("Second String : "+secondString.isBlank());
String thirdString = "";
System.out.println("Third String : "+thirdString.isBlank());
}
}
First String : false
Second String : true
Third String : true
Using the lines method 🔝
See solution
import java.util.stream.Stream;
public class Main
{
public static void main(String[] args)
{
try
{
String str = "A \n B \n C \n D";
Stream<String> lines = str.lines();
lines.forEach(System.out::println);
}
catch (Error e)
{
e.printStackTrace();
}
}
}
A
B
C
D
Using various methods 🔝
See solution
public class ExampleClass {
public static void main(String args[]) {
String multilineString = "Baeldung helps \n \n developers \n explore Java.";
List<String> lines = multilineString.lines()
.filter(line -> !line.isBlank())
.map(String::strip)
.collect(Collectors.toList());
assertThat(lines).containsExactly("Baeldung helps", "developers", "explore Java.");
}
}