Associates the specified value with the specified key in this map. @param key key with which the specified value is to be associated. @param value value to be associated with the specified key. @return null @exception ClassCastException if the class of the specified key
(Object key, Object value)
| 1423 | * existing value |
| 1424 | */ |
| 1425 | public Object put(Object key, Object value) |
| 1426 | throws ClassCastException, NullPointerException, |
| 1427 | IllegalArgumentException |
| 1428 | { |
| 1429 | checkKeyAndValue(key, value); |
| 1430 | Node node = _root[ _KEY ]; |
| 1431 | |
| 1432 | if (node == null) |
| 1433 | { |
| 1434 | Node root = new Node(( Comparable ) key, ( Comparable ) value); |
| 1435 | |
| 1436 | _root[ _KEY ] = root; |
| 1437 | _root[ _VALUE ] = root; |
| 1438 | grow(); |
| 1439 | } |
| 1440 | else |
| 1441 | { |
| 1442 | while (true) |
| 1443 | { |
| 1444 | int cmp = compare(( Comparable ) key, node.getData(_KEY)); |
| 1445 | |
| 1446 | if (cmp == 0) |
| 1447 | { |
| 1448 | throw new IllegalArgumentException( |
| 1449 | "Cannot store a duplicate key (\"" + key |
| 1450 | + "\") in this Map"); |
| 1451 | } |
| 1452 | else if (cmp < 0) |
| 1453 | { |
| 1454 | if (node.getLeft(_KEY) != null) |
| 1455 | { |
| 1456 | node = node.getLeft(_KEY); |
| 1457 | } |
| 1458 | else |
| 1459 | { |
| 1460 | Node newNode = new Node(( Comparable ) key, |
| 1461 | ( Comparable ) value); |
| 1462 | |
| 1463 | insertValue(newNode); |
| 1464 | node.setLeft(newNode, _KEY); |
| 1465 | newNode.setParent(node, _KEY); |
| 1466 | doRedBlackInsert(newNode, _KEY); |
| 1467 | grow(); |
| 1468 | break; |
| 1469 | } |
| 1470 | } |
| 1471 | else |
| 1472 | { // cmp > 0 |
| 1473 | if (node.getRight(_KEY) != null) |
| 1474 | { |
| 1475 | node = node.getRight(_KEY); |
| 1476 | } |
| 1477 | else |
| 1478 | { |
| 1479 | Node newNode = new Node(( Comparable ) key, |
| 1480 | ( Comparable ) value); |
| 1481 | |
| 1482 | insertValue(newNode); |