Missing toList() in Java Streams? Boost Productivity with IntelliJ Live Templates
The article discusses Java 8's Stream API limitations, especially the lack of a toList() method, and shows how IntelliJ IDEA's Live Templates can create shortcuts for common collectors, improving code readability and developer productivity.
Java 8 was released in March 2014, and by March 2015 we upgraded all production servers to it, migrating most code to lambda expressions, the Stream API and the new date/time API, also using Nashorn for dynamic scripting.
Among the new features, the Stream API is the most useful; collection operations appear everywhere and streams improve readability.
However, the Stream API only provides a few terminal operations such as reduce and findFirst; most other results must be obtained via collect. The Collectors utility offers convenient collectors like toList, toSet, joining, and groupingBy.
After converting 300k lines to streams, toList, toSet, and groupingBy became the most frequently used terminal operations. It feels odd that these are not directly available on the Stream interface, forcing code such as:
stringCollection
.stream()
.filter(e -> e.startsWith("a"))
.collect(Collectors.toList());instead of a simpler:
stringCollection
.stream()
.filter(e -> e.startsWith("a"))
.toList();This omission becomes annoying when writing such code repeatedly. I wish convenient collectors like toList() were added to the Stream interface in Java 9.
Note: Stream.js provides a JavaScript interface for Java 8’s Stream API in browsers, exposing all important terminal operations directly on the stream.
IntelliJ IDEA, touted as the smartest Java IDE, offers a solution via its Live Templates feature.
Using IntelliJ IDEA to Help
Live Templates let you expand short abbreviations into larger code snippets. For example, typing sout and pressing TAB inserts System.out.println().
To address the missing toList method, you can create your own Live Templates for the most common collectors. Create a template that expands .toList into .collect(Collectors.toList()), and similarly for .toSet, .join, and .groupBy.
In IntelliJ, go to Settings → Live Templates, add a new group named “Stream”, and define templates with the Java → Other context.
// Abbreviation: .toList
.collect(Collectors.toList())
// Abbreviation: .toSet
.collect(Collectors.toSet())
// Abbreviation: .join
.collect(Collectors.joining("$END$"))
// Abbreviation: .groupBy
.collect(Collectors.groupingBy(e -> $END$))The special variable $END$ places the cursor after the expanded code, allowing you to type additional arguments such as a delimiter for joining.
Tip: Enable “Add unambiguous imports on the fly” so IDEA automatically adds the import for java.util.stream.Collectors (Editor → General → Auto Import).
Demo: Join and Group
After creating the templates, you can quickly generate code for joining and grouping operations, as shown in the screenshots below.
IntelliJ’s Live Templates are flexible and powerful, dramatically increasing coding productivity.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
