I just started using jshell in intellij idea community version. When I write the below code in jshell, it works.
List<String> list = List.of("a", "b", "c");
System.out.println(list);
However the same code doesn't work in intellij. It says "Expression expected". It executes fine but shows that there is error with List<String>
. The problem is "auto-complete" doesn't work. To workaround this issue, we can use raw type but I want a generic one. Is there something that I am missing? How can I write a generic type?
Convert Raw Types to Generics refactoring If you want to apply generics to a single code fragment, select one in the editor. On the main menu, or on the context menu, select Refactor | Convert Raw Types to Generics. In the dialog that opens, define the refactoring options. Preview and apply changes.
Open the JShell ConsoleFrom the main menu, select Tools | JShell Console.
also What is Generify in Android? Generify refactoring The Generify refactoring is designed to transform existing code that does not use Generics, into the Generics-aware code. The refactoring analyzes existing code, and for each raw type creates safe and consistent parameter type.
Not an IntelliJ IDEA expert but I get my way around by writing a anonymous class:
new Object() {
void method() {
List<String> list = List.of("a", "b", "c");
System.out.println(list);
}
}.method();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With