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

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

王林
王林Original
2019-11-29 11:57:585535browse

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

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;

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 ? '空' : '不为空'

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!

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