Open In App

Splitter limit() method | Guava | Java

The method limit(int limit) returns a splitter that behaves equivalently to this splitter but stops splitting after it reaches the limit. The limit defines the maximum number of items returned by the iterator, or the maximum size of the list returned by splitToList(java.lang.CharSequence).
For example, Splitter.on(‘, ‘).limit(3).split(“a, b, c, d”) returns an iterable containing [“a”, “b”, “c, d”].

Syntax:



public Splitter limit(int limit)

Parameters: This method takes limit as parameter which is the maximum number of items to be returned.

Return Value: This method returns a splitter with the desired configuration.



Example 1:




// Java code to show implementation of
// limit(int limit) method
// of Guava's Splitter Class
  
import com.google.common.base.Splitter;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a string variable
        String str = "geeks, .  for, .Hey.,  "
                     + "geeks, ., noida, ., classes";
  
        // Initially setting limit as 3
        System.out.println("When Limit is 3 : ");
  
        // Using limit(int limit) method which
        // stops splitting after it reaches the limit.
        Iterable<String> result = Splitter.on(',')
                                      .limit(3)
                                      .trimResults()
                                      .split(str);
        for (String temp : result) {
            System.out.println(temp);
        }
  
        // Setting limit as 4
        System.out.println("\n\nWhen Limit is 4 : ");
  
        // Using limit(int limit) method which
        // stops splitting after it reaches the limit.
        Iterable<String> result1 = Splitter.on(',')
                                       .limit(4)
                                       .trimResults()
                                       .split(str);
        for (String temp : result1) {
            System.out.println(temp);
        }
    }
}

Output:
When Limit is 3 : 
geeks
.  for
.Hey.,  geeks, ., noida, ., classes


When Limit is 4 : 
geeks
.  for
.Hey.
geeks, ., noida, ., classes

Example 2:




// Java code to show implementation of
// limit(int limit) method
// of Guava's Splitter Class
  
import com.google.common.base.Splitter;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a string variable
        String str = "Learn$,,Data $ structures"
                     + " 123$ to be $ best Coder..";
  
        // Initially setting limit as 2
        System.out.println("When Limit is 2 : ");
  
        // Using limit(int limit) method which
        // stops splitting after it reaches the limit.
        Iterable<String> result = Splitter.on('$')
                                      .limit(2)
                                      .trimResults()
                                      .split(str);
        for (String temp : result) {
            System.out.println(temp);
        }
  
        // Setting limit as 4
        System.out.println("\n\nWhen Limit is 4 : ");
  
        // Using limit(int limit) method which
        // stops splitting after it reaches the limit.
        Iterable<String> result1 = Splitter.on('$')
                                       .limit(4)
                                       .trimResults()
                                       .split(str);
        for (String temp : result1) {
            System.out.println(temp);
        }
    }
}

Output:
When Limit is 2 : 
Learn,,Data $ structures 123$ to be $ best Coder..


When Limit is 4 : 
Learn,,Data
structures 123
to be $ best Coder..

Note:


Article Tags :