sort a hashmap, fast

Imagine you have an hash map where key is an String and value is a integer.
Now you want to sort based on the value of each entry in this hashmap in descanding order.

Here is how I do it:

List<Map.Entry<String, Integer>> list = new Vector<Map.Entry<String, Integer>>(myHashMap.entrySet());
java.util.Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
      public int compare(Map.Entry<String, Integer> e, Map.Entry<String, Integer> e1) {
          return (e.getValue().equals(e1.getValue()) ? 0 : (e.getValue() > e1.getValue() ? 1 : -1));
}}); 
 

Now the list contains the sorted entrys from hashmap ‘THashMap myHashMap<String,Integer>’.
Iterate over it or copy it back to ‘myHashMap’ if you need to.

Hope this helps …

bash screen auto reattach

automatically search for an attached screen session at login and reattach

cb0@home:~/$cat >> ~/.bashrc << EOF
if [ \$SSH_TTY ] && [ ! \$WINDOW ]; then #comment out for local usage
  SCREENLIST=\`screen -ls | grep 'Att'\`
  if (( ! \$? )); then
    echo -e "Screen running and attached:\n \${SCREENLIST}"
  else
    screen -U -R
  fi
fi #comment out for local usage
EOF

[:edit:]

The script above only works when you connect through ssh. If you login from a real terminal $SSH_TTY and $WINDOW won’t be set.
Comment out lines marked for local usage if like screen as much as i do.