示例#1
0
 public int get(int key) {
   if (map.containsKey(key)) {
     Node n = map.get(key);
     if (n.pre != null) {
       if (list.last == n) list.last = n.pre;
       n.pre.next = n.next;
       if (n.next != null) n.next.pre = n.pre;
       n.pre = null;
       list.first.pre = n;
       n.next = list.first;
       list.first = n;
     }
     return n.value;
   } else {
     return -1;
   }
 }
示例#2
0
 public void set(int key, int value) {
   Node n;
   if (!map.containsKey(key)) {
     n = new Node(key, value, null, null);
     if (list.size != list.count) {
       n.next = list.first;
       if (list.count != 0) {
         list.first.pre = n;
       } else {
         list.last = n;
       }
       list.first = n;
       list.count++;
     } else {
       if (list.count != 1) {
         map.remove(list.last.key);
         list.last.pre.next = null;
         list.last = list.last.pre;
         list.first.pre = n;
         n.next = list.first;
         list.first = n;
       } else {
         map.remove(list.first.key);
         list.first = n;
         list.last = n;
       }
     }
     map.put(key, n);
   } else {
     n = map.get(key);
     n.value = value;
     if (n.pre != null) {
       if (list.last == n) {
         list.last = n.pre;
       }
       n.pre.next = n.next;
       if (n.next != null) n.next.pre = n.pre;
       n.pre = null;
       n.next = list.first;
       list.first.pre = n;
       list.first = n;
     }
   }
 }