Refactor Static Map Initializations to Use Map.ofEntries()
- Dominant language
- Java
- Stars
- 2.1k
- Forks
- 522
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 137
Description
### Is your feature request related to a problem? Please describe.
Several locations in the codebase may initialize maps using a mutable
HashMap (or LinkedHashMap) followed by a sequence of put() calls, even
when the map contents are fixed after construction.
In such cases, the initialization can be simplified by using Java's
Map.ofEntries() factory method. This makes the code more concise,
improves readability, and clearly communicates that the map is intended
to be immutable.
Example:
Current:
Map values = new HashMap<>();
values.put("key1", "value1");
values.put("key2", "value2");
Suggested:
Map values = Map.ofEntries(
Map.entry("key1", "value1"),
Map.entry("key2", "value2")
);
Acceptance Criteria:
- Identify map initializations where entries are added immediately after
creation and the map is not modified later.
- Replace suitable cases with Map.ofEntries().
- Ensure behavior remains unchanged.
- Verify all existing tests continue to pass.
This is a refactoring and maintainability improvement with no intended
functional changes.
### Describe the solution you'd like
Several locations in the codebase may initialize maps using a mutable HashMap
(or LinkedHashMap) followed by a sequence of put() calls, even when the map
contents are fixed after construction.
These cases could be simplified using Java's Map.ofEntries() factory method,
which improves readability, reduces boilerplate, and clearly indicates that
the map is intended to be immutable.
Example:
Current:
Map values = new HashMap<>();
values.put("key1", "value1");
values.put("key2", "value2");
Suggested:
Map values = Map.ofEntries(
Map.entry("key1", "value1"),
Map.entry("key2", "value2")
);
### Describe alternatives you've considered
Keep the current mutable map initialization pattern. While it works correctly,
using Map.ofEntries() would make static map definitions more concise and
self-documenting.
### Additional context
This is a refactoring and maintainability improvement with no intended
behavioral changes.
Contributor guide
Research direction
Search the codebase for HashMap or LinkedHashMap constructions followed by put() calls. Inspect each candidate to confirm its entries are fixed after initialization and that Map.ofEntries() preserves the required behavior. Run the existing test suite after replacing all suitable cases; done means eligible static initializations use the factory without functional changes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100