I need to separate and count how many values in arraylist are the same and print them according to the number of occurrences.
I\'ve got an arraylist called digits :<
The question is to count how many ones twos and threes are there in an array. In Java 7 solution is:
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
}}
In Java 8, the solution to the problem is :
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 ) );
}}
One another method is to use Collections.frequency. The solution is:
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));
}
}}
Another method is to change the int array to Integer List using method => Arrays.stream(array).boxed().collect(Collectors.toList()) and then get the integer using for loop.
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
}
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");
Use below function for count duplicate elements :
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();
}
}
Java 8, the solution: 1. Create Map when the Key is the Value of Array and Value is counter.
2. Check if Map contains the Key increase counter or add a new set.
private static void calculateDublicateValues(int[] array) {
//key is value of array, value is counter
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Integer element : array) {
if (map.containsKey(element)) {
map.put(element, map.get(element) + 1); // increase counter if contains
} else
map.put(element, 1);
}
map.forEach((k, v) -> {
if (v > 1)
System.out.println("The element " + k + " duplicated " + v + " times");
});
}