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); } } } |
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); } } } |
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”].
Recommended Posts:
- Splitter trimResults() method | Guava | Java
- Splitter splitToList() method | Guava | Java
- Splitter fixedLength() method | Guava | Java
- Splitter omitEmptyStrings() method | Guava | Java
- Splitter withKeyValueSeparator(char separator) method | Guava | Java
- Splitter Class | Guava | Java
- stream.limit() method in Java
- ShortBuffer limit() method in Java with Examples
- IntBuffer limit() method in Java with Examples
- LongBuffer limit() method in Java with Examples
- FloatBuffer limit() Method in Java with Examples
- Java Guava | Floats.contains() method with Examples
- Java Guava | Shorts.max() method with Examples
- Java Guava | Chars.min() method with Examples
- Java Guava | Floats.min() method with Examples
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.



