Skip to content

Latest commit

 

History

History
203 lines (129 loc) · 3.29 KB

README.md

File metadata and controls

203 lines (129 loc) · 3.29 KB

Java_11_Exercises

Exercises with java 11. For example String class, Files class, Collection interface, Lambdas, Reflection Api, others.


Index 📜

See

String Methods




Project execution 🔝

See



String Methods

Using the isBlank method 🔝

Check if a string has empty spaces

See solution

Code

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());

   }
}

Console

First String : false
Second String : true
Third String : true


Using the lines method 🔝

Create a Java program to read a string and obtain the content as a stream of lines.

See solution

Code

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();
   }
 }
}

Console

A 
B 
C 
D


Using various methods 🔝

Extract non-blank deleted lines from a multi-line string.

See solution

Code

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.");
   }
}

Console