Skip to content
Advertisement

Finding the duplicate values and the number of occurrences of the same in Java Map

I am using Java. I have a Map as shown below :

JavaScript

I am inserting Map objects with some key value pairs into the above List<Map<String, String>> listMap.

JavaScript

Now, I want to iterate

JavaScript

and then find if there are any duplicate/same values for the key jobDescription in any of the map, then check for the value of interviewType key’s value and see the number of occurrences of the value.

In the above example, the values for the key jobDescription is same in all the Map objects(i.e.Java Developer-SpringBoot). Then verify the values for the key interviewType and see the number of occurrences of each value(In the above case L2 repeated twice and L1 once). Finally I need to construct one more Map that contains my observations.

For example(This data is depicted for illustration purpose, but this should actually go into a new Map:

JavaScript

Can anyone help me on this?

The code that I am trying is given below:

JavaScript

Advertisement

Answer

It looks strange to use map to store the data, as:

  1. All values are limited to the same type(String).
  2. There is no limitation on the keys.

Modelling a class Job is a more proper way. Then follow @Joe comment suggestion, Group by multiple field names in java 8

Below program will output

Java Developer-SpringBoot L1:1, L2:2

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