Map<Object, AtomicInteger> 转换为关联数组

huangapple go评论112阅读模式
英文:

Map<Object, AtomicInteger> to Associative array

问题

我有一个输入为int[]的数组,其内容如下:

  1. [5, 65, 22, 1, 58, 5, 69, 12, 1, 22, 22, 58, 12, 54, 89]

使用Map&lt;Object, AtomicInteger&gt;,我将其转换为以下对象:

  1. {1=2, 65=1, 5=2, 69=1, 22=3, 58=2, 12=1}

换句话说,我正在计算动态数组中重复的元素。

现在我需要找出最大和最小的重复次数,但我在进一步的步骤上陷入困境。

重复元素类的代码如下:

  1. public Map&lt;Object, AtomicInteger&gt; countRepeatingElements(int[] inputArray) {
  2. ConcurrentMap&lt;Object, AtomicInteger&gt; output =
  3. new ConcurrentHashMap&lt;Object, AtomicInteger&gt;();
  4. for (Object i : inputArray) {
  5. output.putIfAbsent(i, new AtomicInteger(0));
  6. output.get(i).incrementAndGet();
  7. }
  8. return output;
  9. }
英文:

I have for input int[] with the following content:

  1. [5, 65, 22, 1, 58, 5, 69, 12, 1, 22, 22, 58, 12, 54, 89]

Using Map&lt;Object, AtomicInteger&gt;, I'm converting it to the following object:

  1. {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:

  1. public Map&lt;Object, AtomicInteger&gt; countRepeatingElements(int[] inputArray) {
  2. ConcurrentMap&lt;Object, AtomicInteger&gt; output =
  3. new ConcurrentHashMap&lt;Object, AtomicInteger&gt;();
  4. for (Object i : inputArray) {
  5. output.putIfAbsent(i, new AtomicInteger(0));
  6. output.get(i).incrementAndGet();
  7. }
  8. return output;
  9. }

答案1

得分: 2

  1. 如果你想找到最大和最小的出现次数,通过使用 EntrySet 遍历 Map,并比较每个键的值。
  2. int min = Integer.MAX_VALUE;
  3. int max = Integer.MIN_VALUE;
  4. for(Map.Entry<Object, AtomicInteger> entry : output.entrySet()){
  5. if(entry.getValue().intValue() < min){
  6. min = entry.getValue().intValue();
  7. }
  8. if(entry.getValue().intValue() > max){
  9. max = entry.getValue().intValue();
  10. }
  11. // entry.getValue() 可以获取数字出现的次数
  12. // entry.getKey() 可以获取数字本身
  13. }
英文:

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.

  1. int min = Integer.MAX_VALUE;
  2. int max = Integer.MIN_VALUE;
  3. for(Map.Entry&lt;Object, AtomicInteger&gt; entry : output.entrySet()){
  4. if(entry.getValue().intValue() &lt; min){
  5. min = entry.getValue().intValue();
  6. }
  7. if(entry.getValue().intValue() &gt; max){
  8. max = entry.getValue().intValue();
  9. }
  10. // entry.getValue() gives you number of times number occurs
  11. // entry.getKey() gives you the number itself
  12. }

答案2

得分: 1

Sure, here's the translated code:

  1. int[] inputArray = {5, 65, 22, 1, 58, 5, 69, 12, 1, 22, 22, 58, 12, 54, 89};
  2. // 1
  3. Map<Integer, Long> grouped = Arrays.stream(inputArray)
  4. .boxed()
  5. .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
  6. // 2
  7. LongSummaryStatistics stats = grouped.values()
  8. .stream()
  9. .mapToLong(Long::longValue)
  10. .summaryStatistics();
  11. System.out.println(stats.getMax());
  12. System.out.println(stats.getMin());
英文:
  1. int[] inputArray = {5, 65, 22, 1, 58, 5, 69, 12, 1, 22, 22, 58, 12, 54, 89};
  2. // 1
  3. Map&lt;Integer, Long&gt; grouped = Arrays.stream(inputArray)
  4. .boxed()
  5. .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
  6. // 2
  7. LongSummaryStatistics stats = grouped.values()
  8. .stream()
  9. .mapToLong(Long::longValue)
  10. .summaryStatistics();
  11. System.out.println(stats.getMax());
  12. System.out.println(stats.getMin());

huangapple
  • 本文由 发表于 2020年4月10日 22:48:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/61142793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定