What are the new methods in string class in Java 11?

Michelle 150 Published: 08/14/2024

What are the new methods in string class in Java 11?

Java 11 introduced several new methods to the String class, which provide more functionality and flexibility when working with strings. Some of these new methods include:

lines() Method: This method returns a stream of strings that are obtained by splitting this string using an appropriate delimiter.

Example:

String str = "Hello,nWorld!";

List lines = str.lines();

System.out.println(lines); // prints [Hello!, World!]

repeat(int) Method: This method returns a new String that contains count copies of this string.

Example:

String str = "Hello";

String repeatedStr = str.repeat(3);

System.out.println(repeatedStr); // prints HelloHelloHello

isBlank() and isEmpty() Methods: These two methods check if the string is blank or not. The difference between these two methods is that isEmpty() checks if the string contains no characters at all, while isBlank() checks if the string contains only whitespace.

Example:

String str = "   ";

System.out.println(str.isBlank()); // prints true (since the string contains only whitespace)

formatted() Method: This method returns a formatted string using the specified format string and arguments.

Example:

String.format("Hello, %s!", "World");

// prints Hello, World!

normalize() Method: This method returns a normalized form of the string.

Example:

String str = "u00DC";

System.out.println(str.normalize()); // prints U+00DC (the NFC normalization form)

transform() and mapToInt() Methods: These two methods transform the characters in the string to uppercase or lowercase, or perform other transformations.

Example:

List list = List.of("Hello", "World");

Stream stream = list.stream()

.map(String::toUpperCase)

.collect(Collectors.toList());

System.out.println(stream); // prints [HELLO, WORLD]

transcode() Method: This method returns the transcoded form of the string.

Example:

String str = "Hello";

System.out.println(str.transcode()); // prints Hello (since no transcoding is needed)

equals(), hashCode(), and compareTo() Methods: These three methods provide more flexibility when comparing strings.

Example:

String str1 = "Hello";

String str2 = "HELLO";

System.out.println(str1.compareTo(str2)); // prints 0 (since the strings are equal)

These new methods in the String class of Java 11 provide more functionality and flexibility for working with strings, making it easier to handle common string-related tasks.

How many methods are there in a thread in Java?

I'll respond in English for you this time!

In Java, a thread is an independent sequence of instructions that can run concurrently with other threads. There are several ways to create and manage threads in Java, which we'll explore below:

1. Extends Thread class: The most common method of creating a thread in Java is by extending the Thread class. This involves defining a new class that inherits from Thread, overriding the run() method, and creating an instance of the new thread.

public class MyThread extends Thread {

public void run() {

// code to be executed in a separate thread

}

}

2. Implements Runnable interface: Another way to create a thread is by implementing the Runnable interface. This involves defining a new class that implements Runnable, overriding the run() method, and creating an instance of the new thread using the Thread class.

public class MyRunnable implements Runnable {

public void run() {

// code to be executed in a separate thread

}

}

3. Lambda expression: Java 8 introduced lambda expressions, which provide a concise way to create threads. You can create an anonymous Runnable instance and pass it to the Thread class.

new Thread(() -> {

// code to be executed in a separate thread

}).start();

4. Executor framework: The Executor framework provides a more robust way of managing threads, especially when dealing with large numbers of concurrent tasks. It involves creating an ExecutorService instance and submitting tasks (such as Runnable or Callable) to be executed.

public class MyExecutor {

public void submitTask(Runnable task) {

executorService.submit(task);

}

}

5. Java 8's CompletableFuture: Java 8 introduced the CompletableFuture class, which allows you to create a thread-safe future that can be used to handle asynchronous tasks.

public class MyFuture {

public CompletableFuture fetchString() {

return CompletableFuture.runAsync(() -> {

// code to be executed in a separate thread

return "result";

});

}

}

6. ScheduledExecutorService: The ScheduledExecutorService interface provides a way to schedule tasks to run at a specific time or after a delay.

public class MyScheduler {

public void scheduleTask(Runnable task, long delay) {

executorService.schedule(task, delay, TimeUnit.SECONDS);

}

}

These are some of the primary methods for creating and managing threads in Java. Each has its own strengths and use cases, depending on your specific requirements.

(Note: This response is in English as per your request!)