Code example for LinkedHashMap

Methods: clone, get, keySet, put

0
 
public class OldLinkedHashMapTest extends TestCase {
 
    public void testLinkedHashMap() { 
        // we want to test the LinkedHashMap in access ordering mode. 
        LinkedHashMap map = new LinkedHashMap<String, String>(10, 0.75f, true);
 
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
 
        Iterator iterator = map.keySet().iterator();
        String id = (String) iterator.next();
        map.get(id);
        try { 
            iterator.next();
            // A LinkedHashMap is supposed to throw this Exception when a 
            // iterator.next() Operation takes place after a get 
            // Operation. This is because the get Operation is considered 
            // a structural modification if the LinkedHashMap is in 
            // access order mode. 
            fail("expected ConcurrentModificationException was not thrown.");
        } catch(ConcurrentModificationException expected) {
        } 
 
        LinkedHashMap mapClone = (LinkedHashMap) map.clone();
 
        iterator = map.keySet().iterator();
        id = (String) iterator.next();
        mapClone.get(id);
        iterator.next();
 
        try { 
            new LinkedHashMap<String, String>(-10, 0.75f, true);
            fail("IllegalArgumentException expected");