java.util.TreeSet.contains()方法实例

Sublime ww9992019com 发表于 1 周前

www.xml400.com开户请拨15974937077不规范地名整治上海闵行区FLk5VO<div style="width:100%;float:left;" class="article-content">

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

                <p>

<strong>contains(Object o) </strong>方法是用来判断当且仅当此set是否包含指定的元素,方法返回true。</p>

<h2>

声明</h2>

<p>

以下是<strong>java.util.TreeSet.contains()</strong>方法的声明。</p>

<pre class="prettyprint prettyprinted">

<span class="kwd">public</span><span class="pln"> </span><span class="kwd">boolean</span><span class="pln"> contains</span><span class="pun">(</span><span class="typ">Object</span><span class="pln"> o</span><span class="pun">)</span></pre>

<h2>

参数</h2>

<ul>

<li>

    <p>

        o-- 这是此set要被检查是否包含的对象。</p>

</li>

</ul>

<h2>

返回值</h2>

<p>

如果此set包含指定的元素,方法调用返回true。</p>

<h2>

异常</h2>

<ul>

<li>

    <p>

        ClassCastException-- 如果指定元素不能与元素相比于当前集合,这个异常被抛出。</p>

</li>

<li>

    <p>

        NullPointerException-- 如果指定的元素为null,并且此set使用自然顺序,或者其比较器不允许使用null元素,这个异常将被抛出。</p>

</li>

</ul>

<h2>

例子</h2>

<p>

下面的例子展示java.util.TreeSet.contains()方法的使用。</p>

<pre class="prettyprint prettyprinted">

<span class="kwd">package</span><span class="pln"> com</span><span class="pun">.</span><span class="pln">yiibai</span><span class="pun">;</span><span class="pln">

</span><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">Iterator</span><span class="pun">;</span><span class="pln">

</span><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">TreeSet</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">TreeSetDemo</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="pun">[]</span><span class="pln"> args</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

 </span><span class="com">// creating a TreeSet </span><span class="pln">

 </span><span class="typ">TreeSet</span><span class="pln"> </span><span class="pun">&lt;</span><span class="typ">Integer</span><span class="pun">&gt;</span><span class="pln">treeadd </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">TreeSet</span><span class="pun">&lt;</span><span class="typ">Integer</span><span class="pun">&gt;();</span><span class="pln">



 </span><span class="com">// adding in the tree set</span><span class="pln">

 treeadd</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="lit">12</span><span class="pun">);</span><span class="pln">

 treeadd</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="lit">13</span><span class="pun">);</span><span class="pln">

 treeadd</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="lit">14</span><span class="pun">);</span><span class="pln">

 treeadd</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="lit">15</span><span class="pun">);</span><span class="pln">



 </span><span class="com">// check existence of 15  </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">"Checking existence of 15 "</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">"Is 15 there in the set: "</span><span class="pun">+</span><span class="pln">treeadd</span><span class="pun">.</span><span class="pln">contains</span><span class="pun">(</span><span class="lit">15</span><span class="pun">));</span><span class="pln">

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

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

<p>

现在编译和运行上面的代码示例,将产生以下结果。</p>

<pre class="prettyprint prettyprinted">

<span class="typ">Checking</span><span class="pln"> existence of </span><span class="lit">15</span><span class="pln">

</span><span class="typ">Is</span><span class="pln"> </span><span class="lit">15</span><span class="pln"> there </span><span class="kwd">in</span><span class="pln"> the </span><span class="kwd">set</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">true</span></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="java.util.TreeSet.comparator()方法实例"><i class="layui-icon"></i>
                            java.util.TreeSet.comparator()方法实例</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:java.util.TreeSet.descendingIterator()方法实例"><i class="layui-icon"></i>
                            java.util.TreeSet.descendingIterator()方法实例</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>
暂无回复,说出你的观点吧
登录后即可参与回复