我是靠谱客的博主 默默秋天,最近开发中收集的这篇文章主要介绍Google Guava学习(7)-Guava集合工具 Bimap接口,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.功能:扩展map操作

2.代码:

package com.example.google.guava.demo.collection;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* <p>
* <code>BimapTest</code>
* </p>
* Description:
* 1.Bimap的值唯一;
*
* @author Mcchu
* @date 2017/10/19 16:45
*/
public class BimapTest {
public static void main(String[] args) {
BiMap<Integer, String> idMap = HashBiMap.create();
idMap.put(101, "Mahesh");
idMap.put(102, "Sohan");
idMap.put(103, "Ramesh");
// 1.关联键的值
Integer maheshId = idMap.inverse().get("Mahesh");
System.out.println("1.名字为Mehesh的id:"+maheshId);
// 2.空值
idMap.put(null,"Jack");
idMap.put(null,"Kite"); //BiMap继承Map,固键唯一,值覆盖
idMap.put(104,null);
//idMap.put(105,null);
//抛java.lang.IllegalArgumentException: value already present: null
Set<String> values = idMap.values();
System.out.print("2.输出所有值:");
for ( String value:values ){
System.out.print(value);
System.out.print(",");
}
System.out.println();
// 3.包容另一个map
Map<Integer,String> anotherIdMap = new HashMap<>();
anotherIdMap.put(105,"Jermy");
anotherIdMap.put(106,"Mercy");
anotherIdMap.put(107,"Abely");
anotherIdMap.put(108,"Jalen");
idMap.putAll(anotherIdMap);
System.out.println("3.输出添加新Map过后的所有值:");
for ( Integer id:idMap.keySet() ){
System.out.println( "
key:"+ id + ", value:" + idMap.get(id) );
}
System.out.println();
// 4.强制添加元素
idMap.forcePut(109,null); //强制添加null,不会因已存在null而抛异常,但会覆盖,Bimap始终允许一个值为null
Set<String> values1 = idMap.values();
System.out.print("4.输出所有值:");
for ( String value:values1 ){
System.out.print(value);
System.out.print(",");
}
System.out.println();
}
}
3.输出结果:

1.名字为Mehesh的id:101
2.输出所有值:Mahesh,Sohan,Ramesh,Kite,null,
3.输出添加新Map过后的所有值:
key:101, value:Mahesh
key:102, value:Sohan
key:103, value:Ramesh
key:null, value:Kite
key:104, value:null
key:105, value:Jermy
key:106, value:Mercy
key:107, value:Abely
key:108, value:Jalen
4.输出所有键值:
key:101, value:Mahesh
key:102, value:Sohan
key:103, value:Ramesh
key:null, value:Kite
key:105, value:Jermy
key:106, value:Mercy
key:107, value:Abely
key:108, value:Jalen
key:109, value:null

附录:

1. 参考:

http://www.yiibai.com/guava/guava_bimap.html
2. 接口源码参考:

package com.google.common.collect;
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
/**
* A bimap (or "bidirectional map") is a map that preserves the uniqueness of
* its values as well as that of its keys. This constraint enables bimaps to
* support an "inverse view", which is another bimap containing the same entries
* as this bimap but with reversed keys and values.
*
* <p>See the Guava User Guide article on <a href=
* "https://github.com/google/guava/wiki/NewCollectionTypesExplained#bimap">
* {@code BiMap}</a>.
*
* @author Kevin Bourrillion
* @since 2.0
*/
@GwtCompatible
public interface BiMap<K, V> extends Map<K, V> {
// Modification Operations
/**
* {@inheritDoc}
*
* @throws IllegalArgumentException if the given value is already bound to a
*
different key in this bimap. The bimap will remain unmodified in this
*
event. To avoid this exception, call {@link #forcePut} instead.
*/
@CanIgnoreReturnValue
@Override
@Nullable
V put(@Nullable K key, @Nullable V value);
/**
* An alternate form of {@code put} that silently removes any existing entry
* with the value {@code value} before proceeding with the {@link #put}
* operation. If the bimap previously contained the provided key-value
* mapping, this method has no effect.
*
* <p>Note that a successful call to this method could cause the size of the
* bimap to increase by one, stay the same, or even decrease by one.
*
* <p><b>Warning:</b> If an existing entry with this value is removed, the key
* for that entry is discarded and not returned.
*
* @param key the key with which the specified value is to be associated
* @param value the value to be associated with the specified key
* @return the value which was previously associated with the key, which may
*
be {@code null}, or {@code null} if there was no previous entry
*/
@CanIgnoreReturnValue
@Nullable
V forcePut(@Nullable K key, @Nullable V value);
// Bulk Operations
/**
* {@inheritDoc}
*
* <p><b>Warning:</b> the results of calling this method may vary depending on
* the iteration order of {@code map}.
*
* @throws IllegalArgumentException if an attempt to {@code put} any
*
entry fails. Note that some map entries may have been added to the
*
bimap before the exception was thrown.
*/
@Override
void putAll(Map<? extends K, ? extends V> map);
// Views
/**
* {@inheritDoc}
*
* <p>Because a bimap has unique values, this method returns a {@link Set},
* instead of the {@link java.util.Collection} specified in the {@link Map}
* interface.
*/
@Override
Set<V> values();
/**
* Returns the inverse view of this bimap, which maps each of this bimap's
* values to its associated key. The two bimaps are backed by the same data;
* any changes to one will appear in the other.
*
* <p><b>Note:</b>There is no guaranteed correspondence between the iteration
* order of a bimap and that of its inverse.
*
* @return the inverse view of this bimap
*/
BiMap<V, K> inverse();
}



最后

以上就是默默秋天为你收集整理的Google Guava学习(7)-Guava集合工具 Bimap接口的全部内容,希望文章能够帮你解决Google Guava学习(7)-Guava集合工具 Bimap接口所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部