两种方法:
方法1
public void method1() {
int [] a={55,6,4,4,8,-8,8,8};
Map map=new HashMap();
for(int i=0;i <a.length;i++){
int count=0;
for(int j=0;j <a.length;j++){
if(Math.abs(a[i])==Math.abs(a[j])){
count++;
}
}
//map.put(count, Math.abs(a[i]));
map.put(Math.abs(a[i]),count);
}
System.out.println(map.toString());
System.out.println(map.keySet());
System.out.println(map.values());
}
方法2
public void method2(){
int [] arr={55,6,4,4,8,-8,8,8};
Set<Integer> ts = new TreeSet<Integer>();
for (int n : arr) {
ts.add(Math.abs(n));
}
System.out.println(ts.size());
}