search
HomeJavaJavaBaseJava determines whether a collection object is empty

Java determines whether a collection object is empty

Nov 21, 2019 am 09:38 AM
javaIs emptyjudgment

Java determines whether a collection object is empty

The List.isEmpty() method in Java collection class is used to determine whether the collection object is empty. If it is empty, it returns true, otherwise it returns false.

Syntax:

isEmpty()

Application:

This example uses the implementation class ArrayList of the List interface to initialize a list object list, add 4 elements to the list, and then call The isEmpty method determines whether the list object is empty and outputs the determination result, then executes the clear method to remove all elements from the list, and finally executes the isEmpty method again to determine whether the list object is empty and outputs the determination result.

The code is as follows:

public static void main(String[] args){
    List<String>list = new ArrayList<String>();
    list.add("保护环境");  //向列表中添加数据
    list.add("从我做起");  //向列表中添加数据
    list.add("爱护地球");  //向列表中添加数据
    list.add("从我做起");  //向列表中添加数据
    boolean empty = list.isEmpty();
    if(empty){
    System.out.println("该列表为空");
  }else{
    System.out.println("该列表不为空");
  }
    list.clear();  //从列表中移除所有元素
    System.out.println("执行clear方法后");
    empty = list.isEmpty();
    if(empty){
    System.out.println("该列表为空");
  }else{
    System.out.println("该列表不为空");
  }
}

The running results are as follows:

该列表不为空
执行clear方法后
该列表为空

Recommended tutorial: java quick start

The above is the detailed content of Java determines whether a collection object is empty. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)