判断List列表中重复数据的个数

判断List列表中重复数据的个数.很简单一个逻辑,写下留做笔记

public class ListRepeat {
	public static void main(String[] args) {
		String[] stringBuf = {"a","b","c","a","e","a","a","b","c","e","b"};
		
		List<String> list = new ArrayList<String>();
		Map<String, Integer> map = new HashMap<String,Integer>();
		for (String value : stringBuf) {
			list.add(value);
		}
		
		/*
		 * Collections.frequency(Collection<?> c, Object o) 
		 * 返回指定 collection 中等于指定对象的元素数。
		 * 使用此方法时,是将list中的每一个数据都进行遍历比对一遍
		 * 
		 * */
		
		for (String str : list) {
			System.out.println(str + "出现次数 : " + Collections.frequency(list, str));
			Integer integer = map.get(str);
			map.put(str, (integer == null) ? 1 : ++integer);
		}
		/*Map集合的三种遍历*/
		for (Entry<String, Integer> set : map.entrySet()) {
			System.out.println("MapKEY: " + set.getKey() + "  MapValue: " + set.getValue());
		}
		
		for (String set :map.keySet()) {
			System.out.println("MapKEY: " + set + "  MapValue: " + map.get(set));
		}
		
		for (Integer value : map.values()) {
			System.out.println("MapValue: " + value);
		}
		
	}
}

版权声明:本文为qq_39236265原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。