GuavaMultimap类

Sublime ww9992019com 发表于 5 天前

www.lyjust.net开户请拨18288069766闺蜜抢买单引冲突上海长宁区CyzvUL<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="6122">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>

多重映射接口扩展映射,使得其键一次可被映射到多个值。</p>

<h2>

接口声明</h2>

<p>

以下是com.google.common.collect.Multimap&lt;K,V&gt;接口的声明:</p>

<pre class="prettyprint" courier new monospace border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: line-height: font-size: overflow: auto color: rgb>

@GwtCompatible

public interface Multimap&lt;K,V&gt;
</pre>

<h2>

接口方法</h2>

<table class="src">

<tbody>

    <tr>

        <th>

            S.N.</th>

        <th>

            方法及说明</th>

    </tr>

    <tr>

        <td>

            1</td>

        <td>

            <b>Map&lt;K,Collection&lt;V&gt;&gt; asMap()</b><br>

            返回此multimap中的视图,从每个不同的键在键的关联值的非空集合映射。</td>

    </tr>

    <tr>

        <td>

            2</td>

        <td>

            <b>void clear()</b><br>

            将删除所有multimap中的键值对,留下空。</td>

    </tr>

    <tr>

        <td>

            3</td>

        <td>

            <b>boolean containsEntry(Object key, Object value)</b><br>

            返回true如果此多重映射包含至少一个键 - 值对用键键和值value。</td>

    </tr>

    <tr>

        <td>

            4</td>

        <td>

            <b>boolean containsKey(Object key)</b><br>

            返回true,如果这个multimap中至少包含一个键值对的键key。</td>

    </tr>

    <tr>

        <td>

            5</td>

        <td>

            <b>boolean containsValue(Object value)</b><br>

            返回true,如果这个multimap至少包含一个键值对的值值。</td>

    </tr>

    <tr>

        <td>

            6</td>

        <td>

            <b>Collection&lt;Map.Entry&lt;K,V&gt;&gt; entries()</b><br>

            返回包含在此multimap中,为Map.Entry的情况下,所有的键 - 值对的视图集合。</td>

    </tr>

    <tr>

        <td>

            7</td>

        <td>

            <b>boolean equals(Object obj)</b><br>

            比较指定对象与此多重映射是否相等。</td>

    </tr>

    <tr>

        <td>

            8</td>

        <td>

            <b>Collection&lt;V&gt; get(K key)</b><br>

            返回,如果有的话,在这个multimap中键关联的值的视图集合。</td>

    </tr>

    <tr>

        <td>

            9</td>

        <td>

            <b>int hashCode()</b><br>

            返回此多重映射的哈希码。</td>

    </tr>

    <tr>

        <td>

            10</td>

        <td>

            <b>boolean isEmpty()</b><br>

            返回true,如果这个multimap中未包含键 - 值对。</td>

    </tr>

    <tr>

        <td>

            11</td>

        <td>

            <b>Multiset&lt;K&gt; keys()</b><br>

            返回一个视图集合包含从每个键值对这个multimap中的关键,没有折叠重复。</td>

    </tr>

    <tr>

        <td>

            12</td>

        <td>

            <b>Set&lt;K&gt; keySet()</b><br>

            Returns a view collection of all distinct keys contained in this multimap.</td>

    </tr>

    <tr>

        <td>

            13</td>

        <td>

            <b>boolean put(K key, V value)</b><br>

            存储键 - 值对在这个multimap中。</td>

    </tr>

    <tr>

        <td>

            14</td>

        <td>

            <b>boolean putAll(K key, Iterable&lt;? extends V&gt; values)</b><br>

            存储一个键 - 值对在此multimap中的每个值,都使用相同的键 key。</td>

    </tr>

    <tr>

        <td>

            15</td>

        <td>

            <b>boolean putAll(Multimap&lt;? extends K,? extends V&gt; multimap)</b><br>

            存储了所有键 - 值对多重映射在这个multimap中,通过返回 multimap.entries() 的顺序<span>.</span></td>

    </tr>

    <tr>

        <td>

            16</td>

        <td>

            <b>boolean remove(Object key, Object value)</b><br>

            删除一个键 - 值对用键键,并从该多重映射的值的值,如果这样的存在。</td>

    </tr>

    <tr>

        <td>

            17</td>

        <td>

            <b>Collection&lt;V&gt; removeAll(Object key)</b><br>

            删除与键键关联的所有值。</td>

    </tr>

    <tr>

        <td>

            18</td>

        <td>

            <b>Collection&lt;V&gt; replaceValues(K key, Iterable&lt;? extends V&gt; values)</b><br>

            存储与相同的键值,替换任何现有值的键的集合。</td>

    </tr>

    <tr>

        <td>

            19</td>

        <td>

            <b>int size()</b><br>

            返回此多重映射键 - 值对的数量。</td>

    </tr>

    <tr>

        <td>

            20</td>

        <td>

            <b>Collection&lt;V&gt; values()</b><br>

            返回一个视图集合包含从包含在该multimap中的每个键 - 值对的值,而不发生重复 (so values().size() == size()).</td>

    </tr>

</tbody>

</table>

<h2>

Multimap 示例</h2>

<p>

使用所选择的任何编辑器创建下面的java程序 <strong>C:/&gt; Guava</strong></p>

<i>GuavaTester.java</i>

<pre class="prettyprint tryit" courier new monospace border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: line-height: font-size: overflow: auto color: rgb cursor: default background-image: url background-attachment: initial background-size: background-origin: background-clip: background-position: background-repeat: no-repeat>

import java.util.Collection;

import java.util.List;

import java.util.Map;

import java.util.Set;

import com.google.common.collect.ArrayListMultimap;

import com.google.common.collect.Multimap;

public class GuavaTester {

public static void main(String args[]){

  GuavaTester tester = new GuavaTester();

  Multimap&lt;String,String&gt; multimap = tester.getMultimap();



  List&lt;String&gt; lowerList = (List&lt;String&gt;)multimap.get("lower");

  System.out.println("Initial lower case list");

  System.out.println(lowerList.toString());

  lowerList.add("f");

  System.out.println("Modified lower case list");

  System.out.println(lowerList.toString());



  List&lt;String&gt; upperList = (List&lt;String&gt;)multimap.get("upper");

  System.out.println("Initial upper case list");

  System.out.println(upperList.toString());

  upperList.remove("D");

  System.out.println("Modified upper case list");

  System.out.println(upperList.toString());



  Map&lt;String, Collection&lt;String&gt;&gt; map = multimap.asMap();

  System.out.println("Multimap as a map");

  for (Map.Entry&lt;String,  Collection&lt;String&gt;&gt; entry : map.entrySet()) {

     String key = entry.getKey();

     Collection&lt;String&gt; value =  multimap.get("lower");

     System.out.println(key + ":" + value);

  }



  System.out.println("Keys of Multimap");

  Set&lt;String&gt; keys =  multimap.keySet();

  for(String key:keys){

     System.out.println(key);

  }



  System.out.println("Values of Multimap");

  Collection&lt;String&gt; values = multimap.values();

  System.out.println(values);

}

private Multimap&lt;String,String&gt; getMultimap(){

  //Map&lt;String, List&lt;String&gt;&gt;

  // lower -&gt; a, b, c, d, e

  // upper -&gt; A, B, C, D



  Multimap&lt;String,String&gt; multimap = ArrayListMultimap.create();



  multimap.put("lower", "a");

  multimap.put("lower", "b");

  multimap.put("lower", "c");

  multimap.put("lower", "d");

  multimap.put("lower", "e");



  multimap.put("upper", "A");

  multimap.put("upper", "B");

  multimap.put("upper", "C");

  multimap.put("upper", "D");

  return multimap;

}

}
</pre>

<h2>

验证结果</h2>

<p>

使用javac编译器编译如下类</p>

<pre class="result" courier new monospace margin-bottom: border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: font-size: overflow: auto color: rgb line-height: background-color:>

C:\Guava&gt;javac GuavaTester.java
</pre>

<p>

现在运行GuavaTester看到的结果</p>

<pre class="result" courier new monospace margin-bottom: border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: font-size: overflow: auto color: rgb line-height: background-color:>

C:\Guava&gt;java GuavaTester
</pre>

<p>

看到结果。</p>

<pre class="result" courier new monospace margin-bottom: border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: font-size: overflow: auto color: rgb line-height: background-color:>

Initial lower case list

[a, b, c, d, e]

Modified lower case list

[a, b, c, d, e, f]

Initial upper case list

[A, B, C, D]

Modified upper case list

[A, B, C]

Multimap as a map

upper:[a, b, c, d, e, f]

lower:[a, b, c, d, e, f]

Keys of Multimap

upper

lower

Values of Multimap

[A, B, C, a, b, c, d, e, f]</pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="Guava BigIntegerMath类"><i class="layui-icon"></i>
                            Guava BigIntegerMath类</a></span>
                                            <span style="float:right;">下一篇:哥,这回真没有了</span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复