Friday, October 24, 2025
HomeLanguagesJavaSplitter limit() method | Guava | Java

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:

  • 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”].
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS