Home > Java > JavaBase > body text

Summary of methods to determine whether an object is empty in Java

王林
Release: 2019-11-29 11:57:58
Original
5603 people have browsed it

Summary of methods to determine whether an object is empty in Java

We want to determine whether the object is empty. It is not possible to determine whether the object is empty like the basic type, ==={}? This is wrong because it only compares whether the reference addresses are the same, so the following method can be used to judge.

1. Traverse the object according to for...in, return true if it exists, otherwise return false

for ( let i in obj) {
return true;
}
return false
Copy after login

Online video learning sharing: java online video

2. Use the JSON.stringify() method that comes with JSON to judge

The general idea is to convert it into a string '{}' for judgment.

if (JSON.stringify(obj) === '{}') {
return true;
}
return false;
Copy after login

3. Use Object.keys() in ES6 to make judgments (recommended)

The Object.keys() method will return an enumerable property consisting of a given object. array. If our object is empty, it will return an empty array.

Object.keys(obj).length === 0 ? '空' : '不为空'
Copy after login

More related articles and tutorials are recommended: javaQuick Start

The above is the detailed content of Summary of methods to determine whether an object is empty in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!