Java Programs
How to find Duplcate values in ArrayList with countimport java.util.ArrayList; import java.util.HashMap; public class CollectionsEx2 { public static void main(String[] args) { ArrayListlist = new ArrayList(); list.add("Sagio Mane"); list.add("Karius"); list.add("Mo Salah"); list.add("Firmino"); list.add("Lovren"); list.add("Steven Gerrard"); list.add("Karius"); list.add("Mo Salah"); list.add("Mo Salah"); HashMap hm = new HashMap (); for(String i: list) { if (hm.containsKey(i)) hm.put(i, hm.get(i)+1); else hm.put(i, 1); } System.out.println("HashMap:" + hm); } } Output: HashMap:{Firmino=1, Sagio Mane=1, Lovren=1, Karius=2, Steven Gerrard=1, Mo Salah=3}
No comments:
Post a Comment