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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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


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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

  • When omitting empty strings, the omitted strings do not count. Hence, Splitter.on(‘, ‘).limit(3).omitEmptyStrings().split(“a,,, b,,, c, d”) returns an iterable containing [“a”, “b”, “c, d”].
  • When trim is requested, all entries are trimmed, including the last. Hence Splitter.on(‘, ‘).limit(3).trimResults().split(” a, b, c, d “) results in [“a”, “b”, “c, d”].


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.