GuavaBooleans类

Sublime ww9992019com 发表于 1 周前

<div style="width:100%;float:left;" class="article-content">

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

                <p>

Booleans是布尔型基本的实用工具类。</p>

<h2>

类声明</h2>

<p>

以下是com.google.common.primitives.Booleans类的声明:</p>

<pre class="prettyprint prettyprinted" 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>

<span class="lit">@GwtCompatible </span><span class="pun">(</span><span class="pln">emulated</span><span class="pun">=</span><span class="kwd">true</span><span class="pun">)</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Booleans</span><span class="pln">

  </span><span class="kwd">extends</span><span class="pln"> </span><span class="typ">Object</span></pre>

<h2>

方法</h2>

<table class="src">

<tbody>

    <tr>

        <th>

            S.N.</th>

        <th>

            方法及说明</th>

    </tr>

    <tr>

        <td>

            1</td>

        <td>

            <b>static List&lt;Boolean&gt; asList(boolean... backingArray)</b><br>

            返回由指定数组支持的固定大小的列表,类似 Arrays.asList(Object[]).</td>

    </tr>

    <tr>

        <td>

            2</td>

        <td>

            <b>static int compare(boolean a, boolean b)</b><br>

            比较两个指定的布尔值的标准方式(假的比真的少考虑以下)。</td>

    </tr>

    <tr>

        <td>

            3</td>

        <td>

            <b>static boolean[] concat(boolean[]... arrays)</b><br>

            每个数组提供组合成一个单一的数组,则值<span>返回。</span></td>

    </tr>

    <tr>

        <td>

            4</td>

        <td>

            <b>static boolean contains(boolean[] array, boolean target)</b><br>

            返回true,如果target存在在任何地方数组元素。</td>

    </tr>

    <tr>

        <td>

            5</td>

        <td>

            <b>static int countTrue(boolean... values)</b><br>

            返回为true值的数目。</td>

    </tr>

    <tr>

        <td>

            6</td>

        <td>

            <b>static boolean[] ensureCapacity(boolean[] array, int minLength, int padding)</b><br>

            返回一个包含相同的值数组的数组,但保证是一个规定的最小长度。</td>

    </tr>

    <tr>

        <td>

            7</td>

        <td>

            <b>static int hashCode(boolean value)</b><br>

            返回哈希码的值;等于调用的结果 ((Boolean) value).hashCode().</td>

    </tr>

    <tr>

        <td>

            8</td>

        <td>

            <b>static int indexOf(boolean[] array, boolean target)</b><br>

            返回目标数组的首次出现的索引值。</td>

    </tr>

    <tr>

        <td>

            9</td>

        <td>

            <b>static int indexOf(boolean[] array, boolean[] target)</b><br>

            返回指定目标的第一个匹配的起始位置数组内,或-1,如果不存在。</td>

    </tr>

    <tr>

        <td>

            10</td>

        <td>

            <b>static String join(String separator, boolean... array)</b><br>

            返回包含由分离器分离所提供的布尔值的字符串。</td>

    </tr>

    <tr>

        <td>

            11</td>

        <td>

            <b>static int lastIndexOf(boolean[] array, boolean target)</b><br>

            返回target 在数组中最后一个出现的索引值。</td>

    </tr>

    <tr>

        <td>

            12</td>

        <td>

            <b>static Comparator&lt;boolean[]&gt; lexicographicalComparator()</b><br>

            返回一个比较器,它比较两个布尔数组字典顺序。</td>

    </tr>

    <tr>

        <td>

            13</td>

        <td>

            <b>static boolean[] toArray(Collection&lt;Boolean&gt; collection)</b><br>

            复制Boolean实例集合到原始的布尔值的新数组。</td>

    </tr>

</tbody>

</table>

<h2>

方法继承</h2>

<p>

这个类继承了以下类方法:</p>

<ul>

<li>

    <p>

        java.lang.Object</p>

</li>

</ul>

<h2>

Booleans 示例</h2>

<p>

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

<i>GuavaTester.java</i>

<pre class="prettyprint tryit prettyprinted" 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 background-image: url background-attachment: initial background-size: background-origin: background-clip: background-position: background-repeat: no-repeat>

<span class="kwd">import</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">util</span><span class="pun">.</span><span class="typ">List</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> com</span><span class="pun">.</span><span class="pln">google</span><span class="pun">.</span><span class="pln">common</span><span class="pun">.</span><span class="pln">primitives</span><span class="pun">.</span><span class="typ">Booleans</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">GuavaTester</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> main</span><span class="pun">(</span><span class="typ">String</span><span class="pln"> args</span><span class="pun">[]){</span><span class="pln">

  </span><span class="typ">GuavaTester</span><span class="pln"> tester </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">GuavaTester</span><span class="pun">();</span><span class="pln">

  tester</span><span class="pun">.</span><span class="pln">testBooleans</span><span class="pun">();</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> testBooleans</span><span class="pun">(){</span><span class="pln">

  </span><span class="kwd">boolean</span><span class="pun">[]</span><span class="pln"> booleanArray </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="kwd">true</span><span class="pun">,</span><span class="kwd">true</span><span class="pun">,</span><span class="kwd">false</span><span class="pun">,</span><span class="kwd">true</span><span class="pun">,</span><span class="kwd">true</span><span class="pun">,</span><span class="kwd">false</span><span class="pun">,</span><span class="kwd">false</span><span class="pun">};</span><span class="pln">



  </span><span class="com">//convert array of primitives to array of objects</span><span class="pln">

  </span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">Boolean</span><span class="pun">&gt;</span><span class="pln"> objectArray </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Booleans</span><span class="pun">.</span><span class="pln">asList</span><span class="pun">(</span><span class="pln">booleanArray</span><span class="pun">);</span><span class="pln">

  </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="pln">objectArray</span><span class="pun">.</span><span class="pln">toString</span><span class="pun">());</span><span class="pln">



  </span><span class="com">//convert array of objects to array of primitives</span><span class="pln">

  booleanArray </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Booleans</span><span class="pun">.</span><span class="pln">toArray</span><span class="pun">(</span><span class="pln">objectArray</span><span class="pun">);</span><span class="pln">

  </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"[ "</span><span class="pun">);</span><span class="pln">

  </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;</span><span class="pln"> booleanArray</span><span class="pun">.</span><span class="pln">length </span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++){</span><span class="pln">

     </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">booleanArray</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">" "</span><span class="pun">);</span><span class="pln">

  </span><span class="pun">}</span><span class="pln">

  </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"]"</span><span class="pun">);</span><span class="pln">

  </span><span class="com">//check if element is present in the list of primitives or not</span><span class="pln">

  </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"true is in list? "</span><span class="pun">+</span><span class="pln"> </span><span class="typ">Booleans</span><span class="pun">.</span><span class="pln">contains</span><span class="pun">(</span><span class="pln">booleanArray</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">));</span><span class="pln">



  </span><span class="com">//return the first index of element</span><span class="pln">

  </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"true position in list "</span><span class="pun">+</span><span class="pln"> </span><span class="typ">Booleans</span><span class="pun">.</span><span class="pln">indexOf</span><span class="pun">(</span><span class="pln">booleanArray</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">));</span><span class="pln">



  </span><span class="com">//Returns the count of true values   </span><span class="pln">

  </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"true occured: "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="typ">Booleans</span><span class="pun">.</span><span class="pln">countTrue</span><span class="pun">());</span><span class="pln">



  </span><span class="com">//Returns the comparisons        </span><span class="pln">

  </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"false Vs true: "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="typ">Booleans</span><span class="pun">.</span><span class="pln">compare</span><span class="pun">(</span><span class="kwd">false</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">));</span><span class="pln">

  </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"false Vs false: "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="typ">Booleans</span><span class="pun">.</span><span class="pln">compare</span><span class="pun">(</span><span class="kwd">false</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">));</span><span class="pln">

  </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"true Vs false: "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="typ">Booleans</span><span class="pun">.</span><span class="pln">compare</span><span class="pun">(</span><span class="kwd">true</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">));</span><span class="pln">

  </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"true Vs true: "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="typ">Booleans</span><span class="pun">.</span><span class="pln">compare</span><span class="pun">(</span><span class="kwd">true</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">));</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span></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:>

[true, true, false, true, true, false, false]

[ true true false true true false false ]

true is in list? true

true position in list 0

true occured: 0

false Vs true: -1

false Vs false: 0

true Vs false: 1

true Vs true: 0</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 Chars类"><i class="layui-icon"></i>
                            Guava Chars类</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:Guava数学工具"><i class="layui-icon"></i>
                            Guava数学工具</a></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>
暂无回复,说出你的观点吧
登录后即可参与回复