英文:
Map<Object, AtomicInteger> to Associative array
问题
我有一个输入为int[]
的数组,其内容如下:
[5, 65, 22, 1, 58, 5, 69, 12, 1, 22, 22, 58, 12, 54, 89]
使用Map<Object, AtomicInteger>
,我将其转换为以下对象:
{1=2, 65=1, 5=2, 69=1, 22=3, 58=2, 12=1}
换句话说,我正在计算动态数组中重复的元素。
现在我需要找出最大和最小的重复次数,但我在进一步的步骤上陷入困境。
重复元素类的代码如下:
public Map<Object, AtomicInteger> countRepeatingElements(int[] inputArray) {
ConcurrentMap<Object, AtomicInteger> output =
new ConcurrentHashMap<Object, AtomicInteger>();
for (Object i : inputArray) {
output.putIfAbsent(i, new AtomicInteger(0));
output.get(i).incrementAndGet();
}
return output;
}
英文:
I have for input int[]
with the following content:
[5, 65, 22, 1, 58, 5, 69, 12, 1, 22, 22, 58, 12, 54, 89]
Using Map<Object, AtomicInteger>
, I'm converting it to the following object:
{1=2, 65=1, 5=2, 69=1, 22=3, 58=2, 12=1}
In other words, I'm calculating the repeating elements of the dynamic array.
Now I need to find out the max and min occurrence and I'm really stuck on further steps.
The code of repeating elements class is below:
public Map<Object, AtomicInteger> countRepeatingElements(int[] inputArray) {
ConcurrentMap<Object, AtomicInteger> output =
new ConcurrentHashMap<Object, AtomicInteger>();
for (Object i : inputArray) {
output.putIfAbsent(i, new AtomicInteger(0));
output.get(i).incrementAndGet();
}
return output;
}
答案1
得分: 2
如果你想找到最大和最小的出现次数,通过使用 EntrySet 遍历 Map,并比较每个键的值。
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(Map.Entry<Object, AtomicInteger> entry : output.entrySet()){
if(entry.getValue().intValue() < min){
min = entry.getValue().intValue();
}
if(entry.getValue().intValue() > max){
max = entry.getValue().intValue();
}
// entry.getValue() 可以获取数字出现的次数
// entry.getKey() 可以获取数字本身
}
英文:
If you want to find the max and the min occurrence, iterate through the Map using the EntrySet and compare the values of each key.
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(Map.Entry<Object, AtomicInteger> entry : output.entrySet()){
if(entry.getValue().intValue() < min){
min = entry.getValue().intValue();
}
if(entry.getValue().intValue() > max){
max = entry.getValue().intValue();
}
// entry.getValue() gives you number of times number occurs
// entry.getKey() gives you the number itself
}
答案2
得分: 1
Sure, here's the translated code:
int[] inputArray = {5, 65, 22, 1, 58, 5, 69, 12, 1, 22, 22, 58, 12, 54, 89};
// 1
Map<Integer, Long> grouped = Arrays.stream(inputArray)
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
// 2
LongSummaryStatistics stats = grouped.values()
.stream()
.mapToLong(Long::longValue)
.summaryStatistics();
System.out.println(stats.getMax());
System.out.println(stats.getMin());
英文:
int[] inputArray = {5, 65, 22, 1, 58, 5, 69, 12, 1, 22, 22, 58, 12, 54, 89};
// 1
Map<Integer, Long> grouped = Arrays.stream(inputArray)
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
// 2
LongSummaryStatistics stats = grouped.values()
.stream()
.mapToLong(Long::longValue)
.summaryStatistics();
System.out.println(stats.getMax());
System.out.println(stats.getMin());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论