Java Class that implements Map and keeps insertion order?

后端 未结 9 2204
一向
一向 2020-11-22 11:28

I\'m looking for a class in java that has key-value association, but without using hashes. Here is what I\'m currently doing:

  1. Add values to a Hashtable<
9条回答
  •  不要未来只要你来
    2020-11-22 11:43

    Either You can use LinkedHashMap or you can implement you own CustomMap which maintains insertion order.

    You can use the Following CustomHashMap with the following features:

    • Insertion order is maintained, by using LinkedHashMap internally.
    • Keys with null or empty strings are not allowed.
    • Once key with value is created, we are not overriding its value.

    HashMap vs LinkedHashMap vs CustomHashMap

    interface CustomMap extends Map {
        public boolean insertionRule(K key, V value);
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public class CustomHashMap implements CustomMap {
        private Map entryMap;
        // SET: Adds the specified element to this set if it is not already present.
        private Set entrySet;
    
        public CustomHashMap() {
            super();
            entryMap = new LinkedHashMap();
            entrySet = new HashSet();
        }
    
        @Override
        public boolean insertionRule(K key, V value) {
            // KEY as null and EMPTY String is not allowed.
            if (key == null || (key instanceof String && ((String) key).trim().equals("") ) ) {
                return false;
            }
    
            // If key already available then, we are not overriding its value.
            if (entrySet.contains(key)) { // Then override its value, but we are not allowing
                return false;
            } else { // Add the entry
                entrySet.add(key);
                entryMap.put(key, value);
                return true;
            }
        }
        public V put(K key, V value) {
            V oldValue = entryMap.get(key);
            insertionRule(key, value);
            return oldValue;
        }
        public void putAll(Map t) {
            for (Iterator i = t.keySet().iterator(); i.hasNext();) {
                K key = (K) i.next();
                insertionRule(key, t.get(key));
            }
        }
    
        public void clear() {
            entryMap.clear();
            entrySet.clear();
        }
        public boolean containsKey(Object key) {
            return entryMap.containsKey(key);
        }
        public boolean containsValue(Object value) {
            return entryMap.containsValue(value);
        }
        public Set entrySet() {
            return entryMap.entrySet();
        }
        public boolean equals(Object o) {
            return entryMap.equals(o);
        }
        public V get(Object key) {
            return entryMap.get(key);
        }
        public int hashCode() {
            return entryMap.hashCode();
        }
        public boolean isEmpty() {
            return entryMap.isEmpty();
        }
        public Set keySet() {
            return entrySet;
        }
        public V remove(Object key) {
            entrySet.remove(key);
            return entryMap.remove(key);
        }
        public int size() {
            return entryMap.size();
        }
        public Collection values() {
            return entryMap.values();
        }
    }
    

    Usage of CustomHashMap:

    public static void main(String[] args) {
        System.out.println("== LinkedHashMap ==");
        Map map2 = new LinkedHashMap();
        addData(map2);
    
        System.out.println("== CustomHashMap ==");
        Map map = new CustomHashMap();
        addData(map);
    }
    public static void addData(Map map) {
        map.put(null, "1");
        map.put("name", "Yash");
        map.put("1", "1 - Str");
        map.put("1", "2 - Str"); // Overriding value
        map.put("", "1"); // Empty String
        map.put(" ", "1"); // Empty String
        map.put(1, "Int");
        map.put(null, "2"); // Null
    
        for (Map.Entry entry : map.entrySet()) {
            System.out.println(entry.getKey() + " = " + entry.getValue());
        }
    }
    

    O/P:

    == LinkedHashMap == | == CustomHashMap ==
    null = 2            | name = Yash
    name = Yash         | 1 = 1 - Str
    1 = 2 - Str         | 1 = Int
     = 1                |
      = 1               |
    1 = Int             |
    

    If you know the KEY's are fixed then you can use EnumMap. Get the values form Properties/XML files

    EX:

    enum ORACLE {
        IP, URL, USER_NAME, PASSWORD, DB_Name;
    }
    
    EnumMap props = new EnumMap(ORACLE.class);
    props.put(ORACLE.IP, "127.0.0.1");
    props.put(ORACLE.URL, "...");
    props.put(ORACLE.USER_NAME, "Scott");
    props.put(ORACLE.PASSWORD, "Tiget");
    props.put(ORACLE.DB_Name, "MyDB");
    

提交回复
热议问题