然而,这一切很容易被人们舒适的办公环境所掩盖,因为办公室里有冷暖空调吹着,饮水机里有水喝,食堂有饭吃,电脑还能上网,办公楼打扫得一尘不染....
舒适的环境使人们产生错觉,忘记背后的现实。
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Set set = new TreeSet();
set.add(new Pair("me", "1000"));
set.add(new Pair("and", "4000"));
set.add(new Pair("you", "3000"));
set.add(new Pair("food", "10000"));
set.add(new Pair("hungry", "5000"));
set.add(new Pair("later", "6000"));
set.add(new Pair("myself", "1000"));
for (Iterator i = set.iterator(); i.hasNext();)
System.out.println(i.next());
}
}
class Pair implements Comparable {
private final String name;
private final int number;
public Pair(String name, int number) {
this.name = name;
this.number = number;
}
public Pair(String name, String number) throws NumberFormatException {
this.name = name;
this.number = Integer.parseInt(number);
}
public int compareTo(Object o) {
if (o instanceof Pair) {
int cmp = Double.compare(number, ((Pair) o).number);
if (cmp != 0) {
return cmp;
}
return name.compareTo(((Pair) o).name);
}
throw new ClassCastException("Cannot compare Pair with "
+ o.getClass().getName());
}
public String toString() {
return name + ' ' + number;
}
}
public static Map sortByValue(Map map) {
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
Map result = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}
public static Map sortByValue(Map map, final boolean reverse) {
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
if (reverse) {
return -((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
Map result = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}
Map map = new HashMap();
map.put("a", 4);
map.put("b", 1);
map.put("c", 3);
map.put("d", 2);
Map sorted = sortByValue(map);
System.out.println(sorted);
Map map = new HashMap();
map.put("a", 4);
map.put("b", 1);
map.put("c", 3);
map.put("d", 2);
Set<Map.Entry<String, Integer>> treeSet = new TreeSet<Map.Entry<String, Integer>>(
new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
Integer d1 = o1.getValue();
Integer d2 = o2.getValue();
int r = d2.compareTo(d1);
if (r != 0)
return r;
else
return o2.getKey().compareTo(o1.getKey());
}
});
treeSet.addAll(map.entrySet());
System.out.println(treeSet);
// output : [a=4, c=3, d=2, b=1]
Integer Division | col / row |
Java Remainder | col % row == col - row * ( col / row ) |
Mathematical Modulus | col mod row == (col % row + row) % row |
Signs | Division / | Remainder % | Modulus |
---|---|---|---|
+ + | 7 / 4 = 1 | 7 % 4 = 3 | 7 mod 4 = 3 |
- + | -7 / 4 = -1 | -7 % 4 = -3 | -7 mod 4 = 1 |
+ - | 7 / -4 = -1 | 7 % -4 = 3 | 7 mod -4 = -1 |
- - | -7 / -4 = +1 | -7 % -4 = -3 | -7 mod -4 = -3 |