java 49 lines · 8 steps

Sorting Java imports with a regex pass

A single scan collects, dedupes, and sorts import statements, then splices a clean block back into the source.

Explained by highlit
1public final class ImportOrganizer {
2 
3 private static final Pattern IMPORT_LINE =
4 Pattern.compile("^import\\s+(static\\s+)?([\\w.]+(?:\\.\\*)?)\\s*;\\s*$");
5 
6 public String organize(String source) {
7 List<String> lines = source.lines().collect(Collectors.toList());
8 
9 SortedSet<String> staticImports = new TreeSet<>();
10 SortedSet<String> normalImports = new TreeSet<>();
11 
12 int firstImport = -1;
13 int lastImport = -1;
14 
15 for (int i = 0; i < lines.size(); i++) {
16 Matcher m = IMPORT_LINE.matcher(lines.get(i));
17 if (!m.matches()) {
18 continue;
19 }
20 if (firstImport < 0) {
21 firstImport = i;
22 }
23 lastImport = i;
24 
25 if (m.group(1) != null) {
26 staticImports.add(m.group(2));
27 } else {
28 normalImports.add(m.group(2));
29 }
30 }
31 
32 if (firstImport < 0) {
33 return source;
34 }
35 
36 List<String> block = new ArrayList<>();
37 staticImports.forEach(fqn -> block.add("import static " + fqn + ";"));
38 if (!staticImports.isEmpty() && !normalImports.isEmpty()) {
39 block.add("");
40 }
41 normalImports.forEach(fqn -> block.add("import " + fqn + ";"));
42 
43 List<String> result = new ArrayList<>(lines.subList(0, firstImport));
44 result.addAll(block);
45 result.addAll(lines.subList(lastImport + 1, lines.size()));
46 
47 return String.join(System.lineSeparator(), result) + System.lineSeparator();
48 }
49}
01 / 01
STEP 01

Walkthrough

Space play ←→ step click any line
Three takeaways
  1. 1A TreeSet gives you sorted, deduplicated output for free while you scan.
  2. 2Tracking first and last match indices lets you replace a contiguous region without re-parsing.
  3. 3A capturing group can classify a match (static vs normal) in the same pass that extracts it.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Sorting Java imports with a regex pass — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code