I learned stream (I want to convert List to Map <Integer, List>)

Convert List format data to Map <yyy, List > format

I used to use for honestly so far, so make a note.

List format data obtained from DB I used it when I wanted to filter it and convert it to Map format (which was easier when displaying it with jsp).

List<myClass> resList = myClassDao.query(insMap);

//filtering
List<myClass> dataList = resList.stream()
        .filter(c -> c.getHogeId() != null)
        .collect(Collectors.toList());

//Convert to Map
Map<Integer, List<myClass>> res = dataList.stream().collect(
        Collectors.groupingBy(myClass::getHogeId)
);

With this, the data like this

[
    {
        id:1,
        hogeId:1,
        data:"a"
    },
    {
        id:2,
        hogeId:1,
        data:"b"
    },
    {
        id:3,
        hogeId:2,
        data:"c"
    },
    {
        id:4,
        hogeId:null,
        data:"d"
    }
]

It will be like this. Perhaps.

{
    1:[
        {
            id:1,
            hogeId:1,
            data:"a"
        },
        {
            id:2,
            hogeId:1,
            data:"b"
        }
    ],
    2:[
        {
            id:3,
            hogeId:2,
            data:"c"
        }
    ]
}

Please let me know if there is a better way!

Recommended Posts

I learned stream (I want to convert List to Map <Integer, List>)
I want to convert characters ...
I want to convert InputStream to String
[Java] Convert 1-to-N List to Map
Sample code to convert List to List <String> in Java Stream
I want to get along with Map [Java beginner]
[Java] I want to convert a byte array to a hexadecimal number
I want to make a list with kotlin and java!
I want to recursively search the class list under the package
For Java beginners: List, Map, Iterator / Array ... How to convert?
Swift: I want to chain arrays
Convert Map <K, V1> to Map <K, V2> (Convert Map Value)
I want to convert an array to Active Record Relation with Rails
[Ruby] I want to put an array in a variable. I want to convert to an array
I want to docker-compose up Next.js!
[Small story] Convert Stream to Iterable
Java8 list conversion with Stream map
Glassfish tuning list that I want to keep for the time being
I want to perform Group By processing with Stream (group-by-count, group-by-sum, group-by-max)
I want to develop a web application!
I want to eliminate duplicate error messages
I want to display background-ground-image on heroku.
I want to use DBViewer with Eclipse 2018-12! !!
I want to RSpec even at Jest!
I want to write a unit test!
I want to install PHP 7.2 on Ubuntu 20.04.
I tried to summarize the Stream API
I want to stop Java updates altogether
I want to use @Autowired in Servlet
I want to target static fields to @Autowired
I want to do team development remotely
I want to test Action Cable with RSpec test
Run R from Java I want to run rJava
[Swift] I want to draw grid lines (squares)
I want to send an email in Java.
I want to graduate from npm install properly [2020]
I want to use arrow notation in Ruby
[Ruby] I want to do a method jump!
I want to use java8 forEach with index
I want to var_dump the contents of the intent
I want to pass APP_HOME to logback in Gradle
I want to simply write a repeating string
I want to design a structured exception handling
rsync4j --I want to touch rsync in Java.
Convert Json to List <T> as it is
I want to play with Firestore from Rails
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
I want to write quickly from java to sqlite
I want to truncate after the decimal point
I want to reduce simple mistakes. To command yourself.
[Java] How to operate List using Stream API
I want to perform aggregation processing with spring-batch
[Rails] I want to load CSS with webpacker
I want to delete files managed by Git
I want to get the value in Ruby
[MyBatis] I want to map a query query to a table that has a one-to-many relationship to a nested bean