Skip to content
Advertisement

Java – Collectors Group By and Expand Map by number of records

I have the following java list of UserRecords objects that are grouped by user_name.

List<UserRecords> userRecordsList = new ArrayList<UserRecords>();

    // load mock data into the data structure
    for(int i = 0; i < 20; i ++) {
            UserRecords entry = new UserRecords();
            entry.id = i;
            entry.record_type = "audit";
            entry.info = "some information.";

            if(i < 5) {
                entry.user_name = "John Doe";
            } else {
                entry.user_name = "Jane Doe";
            }

        userRecordsList.add(entry);
    }

    // group by user_name
    Map<String, List<UserRecords>> userGroup =
            userRecordsList.stream().collect(Collectors.groupingBy(UserRecords::getUser_name));

I want to be able to break down this list into small batches of 5 records if a user has more than 5 records.

Here’s the result of my user group:

{John Doe=[
UserRecords{id=0, user_name='John Doe', record_type='audit', info='some information.'}, 
UserRecords{id=1, user_name='John Doe', record_type='audit', info='some information.'}, 
UserRecords{id=2, user_name='John Doe', record_type='audit', info='some information.'}, 
UserRecords{id=3, user_name='John Doe', record_type='audit', info='some information.'}, 
UserRecords{id=4, user_name='John Doe', record_type='audit', info='some information.'}], 

Jane Doe=[
UserRecords{id=5, user_name='Jane Doe', record_type='audit', info='some information.'}, 
UserRecords{id=6, user_name='Jane Doe', record_type='audit', info='some information.'}, 
UserRecords{id=7, user_name='Jane Doe', record_type='audit', info='some information.'}, 
UserRecords{id=8, user_name='Jane Doe', record_type='audit', info='some information.'}, 
UserRecords{id=9, user_name='Jane Doe', record_type='audit', info='some information.'}, 
UserRecords{id=10, user_name='Jane Doe', record_type='audit', info='some information.'}, 
UserRecords{id=11, user_name='Jane Doe', record_type='audit', info='some information.'}, 
UserRecords{id=12, user_name='Jane Doe', record_type='audit', info='some information.'}, 
UserRecords{id=13, user_name='Jane Doe', record_type='audit', info='some information.'}, 
UserRecords{id=14, user_name='Jane Doe', record_type='audit', info='some information.'}, 
UserRecords{id=15, user_name='Jane Doe', record_type='audit', info='some information.'}, 
UserRecords{id=16, user_name='Jane Doe', record_type='audit', info='some information.'}, 
UserRecords{id=17, user_name='Jane Doe', record_type='audit', info='some information.'}, 
UserRecords{id=18, user_name='Jane Doe', record_type='audit', info='some information.'}, 
UserRecords{id=19, user_name='Jane Doe', record_type='audit', info='some information.'}]}

Since Jane Doe has more than 5 records, I would like her results to be split as follows:

  Jane Doe=[
     UserRecords{id=5, user_name='Jane Doe', record_type='audit', info='some information.'},
     UserRecords{id=6, user_name='Jane Doe', record_type='audit', info='some information.'},
     UserRecords{id=7, user_name='Jane Doe', record_type='audit', info='some information.'},
     UserRecords{id=8, user_name='Jane Doe', record_type='audit', info='some information.'},
     UserRecords{id=9, user_name='Jane Doe', record_type='audit', info='some information.'},
     UserRecords{id=10, user_name='Jane Doe', record_type='audit', info='some information.'}],

  Jane Doe1=[
        UserRecords{id=11, user_name='Jane Doe', record_type='audit', info='some information.'},
       UserRecords{id=12, user_name='Jane Doe', record_type='audit', info='some information.'},
       UserRecords{id=13, user_name='Jane Doe', record_type='audit', info='some information.'},
       UserRecords{id=14, user_name='Jane Doe', record_type='audit', info='some information.'},
       UserRecords{id=15, user_name='Jane Doe', record_type='audit', info='some information.'}],

   Jane Doe2=[
        UserRecords{id=16, user_name='Jane Doe', record_type='audit', info='some information.'},
       UserRecords{id=17, user_name='Jane Doe', record_type='audit', info='some information.'},
       UserRecords{id=18, user_name='Jane Doe', record_type='audit', info='some information.'},
       UserRecords{id=19, user_name='Jane Doe', record_type='audit', info='some information.'}]

How can I approach this?

Advertisement

Answer

I would recommend reading about Guava.

Try using this method, which divides a list for sublists of maximum chosen size.

List<List<UserGroup>> janeDoeUserGroups = Lists.partition(userGroup.get(”Jane Doe”), 5);

Every element of the janeDoeUserGroups is a list containing maximum 5 elements of UserGroup, only the last one might contain less in case the total elements number % 5 != 0.

Also class fields should be written in camel case style, like:

String recordType;

Instead of

String record_type;

Hope it helps.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement