To create a list with a single element in Java, you can use the Collections.singletonList method from the java.util package.
Here is an example of how to create a list with a single element:
import java.util.List;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
List<String> list = Collections.singletonList("apple");
System.out.println(list); // prints [apple]
}
}
This creates a new list with the single element "apple".
You can also create a list with a single element using the Arrays.asList method from the java.util package. For example:
import java.util.List;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
List<String> list = Arrays.asList("apple");
System.out.println(list); // prints [apple]
}
}