
Guava - Useful Classes
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - LoadingCache Interface
Guava - Collection Utilities
- Guava - Collections Utilities
- Guava - MultiSet Interface
- Guava - MultiMap Interface
- Guava - BiMap Interface
- Guava - Table Interface
Guava - String Utilities
- Guava - String Utilities
- Guava - Joiner class
- Guava - Splitter
- Guava - CharMatcher
- Guava - CaseFormat
Guava - Primitive Utilities
- Guava - Primitive Utilities
- Guava - Bytes
- Guava - Shorts
- Guava - Ints
- Guava - Longs
- Guava - Floats
- Guava - Doubles
- Guava - Chars
- Guava - Booleans
Guava - Math Utilities
Guava - Useful Resources
Guava - BiMap Interface
A BiMap is a special kind of map which maintains an inverse view of the map while ensuring that no duplicate values are present in the map and a value can be used safely to get the key back.
Interface Declaration
Following is the declaration for com.google.common.collect.Bimap<K,V> interface −
@GwtCompatible public interface BiMap<K,V> extends Map<K,V>
Sr.No | Method & Description |
---|---|
1 |
V forcePut(K key, V value) An alternate form of 'put' that silently removes any existing entry with the value before proceeding with the put(K, V) operation. |
2 |
BiMap<V,K> inverse() Returns the inverse view of this bimap, which maps each of this bimap's values to its associated key. |
3 |
V put(K key, V value) Associates the specified value with the specified key in this map (optional operation). |
4 |
void putAll(Map<? extends K,? extends V> map) Copies all of the mappings from the specified map to this map (optional operation). |
5 |
Set<V> values() Returns a Collection view of the values contained in this map. |
Methods Inherited
This class inherits methods from the following interface −
- java.util.Map
Example - Adding values to a BiMap
GuavaTester.java
package com.tutorialspoint; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; public class GuavaTester { public static void main(String args[]) { BiMap<Integer, String> empIDNameMap = HashBiMap.create(); empIDNameMap.put(Integer.valueOf(101), "Mahesh"); empIDNameMap.put(Integer.valueOf(102), "Sohan"); empIDNameMap.put(Integer.valueOf(103), "Ramesh"); // Get Name of Employee with Emp Id 101 System.out.println(empIDNameMap.get(Integer.valueOf(101))); } }
Output
Run the GuavaTester and verify the output −
Mahesh
Example - Getting Key based on Value from a BiMap
GuavaTester.java
package com.tutorialspoint; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; public class GuavaTester { public static void main(String args[]) { BiMap<Integer, String> empIDNameMap = HashBiMap.create(); empIDNameMap.put(Integer.valueOf(101), "Mahesh"); empIDNameMap.put(Integer.valueOf(102), "Sohan"); empIDNameMap.put(Integer.valueOf(103), "Ramesh"); //Emp Id of Employee "Mahesh" System.out.println(empIDNameMap.inverse().get("Mahesh")); } }
Output
Run the GuavaTester and verify the output −
101
Example - Getting values of a BiMap
GuavaTester.java
package com.tutorialspoint; import java.util.Collection; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; public class GuavaTester { public static void main(String args[]) { BiMap<Integer, String> empIDNameMap = HashBiMap.create(); empIDNameMap.put(Integer.valueOf(101), "Mahesh"); empIDNameMap.put(Integer.valueOf(102), "Sohan"); empIDNameMap.put(Integer.valueOf(103), "Ramesh"); System.out.println("Values of Bimap"); Collection<String> values = empIDNameMap.values(); System.out.println(values); } }
Output
Run the GuavaTester and verify the output −
Values of Bimap [Mahesh, Sohan, Ramesh]