Java HashMap isEmpty() Method |
(#)
Java Tutorial
Java TutorialJava IntroductionJava Development Environment SetupJava Basic SyntaxJava CommentsJava Objects and ClassesJava Basic Data TypesJava Variable TypesJava Variable Naming ConventionsJava ModifiersJava OperatorsJava Loop StructuresJava Conditional StatementsJava switch caseJava Number & Math ClassJava Character ClassJava String ClassJava StringBufferJava ArraysJava Date TimeJava Regular ExpressionsJava MethodsJava ConstructorsJava Stream, File, IOJava Scanner ClassJava Exception Handling
Java Object-Oriented
Java InheritanceJava Override/OverloadJava PolymorphismJava Abstract ClassesJava EncapsulationJava InterfacesJava EnumsJava PackagesJava Reflection
Java Advanced Tutorial
Java Data StructuresJava Collections FrameworkJava ArrayListJava LinkedListJava HashSetJava HashMapJava IteratorJava ObjectJava NIO FilesJava GenericsJava SerializationJava Network ProgrammingJava Sending EmailJava Multithreading ProgrammingJava Applet BasicsJava Documentation CommentsJava ExamplesJava 8 New FeaturesJava MySQL ConnectionJava 9 New FeaturesJava QuizJava Common Class Libraries
In-Depth Exploration
Programming Languages
Web Services
Programming
Development Tools
Web Design and Development
Scripting Languages
Web Service
Scripting
Software
Computer Science
Java HashMap isEmpty() Method
The isEmpty() method is used to check whether this HashMap is empty.
The syntax of isEmpty() is:
hashmap.isEmpty()
Note: hashmap is an object of the HashMap class.
Parameter Description:
- None
Return Value
Returns true if the HashMap contains no key-value mappings, otherwise returns false.
Example
The following example demonstrates the usage of isEmpty():
Example
import java.util.HashMap;
class Main {
public static void main(String[] args){
HashMap<Integer, String> sites = new HashMap<>();
// Check if the HashMap has any elements
boolean result = sites.isEmpty();// true
System.out.println("Is empty? "+ result);
// Add some elements to the HashMap
sites.put(1, "Google");
sites.put(2, "");
sites.put(3, "Taobao");
System.out.println("HashMap: "+ sites);
result = sites.isEmpty();// false
System.out.println("Is empty? "+ result);
}
}
Executing the above program produces the following output:
Is empty? trueHashMap: {1=Google, 2=, 3=Taobao}Is empty? false
In the above example, we created a HashMap named sites and used the isEmpty() method to check if it is empty.
YouTip