如何计算ArrayList中的重复元素?

Pio*_*nik 6 java sorting arraylist

我需要分离和计算arraylist中的多少个值是相同的,并根据出现的次数打印它们.

我有一个名为数字的arraylist:

 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
Run Code Online (Sandbox Code Playgroud)

我创建了一个方法来分隔每个值并将其保存到一个新数组.

public static ArrayList<Integer> myNumbers(int z) {

    ArrayList<Integer> digits = new ArrayList<Integer>();
    String number = String.valueOf(z);
    for (int a = 0; a < number.length(); a++) {
        int j = Character.digit(number.charAt(a), 10);
        digits.add(j);
    }
    return digits;

}
Run Code Online (Sandbox Code Playgroud)

在此之后,我有一个名为数字的新数组.我在这个数组上使用sort

Collections.sort(numbers);
Run Code Online (Sandbox Code Playgroud)

我的ArrayList看起来像这样:

[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9]
Run Code Online (Sandbox Code Playgroud)

它有:

2 times 0; 
9 times 1;
4 times 2;
6 times 3;
5 times 4;
6 times 5;
5 times 6;
5 times 7;
5 times 8;
3 times 9;
Run Code Online (Sandbox Code Playgroud)

我需要打印出一串数字,取决于它们的数量,所以它看起来像这样:1354678290

pru*_*dam 16

List<String> list = new ArrayList<String>();
    list.add("a");
    list.add("b");
    list.add("c");
    list.add("a");
    list.add("a");
    list.add("a");

int countA=Collections.frequency(list, "a");
int countB=Collections.frequency(list, "b");
int countC=Collections.frequency(list, "c");
Run Code Online (Sandbox Code Playgroud)


Sou*_*tta 8

问题是计算数组中有多少个三三两两。在 Java 7 中,解决方案是:

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class howMany1 {
public static void main(String[] args) {

    List<Integer> list = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765);

    Map<Integer ,Integer> map = new HashMap<>();

      for(  Integer r  : list) {
          if(  map.containsKey(r)   ) {
                 map.put(r, map.get(r) + 1);
          }//if
          else {
              map.put(r, 1);
          }
      }//for

      //iterate

      Set< Map.Entry<Integer ,Integer> > entrySet = map.entrySet();
      for(    Map.Entry<Integer ,Integer>  entry : entrySet     ) {
          System.out.printf(   "%s : %d %n "    , entry.getKey(),entry.getValue()  );
      }//for

}}
Run Code Online (Sandbox Code Playgroud)

在 Java 8 中,问题的解决方案是:

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class howMany2 {
public static void main(String[] args) {

    List<Integer> list = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765);
     // we can also use Function.identity() instead of c->c
    Map<Integer ,Long > map = list.stream()
            .collect(  Collectors.groupingBy(c ->c , Collectors.counting())         ) ;


    map.forEach(   (k , v ) -> System.out.println( k + " : "+ v )                    );

}}
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用 Collections.frequency。解决办法是:

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Duplicates1 {
public static void main(String[] args) {

    List<Integer> list = Arrays.asList(1, 1, 2, 3, 5, 8, 13,13, 21, 34, 55, 89, 144, 233);

    System.out.println("Count all with frequency");
    Set<Integer> set = new HashSet<Integer>(list);
    for (Integer r : set) {
        System.out.println(r + ": " + Collections.frequency(list, r));
    }

}}
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用 method => Arrays.stream(array).boxed().collect(Collectors.toList()) 将 int 数组更改为整数列表,然后使用 for 循环获取整数。

public class t7 {
    public static void main(String[] args) {
        int[] a = { 1, 1, 2, 3, 5, 8, 13, 13 };
        List<Integer> list = Arrays.stream(a).boxed().collect(Collectors.toList());

        for (Integer ch : list) {
            System.out.println(ch + " :  " + Collections.frequency(list, ch));
        }

    }// main
}
Run Code Online (Sandbox Code Playgroud)


Pra*_*san 6

使用Collections.frequency方法计算重复项

  • TIL存在.我准备写一个很长的答案哈哈.好一个 (2认同)
  • 它计算特定项目的出现次数。它不会创建频率表,也不会以任何特定顺序对其进行排序。这意味着在此和正确的答案之间还有很长的路要走。 (2认同)

小智 5

好吧,为此,您可以尝试使用地图

Map<Integer, Integer> countMap = new HashMap<>();

  for (Integer item: yourArrayList) {

      if (countMap.containsKey(item))
          countMap.put(item, countMap.get(item) + 1);
      else
          countMap.put(item, 1);
  }
Run Code Online (Sandbox Code Playgroud)

在forEach循环结束后,您将获得一个填充地图,其中包含您要计数的物品


Flo*_*vic 5

例如,通过使用 Stream API。

package tests;

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Duplicates {

    @Test
    public void duplicates() throws Exception {
        List<Integer> items = Arrays.asList(1, 1, 2, 2, 2, 2);

        Map<Integer, Long> result = items.stream()
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        Assert.assertEquals(Long.valueOf(2), result.get(1));
        Assert.assertEquals(Long.valueOf(4), result.get(2));
    }
}
Run Code Online (Sandbox Code Playgroud)