[JAVA] Make a list map with LazyMap

Convert the information of prefectures in the DB to a map with a list of names as a value, using category as a key. (I don't know if I write it in Japanese, but in short I want to convert List \ <SampleEntity > to Map \ <String, List \ <String > >)

category name
Hokkaido Hokkaido
Tohoku region Aomori Prefecture
Tohoku region Iwate Prefecture
Tohoku region Miyagi Prefecture
Tohoku region Akita
Tohoku region Yamagata Prefecture
Tohoku region Fukushima Prefecture
Kanto region Ibaraki Prefecture
Kanto region Tochigi Prefecture
Kanto region Gunma Prefecture
Kanto region Saitama
Kanto region Chiba
Kanto region Tokyo
Kanto region Kanagawa Prefecture

If you try to do it normally, it is troublesome because you have to branch between the first time (when the list does not exist) and the second and subsequent times (when the list exists) ...


Map<String, List<String>> map = new HashMap<String, List<String>>();

for (SampleEntity en : entityList) {
    if (!map.containsKey(en.getCategory()) {
        map.put(en.getCategory(), new ArrayList<String>());

With LazyMap, you can write this neatly. The declaration is messy, but the logic is simpler.


Map<String, List<String>> map = MapUtils.lazyMap(new HashMap<String, List<String>>(),
        InstantiateFactory.instantiateFactory(ArrayList.class, null, null));

for (SampleEntity en : entityList) {

/* ===Output result===
 * {Hokkaido=[Hokkaido],
 *Tohoku region=[Aomori Prefecture,Iwate Prefecture,Miyagi Prefecture,Akita,Yamagata Prefecture,Fukushima Prefecture],
 *Kanto region=[Ibaraki Prefecture,Tochigi Prefecture,Gunma Prefecture,Saitama,Chiba,Tokyo,Kanagawa Prefecture]}

Recommended Posts

Make a list map with LazyMap
Make a family todo list with Sinatra
Make a digging maze with Ruby2D
Make a Christmas tree with swift
I want to make a list with kotlin and java!
Make a garbage reminder with line-bot-sdk-java
Java8 list conversion with Stream map
Make a typing game with ruby
Let's make a Christmas card with Processing!
[Java] Get List / Map elements with Iterator
Let's make a smart home with Ruby!
Make a login function with Rails anyway
[docker] [nginx] Make a simple ALB with nginx
Make a site template easily with Rails
List, Set, Map
Array / list / map
Create Scala Seq from Java, make Scala Seq a Java List
Let's make a search function with Rails (ransack)
Create a multi-key map with the standard library
Make System.out a Mock with Spock Test Framework
[Android] I tried to make a material list screen with ListView + Bottom Sheet
Run Scala with GraalVM & make it a native image
How to make a factory with a model with polymorphic association
[Java basics] Let's make a triangle with a for statement
[Personal application work memo] Make a calendar with simple_calendar
Make a reflection utility ②
Make a reflection utility ③
Let's make a LINE Bot with Ruby + Sinatra --Part 1
Make a reflection utility ①
[Beginner] Try to make a simple RPG game with Java ①
I want to make a function with kotlin and java!
Make a simple CRUD with SpringBoot + JPA + Thymeleaf ① ~ Hello World ~
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Let's make a simple API with EC2 + RDS + Spring boot ①
About the behavior when doing a file map with java
Make a simple CRUD with SpringBoot + JPA + Thymeleaf ⑤ ~ Template standardization ~
Manipulating List with java8StreamAPI :: reduce
Microservices with DevOps Make Changes
Draw a gradient with CAGradientLayer
[Java] Make it a constant
[Rails] Make a breadcrumb trail
Make a rhombus using Java
A story stuck with NotSerializableException
Let's make a book management web application with Spring Boot part1
I want to make a button with a line break with link_to [Note]
How to make a jar file with no dependencies in Maven
Let's make a book management web application with Spring Boot part3
Sorting a list with an int array as an element (Java) (Comparator)
Try to make a cross-platform application with JRuby (jar file generation)
Let's make a book management web application with Spring Boot part2
Make a C compiler to use with Rust x CLion with Docker
Check with Mockito whether Method was called when Map → List (when, verify)
I tried to make a group function (bulletin board) with Rails