-
Notifications
You must be signed in to change notification settings - Fork 1
/
Zoho_NumbersWeights.java
44 lines (39 loc) · 1.05 KB
/
Zoho_NumbersWeights.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class Test2 {
public static void main(String[] args) {
int[] nums={10,36,54,89,12};
Map<Integer, Integer> map=new TreeMap<>();
for(int a:nums){
int wt=0;
double d=Math.sqrt((double)a);
if((a&1)==0)
wt=3;
if(a%4==0 && a%6==0)
wt=wt+4;
if(((int)d)*((int)d)==a)
wt=wt+5;
map.put(a, wt);
}
ValueComparator bvc = new ValueComparator(map);
TreeMap<Integer, Integer> sorted_map = new TreeMap<Integer, Integer>(bvc);
System.out.println("unsorted map: " + map);
sorted_map.putAll(map);
System.out.println("results: " + sorted_map);
System.out.println("***********");
System.out.println(map.entrySet());
System.out.println(map.values());
}
}
class ValueComparator implements Comparator<Integer> {
Map<Integer, Integer> base;
public ValueComparator(Map<Integer, Integer> base) {
this.base = base;
}
@Override
public int compare(Integer o1, Integer o2) {
if (base.get(o1) <= base.get(o2)) {
return -1;
} else {
return 1;
}
}
}