Java 如何计算ArrayList中的重复元素?
我需要分离并计算arraylist中有多少值是相同的,并根据出现的次数打印它们 我有一个名为digits的arraylist:Java 如何计算ArrayList中的重复元素?,java,sorting,arraylist,Java,Sorting,Arraylist,我需要分离并计算arraylist中有多少值是相同的,并根据出现的次数打印它们 我有一个名为digits的arraylist: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765] 我创建了一个方法,用于分离每个值并将其保存到新数组中 public static ArrayList<Integer> myNumbers(int z) { ArrayL
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
我创建了一个方法,用于分离每个值并将其保存到新数组中
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;
}
我的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]
它有:
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;
我需要根据数字的多少打印出数字串
所以它应该是这样的:
1354678290使用
集合。频率
方法对重复项进行计数那么,您可以尝试使用Map
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);
}
Map countMap=newhashmap();
for(整数项:yourArrayList){
if(countMap.containsKey(项目))
countMap.put(item,countMap.get(item)+1);
其他的
countMap.put(第1项);
}
在forEach循环结束后,您将有一个填充的映射,其中包含您的项目,例如使用streamapi进行计数
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));
}
}
包测试;
导入org.junit.Assert;
导入org.junit.Test;
导入java.util.array;
导入java.util.List;
导入java.util.Map;
导入java.util.function.function;
导入java.util.stream.collector;
公共类副本{
@试验
public void duplicates()引发异常{
列表项=数组.asList(1,1,2,2,2);
映射结果=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));
}
}
您可以通过添加列表中的所有元素并将其存储在哈希集中来计算列表中重复元素的数量,完成后,您只需知道哈希集和列表大小的差异
ArrayList<String> al = new ArrayList<String>();
al.add("Santosh");
al.add("Saket");
al.add("Saket");
al.add("Shyam");
al.add("Santosh");
al.add("Shyam");
al.add("Santosh");
al.add("Santosh");
HashSet<String> hs = new HashSet<String>();
hs.addAll(al);
int totalDuplicates =al.size() - hs.size();
System.out.println(totalDuplicates);
ArrayList al=new ArrayList();
al.添加(“桑托什”);
新增(“Saket”);
新增(“Saket”);
al.添加(“Shyam”);
al.添加(“桑托什”);
al.添加(“Shyam”);
al.添加(“桑托什”);
al.添加(“桑托什”);
HashSet hs=新的HashSet();
hs.addAll(al);
int totalDuplicates=al.size()-hs.size();
系统输出打印项次(共份);
让我知道这是否需要更多的澄清问题是数一数数组中有多少个一、二、三。 在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
}}
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));
}
}}
导入java.util.array;
导入java.util.HashMap;
导入java.util.HashSet;
导入java.util.List;
导入java.util.Map;
导入java.util.Set;
公开课人数1{
公共静态void main(字符串[]args){
列表=数组.asList(1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765);
Map Map=newhashmap();
for(整数r:list){
if(地图容器(r)){
map.put(r,map.get(r)+1);
}//如果
否则{
图.put(r,1);
}
}//为了
//迭代
SetentrySet=Map.entrySet();
for(Map.Entry:entrySet){
System.out.printf(“%s:%d%n”,entry.getKey(),entry.getValue());
}//为了
}}
在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 ) );
}}
导入java.util.array;
导入java.util.HashMap;
导入java.util.HashSet;
导入java.util.List;
导入java.util.Map;
导入java.util.Set;
导入java.util.stream.collector;
公开课人数2{
公共静态void main(字符串[]args){
列表=数组.asList(1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765);
//我们也可以使用Function.identity()代替c->c
Map=list.stream()
.collect(Collectors.groupingBy(c->c,Collectors.counting());
map.forEach((k,v)->System.out.println(k+“:”+v));
}}
另一种方法是使用Collections.frequency。解决办法是:
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
}}
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));
}
}}
导入java.util.array;
导入java.util.Collections;
导入java.util.HashSet;
导入java.util.List;
导入java.util.Set;
公共类副本1{
公共静态void main(字符串[]args){
列表=数组.asList(1,1,2,3,5,8,13,13,21,34,55,89,144,233);
System.out.println(“以频率计数所有数据”);
Set Set=新哈希集(列表);
for(整数r:set){
System.out.println(r+“:”+Collections.frequency(list,r));
}
}}
另一种方法是使用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
}
公共类t7{
公共静态void main(字符串[]args){
int[]a={1,1,2,3,5,8,13,13};
List List=Arrays.stream(a.boxed().collect(Collectors.toList());
for(整数ch:list){
System.out.println(ch+“:”+Collections.frequency(list,ch));
}
}//主要
}
Java8,解决方案:1。当键为数组的值且值为计数器时创建映射。
2.检查Map是否包含密钥增加计数器或添加新集合。
私有静态void CalculatedBlicateValues(int[]数组){
//键是数组的值,值是计数器
Map Map=newhashmap();
for(整型元素:数组){
if(映射容器(元素)){
map.put(元素,map.get(元素)+1);//如果包含,则增加计数器
}否则
地图放置(元素,1);
}
图.forEach((k,v)->{
如果(v>1)
System.out.println(“元素”+k+“复制”+v+“次数”);
});
}
使用以下函数计算重复元素:
public void countDuplicate(){
try {
Set<String> set = new HashSet<>(original_array);
ArrayList<String> temp_array = new ArrayList<>();
temp_array.addAll(set);
for (int i = 0 ; i < temp_array.size() ; i++){
Log.e(temp_array.get(i),"=>"+Collections.frequency(original_array,temp_array.get(i)));
}
}catch (Exception e){
e.printStackTrace();
}
}
public void countreplicate(){
试一试{
Set Set=新哈希集(原始_数组);
ArrayList临时数组=新的ArrayList();
临时数组addAll(集合);
对于(int i=0;i”+Collections.frequency(原始数组,temp_数组.get(i));
}
}捕获(例外e){
e、 普林斯特
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");
Map<Integer, Integer> duplicatedCount = new LinkedHashMap<>();
list.forEach(a -> duplicatedCount.put(a, duplicatedCount.getOrDefault(a, 0) +1));
duplicatedCount.forEach((k,v) -> System.out.println(v+" times "+k));
List<Integer> input = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584,
4181, 6765);
final Map<Integer, Integer> result = new HashMap<>();
input.forEach(in -> result.merge(in, 1, Integer::sum));
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class A_List_Find_Duplicate_Count {
public static void main(String[] args) {
List<String> list = Arrays.asList(
"Chennai","Bangalore","Pune","Hyderabad",
"Chennai","Pune","Mysore","Delhi","Hyderabad",
"Pune"
);
String elementToFound = "Chennai";
//JAVA 8
long count = list.stream().filter(i->elementToFound.equals(i)).count();
System.out.println("elementToFound : "+count);
//using Collections
int frequency = Collections.frequency(list, elementToFound);
System.out.println("frequency : "+frequency);
//using Map
HashMap<String, Integer> map = new HashMap<>();
for(String s : list)
{
map.put(s, map.get(s)!=null ? map.get(s)+1 : 1);
}
System.out.println("map : "+map.get(elementToFound));
//JAVA 8 using groupingBy
Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println("collect groupingBy : "+collect.get(elementToFound));
}
}