Home  >  Article  >  Java  >  Java-Class Library-Guava-Bimap

Java-Class Library-Guava-Bimap

黄舟
黄舟Original
2017-01-19 13:18:421517browse

BiMap provides a new collection type, which provides a two-way associated data structure of key and value.

Normally, when we use Java Map, we often search for value through key, but if the following scenario occurs, we need to write some additional code. First, let's look at the following map structure that represents the identification number and file name.

[code]  
    @Test
    public void logMapTest(){
        Map<Integer,String> logfileMap = Maps.newHashMap();
        logfileMap.put(1,"a.log");
        logfileMap.put(2,"b.log");
        logfileMap.put(3,"c.log");        
        System.out.println("logfileMap:"+logfileMap);        
    }

When we need to find the file name by serial number, it is very simple. But if we need to find the serial number through the file name, we have to traverse the map. Of course, we can also write a Map inversion method to help realize the inverted mapping relationship.

[code] /**
     * 逆转Map的key和value
     * @param <S>
     * @param <T>
     * @param map
     * @return
     */
    public static <S,T> Map<T,S> getInverseMap(Map<S,T> map) {
        Map<T,S> inverseMap = new HashMap<T,S>();
        for(Entry<S,T> entry: map.entrySet()) {
            inverseMap.put(entry.getValue(), entry.getKey());
        }
        return inverseMap;
    }
[code] @Test
    public void logMapTest(){
        Map<Integer,String> logfileMap = Maps.newHashMap();
        logfileMap.put(1,"a.log");
        logfileMap.put(2,"b.log");
        logfileMap.put(3,"c.log");

        System.out.println("logfileMap:"+logfileMap);

        Map<String,Integer> logfileInverseMap = Maps.newHashMap();

        logfileInverseMap=getInverseMap(logfileMap);

        System.out.println("logfileInverseMap:"+logfileInverseMap);
    }


The above code can help us realize the map inversion requirements, but there are still some issues we need to consider:

 1. How to deal with duplicate values. If not considered, overwriting will occur during inversion.

2. If a new key is added to the inverted map, does the map before inversion need to update a value?

In this case, the number of things other than business that need to be considered increases, and the code written becomes less readable. At this time we can consider using BiMap in Guava.

 Bimap

Bimap is very simple to use. For the above usage scenario, we can implement it with very simple code:

[code] @Test
    public void BimapTest(){
        BiMap<Integer,String> logfileMap = HashBiMap.create(); 
        logfileMap.put(1,"a.log");
        logfileMap.put(2,"b.log");
        logfileMap.put(3,"c.log"); 
        System.out.println("logfileMap:"+logfileMap); 
        BiMap<String,Integer> filelogMap = logfileMap.inverse();
        System.out.println("filelogMap:"+filelogMap);
    }
Mandatory uniqueness of Bimap data Characteristic

When using BiMap, the uniqueness of Value will be required. If the value is repeated, an error will be thrown: java.lang.IllegalArgumentException, for example:
[code]  @Test
    public void BimapTest(){
        BiMap<Integer,String> logfileMap = HashBiMap.create(); 
        logfileMap.put(1,"a.log");
        logfileMap.put(2,"b.log");
        logfileMap.put(3,"c.log");         
        logfileMap.put(4,"d.log"); 
        logfileMap.put(5,"d.log"); 
    }

LogfileMap.put(5,"d.log") will throw java.lang.IllegalArgumentException: value already present: d.log error. If we really need to insert repeated value values, we can choose the forcePut method. But what we need to note is that the previous key will also be overwritten.

[code] @Test
    public void BimapTest(){
        BiMap<Integer,String> logfileMap = HashBiMap.create(); 
        logfileMap.put(1,"a.log");
        logfileMap.put(2,"b.log");
        logfileMap.put(3,"c.log"); 

        logfileMap.put(4,"d.log"); 
        logfileMap.forcePut(5,"d.log"); 
        System.out.println("logfileMap:"+logfileMap); 
    }

    输出:
    logfileMap:{5=d.log, 3=c.log, 2=b.log, 1=a.log}

Understanding the inverse method

The inverse method will return an inverted BiMap, but note that this inverted map is not a new map object. It implements a view association, so that you can All operations on the reversed map will affect the original map object. For example:

[code]
    @Test
    public void BimapTest(){
        BiMap<Integer,String> logfileMap = HashBiMap.create(); 
        logfileMap.put(1,"a.log");
        logfileMap.put(2,"b.log");
        logfileMap.put(3,"c.log"); 
        System.out.println("logfileMap:"+logfileMap); 
        BiMap<String,Integer> filelogMap = logfileMap.inverse();
        System.out.println("filelogMap:"+filelogMap);

        logfileMap.put(4,"d.log"); 

        System.out.println("logfileMap:"+logfileMap); 
        System.out.println("filelogMap:"+filelogMap); 
    }

Implementation class of BiMap

Key-Value Map Impl Value-Key Map Impl Corresponding BiMap

HashMap HashMap HashBiMap

ImmutableMap ImmutableMap ImmutableBiMap

EnumMap EnumMap EnumBiMap

EnumMap HashMap EnumHashBiMap

The above is the content of Java-Class Library-Guava-Bimap. For more related content, please pay attention to the PHP Chinese website (www.php. cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn