package example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class FruitsExample {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.addAll(Arrays.asList("banana","apple","apple","mango","banana","orange"));
Map<String, Integer> map = new HashMap<String, Integer>();
Set<String> set = new HashSet<String>();
set.addAll(list);
for(String str:set)
{
int occ = Collections.frequency(list, str);
map.put(str,occ);
}
System.out.println(map);
map.forEach((k,n) -> System.out.println(k+"->"+n));
}
}
OUTPUT: {banana=2, orange=1, apple=2, mango=1}
banana->2
orange->1
apple->2
mango->1
No comments:
Post a Comment