當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.util.HashSet.contains()用法及代碼示例


描述

這個contains(Object o)如果此集合包含指定的元素,則使用方法返回 'true'。

聲明

以下是聲明java.util.HashSet.contains()方法。

public boolean contains(Object o)

參數

o─ 這是要測試其在該集合中是否存在的元素。

返回值

如果此集合包含指定的元素,則方法調用返回 'true'。

異常

NA

示例

下麵的例子展示了 java.util.HashSet.contains() 的用法

package com.tutorialspoint;

import java.util.*;

public class HashSetDemo {
   public static void main(String args[]) {
      
      // create hash set
      HashSet <String> newset = new HashSet <String>();

      // populate hash set
      newset.add("Learning"); 
      newset.add("Easy");
      newset.add("Simply");  

      // check the existence of element
      boolean exist = newset.contains("Simply");

      System.out.println("Is the element 'Simply' exists:"+ exist);
   }    
}

讓我們編譯並運行上麵的程序,這將產生以下結果。

Is the element 'Simply' exists:true

相關用法


注:本文由純淨天空篩選整理自 java.util.HashSet.contains() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。