找出重复元素代码如下,先排序(这里省略)关于迭代器 参考链接
public class chongfu {
public static Map<Integer, Integer> buchongfu(Integer[] a) {
Map<Integer,Integer> map=new HashMap<Integer,Integer>();
for (int i = 0; i < a.length; i++) {
if (null!=map.get(a[i])) {
map.put(a[i], map.get(a[i])+1);
}else {
map.put(a[i], 1);
}
}
return map;
}
public static void main(String[] args) {
Integer [] a={1,1,1,1,2,2,2,2,4,4,4,6};
Iterator it=buchongfu(a).entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry=(Map.Entry) it.next();
Integer key=(Integer) entry.getKey();
Integer value=(Integer) entry.getValue();
System.out.println("key:"+key+"value"+value);
}
}
}
)