我有以下类型的地图:
[
{
"RequestId": 453,
"RequestGuid": "a81b5878-6214-44b4-8888-aab46ce48adf",
"ShowRecordingInfo": true,
"IsOfficeDetailsRequest": false,
"UserId": 8515,
"IsSaved": true,
"Name": "saving test check no link to ODP",
"DocumentTypeDetails": {
"PageRec": "AL005",
"State": "AL",
"County": "Autauga County",
"CityTown": null,
"Zip": null,
"ShowRecordingInfo": "true",
"Deed": {
"Checked": "True",
"Pages": "1",
"ConsiderationAmount": "150000"
},
"Amend": {
"Checked": "False",
"Pages": "1",
"ConsiderationAmount": "150000"
},
"MortgageDeed": {
"Checked": "False",
"Pages": null,
"NewDebtAmount": null
},
"MortgageMod": {
"Checked": "False",
"Pages": null,
"NewDebtAmount": null
},
"MortgageRefi": {
"Checked": "False",
"Pages": null,
"NewDebtAmount": null,
"OriginalDebt": null,
"UnpaidDebt": null
},
"Assignment": {
"Checked": "False",
"Pages": null,
"Assignments": null
},
"ReleaseSatisfaction": {
"Checked": "False",
"Pages": null,
"ReleasesSatisfactions": null
},
"poa":{
"Checked":"False",
"Pages":null
},
"Questions": {
"Question": {
"Number": "Q4",
"Category": "Deed",
"Type": "bool",
"QuestionText": "Are the deed and mortgage being recorded at the same time?",
"Answer": "1"
}
}
},
"RequestXml": {
"App": "B",
"Authentication": {
"UserID": "",
"Password": ""
},
"TransactionDate": "1/27/2016",
"TransactionCode": "150",
"Property": {
"Page": "AL005",
"City": null,
"County": null,
"State": "AL",
"EstimatedValue": "150000",
"MortgageAmount": null,
"OriginalDebtAmount": null,
"UnpaidPrincipalBalance": null,
"OriginalMortgageDate": null,
"StateQuestions": {
"Q1": "0",
"Q2": "0",
"Q3": "0",
"Q4": "1",
"Q5": "0",
"Q6": "0",
"Q7": "0",
"Q8": "0",
"Q9": "0",
"Q10": "0",
"Q11": "0",
"Q12": "0",
"Q13": "0",
"Q14": "0",
"Q15": "0",
"Q16": "0",
"Q17": "0",
"Q18": "0",
"Q19": "0",
"Q20": "0",
"Q21": "0",
"V1": "0",
"V2": "0",
"V3": "0",
"V4": "0"
}
},
"NumberOfPages": {
"Mortgage": null,
"Deed": "1"
},
"Assignment": {
"Pages": null,
"NumberOfAssignments": null
},
"Release": {
"Pages": null,
"NumberOfReleases": null
},
"DataSource": "P"
},
}
}
]
键从1 - n开始。整个Map中的subMap具有以下类型:
private HashMap<Integer, HashMap<String, Object>> entireMap;
整个地图的每个键都包含这个子地图(还有更多):
HashMap<String, Object> subMap = new HashMap<String, Object>();
所以我最后有这样的事情:
subMap.put("user_name", usid[1]);
现在我想计算整个地图中user_name的最大出现次数,在这种情况下它将是3(bela发生三次)。
我该怎么做?
答案 0 :(得分:5)
以下是一个实现示例。
注意:请勿在生产代码中使用此类地图初始化!
HashMap<Integer, HashMap<String, Object>> entireMap = new HashMap<>();
entireMap.put(1, new HashMap<String, Object>() {{
put("user_name", "Arthur");
put("other_key1", "val");
put("other_key2", "val");
}});
entireMap.put(2, new HashMap<String, Object>() {{
put("user_name", "Bela");
put("other_key2", "val");
}});
entireMap.put(3, new HashMap<String, Object>() {{
put("user_name", "Ceasar");
}});
entireMap.put(4, new HashMap<String, Object>() {{
put("user_name", "Ceasar");
}});
entireMap.put(5, new HashMap<String, Object>() {{
put("user_name", "Bela");
}});
entireMap.put(6, new HashMap<String, Object>() {{
put("user_name", "Bela");
}});
Map<Object, Long> result = entireMap
.values()
.stream()
.map(map -> map.get("user_name"))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(result);
Long max = Collections.max(result.values());
System.out.println(max);
输出:
{Ceasar=2, Arthur=1, Bela=3}
3
答案 1 :(得分:0)
如果您不想使用java 8功能,只需通过简单的迭代遍历地图即可。
Map<String, Integer> resultMap = new HashMap<String, Integer>();
for (int key : entireMap.keySet()) {
String userName = (String) entireMap.get(key).get("user_name");
if (resultMap.containsKey(userName)) {
resultMap.put(userName, resultMap.get(userName) + 1);
} else {
resultMap.put(userName, 1);
}
}
System.out.println(resultMap);
输出
{Arthur = 1,Ceasar = 2,Bela = 3}
答案 2 :(得分:0)
以下是使用Java 8功能的一种方法,但没有流:
counts
在Entry<String, Long> max = Collections.max(
counts.entrySet(),
Map.Entry.comparingByValue());
地图中,您将拥有每个用户的计数。然后,您可以找到具有最大值的条目:
SomeResponse
答案 3 :(得分:0)
在Java 7或更低版本中,您可以使用Guava MultiSet:
List<Object> names = new ArrayList<>();
for (HashMap<String, Object> outerMap : entireMap.values()) {
names.add(outerMap.get("user_name"));
}
HashMultiset<Object> objects = HashMultiset.create(names);
objects.forEachEntry((name, count) -> System.out.println(name + " -> " + count));
结果:
Ceasar -> 2
Arthur -> 1
Bela -> 3