Posts

Showing posts with the label java 11

Java String repeat() method

Java String repeat() method String repeat() method added jdk 11. Returns a string whose value is the concatenation of this string repeated count times. If this string is empty or count is zero then the empty string is returned. Java Program Example: class StringRepeat{ public static void main(String args[]){ String str = "Two"; System.out.println(str.repeat(2)); } } output: Two Two

Java String lines() method

Java String lines() method String lines() method added jdk 11. Returns a stream of lines extracted from this string, separated by line terminators. A line terminator is one of the following: a line feed character "\n" (U+000A), a carriage return character "\r" (U+000D), or a carriage return followed immediately by a line feed "\r\n" (U+000D U+000A). A line is either a sequence of zero or more characters followed by a line terminator, or it is a sequence of one or more characters followed by the end of the string. A line does not include the line terminator. The stream returned by this method contains the lines from this string in the order in which they occur. Java Program Example: class StringLines{ public static void main(String args[]){ String str =  "this is a test example." str.lines().stream()......// process with stream api } }

Java String isBlank() method

Java String isBlank() method String isBlank() method added in jdk 11. isBlank method returns true if the string is empty or contains only white space codepoints, otherwise false. Java Program Example: class StringIsBlackExample{ public static void main(String args[]){ String str = "Test"; System.out.println(str.isBlank()); } } Output: false Method: public boolean isBlank() Returns true if the string is empty or contains only white space codepoints, otherwise false. Returns: true if the string is empty or contains only white space codepoints, otherwise false

Java - APIs for Creating Unmodifiable Collections

Several new APIs have been added that facilitate the creation of unmodifiable collections. The List.copyOf Set.copyOf Map.copyOf  methods create new collection instances from existing instances. New methods have been added to the Collectors class in the Stream package. toUnmodifiableList toUnmodifiableSet toUnmodifiableMap   These allow the elements of a Stream to be collected into an unmodifiable collection.

Lambda expressions - implicitly typed parameters

For formal parameters of implicitly typed lambda expressions, allow the reserved type name `var` to be used, so that:     (var x, var y) -> x.process(y) is equivalent to:     (x, y) -> x.process(y) An implicitly typed lambda expression must use `var` for all its formal parameters or for none of them. In addition, `var` is permitted only for the formal parameters of implicitly typed lambda expressions --- explicitly typed lambda expressions continue to specify manifest types for _all_ their formal parameters, so it is not permitted for some formal parameters to have manifest types while others use `var`. The following examples are illegal:     (var x, y) -> x.process(y)         // Cannot mix 'var' and 'no var' in implicitly typed lambda expression     (var x, int y) -> x.process(y)     // Cannot mix 'var' and manifest types in explicitly typed lambda expression In theory, it would be...

Java String strip() method Example

String strip() method String strip() method added in jdk 11. String strip()  Returns a string whose value is this string, with all leading and trailing white space removed. If this String object represents an empty string, or if all code points in this string are white space, then an empty string is returned. Otherwise, returns a substring of this string beginning with the first code point that is not a white space up to and including the last code point that is not a white space. This method may be used to strip white space from the beginning and end of a string. strip method remove the extra space from both side. Java Program Example: class StripMethodExample{ public static void main(String args[]){ //with var variable var test = "     hello!   "; System.out.println(test.strip()); //with String class String test1 = "      hello!   "; System.out.println(test1.strip()); } } Output: hello! hello! ...