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


Java java.util.TreeSet.isEmpty()用法及代碼示例


描述

這個isEmpty()如果此集合不包含任何元素,則使用方法返回 true。

聲明

以下是聲明java.util.TreeSet.isEmpty()方法。

public boolean isEmpty()

參數

NA

返回值

如果此集合不包含任何元素,則方法調用返回 true。

異常

NA

示例

下麵的例子展示了 java.util.TreeSet.isEmpty() 方法的用法。

package com.tutorialspoint;

import java.util.TreeSet;

public class TreeSetDemo {
   public static void main(String[] args) {

      // creating a TreeSet 
      TreeSet <Integer>treeadd = new TreeSet<Integer>();

      // checking tree set
      System.out.println("Is the tree set empty:"+treeadd.isEmpty()); 

      // adding elements in the tree set
      System.out.println("Adding elements in the tree set"); 
      treeadd.add(12);
      treeadd.add(11);
      treeadd.add(16);
      treeadd.add(15);

      // checking tree set again
      System.out.println("Is the tree set empty:"+treeadd.isEmpty());  
   }     
}

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

Is the tree set empty:true
Adding elements in the tree set
Is the tree set empty:false

相關用法


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