Skip to content
Related Articles

Related Articles

Java String Class lines() Method with Examples
  • Last Updated : 09 Mar, 2021

lines() method is a static method which returns out stream of lines extracted from a given multi-line string, separated by line terminators which are as follows:

Line terminators Command 
Line feed character\n
A carriage return character\r
A carriage return followed immediately by a line feed\r\n

Syntax:

public Stream<String> lines()

Return Type: Stream of string in order as present in multi-line

Illustration:

Input  : "Geek \n For \n Geeks \n 2021"
Output :
     Geek
     For
     Geeks
     2021

Implementation:



Here we will be discussing out three examples to get better understanding of working of String class lines() method with data structures.

  • forEach
  • Converting stream of lines to ArrayList
  • Converting stream of lines to array

Let’s discuss them one by one:

Example 1: forEach 

Java




// Importing Stream class from
// java.util package
import java.util.stream.Stream;
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Custom input string
        String str
            = " Geeks \n For \n Geeks \r Technical \r\n content \r writer \n Internship";
  
        // Generating stream of lines from string
        // using line method
        Stream<String> lines = str.lines();
  
        // print and display the output string
        // using forEach aand scope resolution operator
        lines.forEach(System.out::println);
    }
}
Output
 Geeks 
 For 
 Geeks 
 Technical 
 content 
 writer 
 Internship

Example 2: Stream of lines to ArrayList using forEach

Java




// Java Program to illustrate String class lines() method
// by converting stream of lines to ArrayList 
  
// Importing ArrayList and Stream class
// from java.util package
import java.util.ArrayList;
import java.util.stream.Stream;
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Custom input string
        String str
            = " Geeks \n For \n Geeks \r Technical \r\n content \r writer \n Internship";
  
        // Generating stream of lines from string
        // using lines() method
        Stream<String> lines = str.lines();
  
        // Creating an ArrayList object of String type
        ArrayList<String> arrayList = new ArrayList<>();
  
        // Now, adding elements to arrayList using forEach
        lines.forEach(arrayList::add);
  
        // Print and display the ArrayList
        System.out.println(arrayList);
    }
}
Output
[ Geeks ,  For ,  Geeks ,  Technical ,  content ,  writer ,  Internship]

Example 3: Stream of lines to array

Java




// Java Program to illustrate String class lines() method
// by converting stream of lines to array 
  
// Importing Arrays and Stream class from
// java.util package
import java.util.Arrays;
import java.util.stream.Stream;
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Custom input string
        String str
            = " Geeks \n For \n Geeks \r Technical \r\n content \r writer \n Internship";
  
        // Generating stream of lines from
        // string using line() method
        Stream<String> lines = str.lines();
  
        // Converting into array
        // using toArray() method
        Object[] array = lines.toArray();
  
        // Print and display the array
        // using standard toString() method
        System.out.println(Arrays.toString(array));
    }
}
Output
[ Geeks ,  For ,  Geeks ,  Technical ,  content ,  writer ,  Internship]

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :