Skip to content
Advertisement

Create multiple Lists from one (Group List’s Objects by a specific field)

Given that I have the array of :

JavaScript

gives me :

JavaScript


I want to create a new array which is grouped by the 3rd parameter of surname object. So that my array becomes

JavaScript

So that when we do .get(1) we would get object of kasis and samika.

Need the help in respective to java 8. I heard that we can use the Map ,but can anyone give the small code sample or any other implementation guide.

Advertisement

Answer

A Map tracks key-value pairs.

  • Your key is the surname string.
  • Your value is a list of the CustEnt objects carrying that surname.
JavaScript

Modern syntax with streams and lambdas makes for brief code to place your objects in a map.

Something like:

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