A simple sample of ArBiMap (two-way map)

Table of Contents ⇒ Java Algorithm Library-Artery-Sample

Q02_00.java


package jp.avaj.lib.algo;

import jp.avaj.lib.test.L;

/**
 *  ArBiMap(Bidirectional Map)Simple sample of
 *
 *-Map gets the value from the key, but sometimes you want to get the key from the value.
 *・ In such a case, you will have to create another Map with the keys and values reversed, but it feels like "it's really bad"..
 *
 *-ArBiMap is a bidirectional map that allows you to get the other party from either the key or the value..
 *・ The terms "key" and "value" are used here for convenience, but this is just a word and there is no distinction..
 *
 *・ It is the user's responsibility to make each key and value unique..
 *
 */
public class Q02_00 {
  public static void main(String[] args) {
    ArBiMap<String,String> biMap = new ArBiMap<String,String>();
    L.p("Element settings");
    {
      biMap.putKeyValue("a0","b0");
      biMap.putKeyValue("a1","b1");
      biMap.putKeyValue("a2","b2");
      biMap.putKeyValue("a3","b3");
      biMap.putKeyValue("a4","b4");
      //Check the contents
      L.p(biMap.toString());
    }
    L.p("Get the value from the key");
    {
      L.p(biMap.getByKey("a1"));
      L.p(biMap.getByKey("a3"));
    }
    L.p("Get the key from the value");
    {
      L.p(biMap.getByValue("b0"));
      L.p(biMap.getByValue("b2"));
      L.p(biMap.getByValue("b4"));
    }
    L.p("Specify a key to delete an element");
    //The corresponding value is returned
    {
      L.p(biMap.removeByKey("a1"));
      L.p(biMap.removeByKey("a3"));
      //Check the contents
      L.p(biMap.toString());
    }
    L.p("Delete an element by specifying a value");
    //The corresponding key is returned
    {
      L.p(biMap.removeByValue("b0"));
      L.p(biMap.removeByValue("b2"));
      //Check the contents
      L.p(biMap.toString());
    }
    L.p("Clear the element");
    {
      biMap.clear();
      //Check the contents
      L.p(biMap.toString());
    }
  }
}

The result is as follows.

result.txt


Element settings
{a1=b1, a2=b2, a3=b3, a4=b4, a0=b0}
Get the value from the key
b1
b3
Get the key from the value
a0
a2
a4
Specify a key to delete an element
b1
b3
{a2=b2, a4=b4, a0=b0}
Delete an element by specifying a value
a0
a2
{a4=b4}
Clear the element
{}

Recommended Posts

A simple sample of ArBiMap (two-way map)
A simple sample callback in Java
A simple sample of displaying a QRCode on a website with JSP + ZXing
A simple example of an MVC model
A simple example of a Servlet that displays Japanese
A collection of simple questions for Java beginners
CICS-Run Java application-(1) Run a simple sample app
Let's make a robot! "A simple demo of Java AWT Robot"
I wrote a sequence diagram of the j.u.c.Flow sample
A Simple CRUD Sample Using Java Servlet / JSP and MySQL
I made a simple MVC sample system using Spring Boot