Guava – bimap

Guava – bimap

bimap即双向map,是一个既保留其值的唯一性又保留其键的唯一性的map。BiMaps支持 inverse view ,它是另一个bimap,包含与这个bimap相同的条目,但是键和值是相反的。

声明 : com.google.common.collect.Bimap < K, V > 接口的声明如下。

@GwtCompatible
public interface BiMap< **K, V** >
extends Map< **K, V** >

以下是Guava BiMap接口提供的一些方法:
Guava - bimap
返回值和异常:

  • put : 如果给定的值已经被绑定到这个bimap的不同键上,则抛出IllegalArgumentException。在这个事件中,bimap将保持不被修改。
  • forcePut : 返回先前与键相关的值,该值可能为空,如果没有先前的条目则为空。
  • putAll : 如果试图放入任何条目失败,则抛出IllegalArgumentException。注意,在抛出异常之前,一些map条目可能已经被添加到bimap中。
  • values : 返回一个Set,而不是Map接口中指定的Collection,因为一个bimap有唯一的值。
  • inverse : 返回这个bimap的反向视图。

以下是Guava BiMap接口的实现:

// Java code to show implementation for
// Guava BiMap interface
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
  
class GFG {
  
    // Driver method
    public static void main(String args[])
    {
  
        // Creating a BiMap with first field as
        // an Integer and second field as String
        // stuRollMap is name of BiMap
        // i.e, the first field of BiMap stores
        // the Roll no. of student and second
        // field stores the name of Student
        BiMap<Integer, String> stuRollMap = HashBiMap.create();
  
        stuRollMap.put(new Integer(2), "Sahil");
        stuRollMap.put(new Integer(6), "Dhiman");
        stuRollMap.put(new Integer(9), "Shubham");
        stuRollMap.put(new Integer(15), "Abhishek");
  
        // To display Roll no. of student "Dhiman"
        System.out.println(stuRollMap.inverse().get("Dhiman"));
  
        // To display Roll no. of student "Shubham"
        System.out.println(stuRollMap.inverse().get("Shubham"));
    }
}

输出:

6
9

赞(1)

评论 抢沙发

评论前必须登录!

 

Guava 教程
Guava 教程Guava - 概述Guava - 环境搭建Guava - Optional类Guava - Preconditions类Guava - Ordering类Guava - Objects类Guava - Range类Guava - Throwables类Guava - Collections 工具类Guava - Caching 工具类Guava - Multiset接口Guava - bimapGuava - MultiMap
Guava Ints类
Guava Ints类Guava Ints - concat() 函数Guava Ints - lastIndexOf() 函数Guava Ints - contains() 函数Guava Ints - asList() 函数Guava Ints - max() 函数Guava Ints - min() 函数Guava Ints - join() 函数Guava Ints - toArray() 函数Guava Ints - indexOf() 函数
Guava Chars类
Guava Chars类Guava - Chars.compare()方法与实例Guava - Chars.hashCode()方法与实例Guava - Chars.min()方法与实例Guava - Chars.max()方法与实例Guava - Chars.toArray()方法与实例Guava - Chars.concat()方法与实例Guava - Chars.contains()方法与实例Guava - Chars.lastIndexOf()方法与实例Guava - Chars.join()方法与实例Guava - Chars.indexOf()方法与实例Guava - Chars.asList()方法与实例
Guava LongMath类
Guava LongMath类Guava - LongMath.binomial方法与实例Guava - LongMath.log2方法与实例Guava - LongMath.pow方法与实例Guava - LongMath.isPowerOfTwo方法与实例Guava - LongMath.gcd方法与实例Guava - LongMath.mean方法与实例Guava - LongMath.sqrt方法与实例Guava - LongMath.mod方法与实例Guava - LongMath.log10方法与实例Guava - LongMath.checkedMultiply方法与实例Guava - LongMath.checkedAdd方法与实例Guava - LongMath.checkedPow方法与实例Guava - LongMath.divide方法与实例
Guava Longs类
Guava Longs类Guava - Longs.compare()方法与实例Guava - Longs.hashCode()方法与实例Guava - Longs.factorial方法与实例Guava - Longs.contains()方法与实例Guava - Longs.min()方法与实例Guava - Longs.max()方法与实例Guava - Longs.toArray()方法与实例Guava - Longs.lastIndexOf()方法与实例Guava - Longs.join()方法与实例Guava - Longs.asList()方法与实例Guava - Longs.checkedSubtract()方法与实例Guava - Longs.concat()方法与实例Guava - Longs.indexOf()方法与实例
Guava Booleans类
Guava Booleans类Guava - Booleans.asList()方法及示例Guava - Booleans.compare()方法及示例Guava - Booleans.concat()方法及示例Guava - Booleans.contains()方法及示例Guava - Booleans.countTrue()方法及示例Guava - Booleans.hashCode()方法及示例Guava - Booleans.indexOf()方法及示例Guava - Booleans.join()方法及示例Guava - Booleans.lastIndexOf()方法及示例Guava - Booleans.toArray()方法及示例
Guava Shorts类
Guava Shorts类Guava - Shorts.toArray()方法及实例Guava - Shorts.min()方法及实例Guava - Shorts.max()方法及实例Guava - Shorts.lastIndexOf()方法及实例Guava - Shorts.join()方法及实例Guava - Shorts.indexOf()方法及实例Guava - Shorts.hashCode()方法及实例Guava - Shorts.contains()方法及实例Guava - Shorts.concat()方法及实例Guava - Shorts.asList()方法及实例
Guava IntMath类
Guava IntMath类Guava - IntMath.checkedAdd()方法及实例Guava - IntMath.checkedMultiply()方法及实例Guava - IntMath.checkedPow()方法及实例Guava - IntMath.checkedSubtract()方法及实例Guava - IntMath.divide()方法及实例Guava - IntMath.log10()方法及实例