Example usage for java.util LinkedHashMap LinkedHashMap

List of usage examples for java.util LinkedHashMap LinkedHashMap

Introduction

In this page you can find the example usage for java.util LinkedHashMap LinkedHashMap.

Prototype

public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) 

Source Link

Document

Constructs an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode.

Usage

From source file:Main.java

public static void main(String[] args) {

    LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>(5, 0.75F, true);

    // add some values in the map
    map.put("One", 1);
    map.put("Two", 2);
    map.put("Three", 3);

    System.out.println(map);/*w  w  w  .  ja  v  a 2  s.c o  m*/

    // clear the map
    map.clear();

    System.out.println(map);
}

From source file:Main.java

public static void main(String[] args) {
    LinkedHashMap lhm = new LinkedHashMap(MAX_ENTRIES + 1, .75F, false) {

        protected boolean removeEldestEntry(Map.Entry eldest) {
            return size() > MAX_ENTRIES;
        }/* www .ja  v  a2 s.c o m*/
    };
    lhm.put(0, "H");
    lhm.put(1, "E");
    lhm.put(2, "L");
    lhm.put(3, "L");
    lhm.put(4, "O");

    System.out.println(lhm);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    final int MAX_ENTRIES = 100;
    Map cache = new LinkedHashMap(MAX_ENTRIES + 1, .75F, true) {
        public boolean removeEldestEntry(Map.Entry eldest) {
            return size() > MAX_ENTRIES;
        }/*from   w w w  . j  ava 2  s . co  m*/
    };

    Object key = "key";
    Object value = "value";
    cache.put(key, value);
    Object o = cache.get(key);
    if (o == null && !cache.containsKey(key)) {
    }
    cache = (Map) Collections.synchronizedMap(cache);
}

From source file:Main.java

public static <K, V> Map<K, V> newCacheMap(int initialCapacity, float loadFactor, boolean accessOrder) {
    return new LinkedHashMap<K, V>(initialCapacity, loadFactor, accessOrder);
}

From source file:Main.java

public static Class<?> findClass(String name) {
    ClassLoader cl;//  w w  w . j a v  a 2 s. c  o m
    try {
        cl = Thread.currentThread().getContextClassLoader();
    } catch (SecurityException e) {
        cl = null;
    }

    Map<String, Class<?>> map;
    synchronized (cache) {
        map = cache.get(cl);

        if (map == null) {
            map = new LinkedHashMap<String, Class<?>>(16, 0.75f, true) {
                private static final long serialVersionUID = 1L;

                protected boolean removeEldestEntry(Map.Entry<String, Class<?>> eldest) {
                    return size() > 1024;
                };
            };
            cache.put(cl, map);
        }
    }
    synchronized (map) {
        if (!map.containsKey(name)) {
            Class<?> target;
            try {
                if (cl != null) {
                    target = cl.loadClass(name);
                } else {
                    target = Class.forName(name);
                }
            } catch (ClassNotFoundException e) {
                target = null;
            }
            map.put(name, target);
        }
        return map.get(name);
    }
}

From source file:org.rimudb.util.LRUCache2.java

 /**
 * Creates a new LRU cache./*from   www .j  a  va  2 s .co m*/
 * 
 * @param cacheSize The maximum number of entries that will be kept in this cache.
 */
public LRUCache2(int cacheSize) {
   this.cacheSize = cacheSize;
   int hashTableCapacity = (int) Math.ceil(cacheSize / hashTableLoadFactor) + 1;
      
   map = new LinkedHashMap<K, V>(hashTableCapacity, hashTableLoadFactor, true) {
      private static final long serialVersionUID = 1;

      @Override
      protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
         return size() > LRUCache2.this.cacheSize;
      }
   };
}