search
HomeJavajavaTutorialJava-Class Library-Guava-Bimap

Java-Class Library-Guava-Bimap

Jan 19, 2017 pm 01:18 PM

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)