Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
AbstractCollection contains() Method in Java with Examples
Next article icon

AbstractCollection clear() Method in Java with Examples

Last Updated : 26 Nov, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The clear() method of Java AbstractCollection in Java is used to remove all of the elements from the Collection. Using the clear() method only clears all the element from the collection and does not delete the collection. In other words, it can be said that the clear() method is used to only empty an existing AbstractCollection. Syntax:
AbstractCollection.clear()
Return Value: The function does not return any value. Below programs illustrate the AbstractCollection.clear() method: Program 1: Java
// Java code to illustrate clear(Object o) // of AbstractCollelction  import java.util.*; import java.util.AbstractCollection;  public class AbstractCollectionDemo {     public static void main(String[] args)     {          // Create an empty Collection         AbstractCollection<Object>             abs = new ArrayList<Object>();          // Use add() method to add         // elements in the collection         abs.add("Welcome");         abs.add("To");         abs.add("Geeks");         abs.add("4");         abs.add("Geeks");          // Displaying the Collection         System.out.println("AbstractCollection: "                            + abs);          // Clearing the Collection         abs.clear();          // Displaying the Collection         System.out.println("AbstractCollection "                            + "after using clear: "                            + abs);     } } 
Output:
  AbstractCollection: [Welcome, To, Geeks, 4, Geeks]  AbstractCollection after using clear: []  
Program 2: Java
// Java code to illustrate clear(Object o) // of AbstractCollelction  import java.util.*; import java.util.AbstractCollection;  public class AbstractCollectionDemo {     public static void main(String[] args)     {          // Create an empty collection         AbstractCollection<Object>             abs = new LinkedList<Object>();          // Use add() method to add         // elements in the collection         abs.add(15);         abs.add(20);         abs.add(25);         abs.add(30);         abs.add(35);          // Displaying the Collection         System.out.println("AbstractCollection: "                            + abs);          // Clearing the Collection         abs.clear();          // Displaying the Collection         System.out.println("AbstractCollection "                            + "after using clear: "                            + abs);     } } 
Output:
  AbstractCollection: [15, 20, 25, 30, 35]  AbstractCollection after using clear: []  

Next Article
AbstractCollection contains() Method in Java with Examples

C

chinmoy lenka
Improve
Article Tags :
  • Misc
  • Java
  • Java-Collections
  • Java - util package
  • Java-Functions
  • Java-AbstractCollection
Practice Tags :
  • Java
  • Java-Collections
  • Misc

Similar Reads

    AbstractCollection in Java with Examples
    The AbstractCollection class in Java is a part of the Java Collection Framework and implements the Collection interface. It is used to implement an unmodifiable collection, for which one needs to only extend this AbstractCollection Class and implement only the iterator and the size methods. Class Hi
    3 min read
    AbstractCollection add() Method in Java with Examples
    The add() method in Java AbstractCollection is used to add a specific element into a Collection. This method will add the element only if the specified element is not present in the Collection else the function will return False if the element is already present in the Collection. Syntax: AbstractCo
    2 min read
    AbstractCollection addAll() Method in Java
    The addAll() method of Java AbstractCollection is used to append all elements from a given collection to the current collection. If the collection being appended is a TreeSet, the elements are stored in sorted order, as TreeSet maintains a natural ordering. It is important to note that AbstractColle
    2 min read
    AbstractCollection clear() Method in Java with Examples
    The clear() method of Java AbstractCollection in Java is used to remove all of the elements from the Collection. Using the clear() method only clears all the element from the collection and does not delete the collection. In other words, it can be said that the clear() method is used to only empty a
    2 min read
    AbstractCollection contains() Method in Java with Examples
    The contains() method of Java AbstractCollection is used to check whether an element is present in a Collection or not. It takes the element as a parameter and returns True if the element is present in the collection.Syntax: AbstractCollection.contains(Object element) Parameters: The parameter eleme
    2 min read
    AbstractCollection containsAll() Method in Java
    The containsAll() method of Java AbstractCollection is used to check whether a collection contains all elements of another collection.Example 1: This program checks if two collections have the same elements using AbstractCollection.containsAll() method.Java// Java Program to illustrate containsAll()
    2 min read
    AbstractCollection isEmpty() Method in Java with Examples
    The isEmpty() method of Java AbstractCollection is used to check and verify if a Collection is empty or not. It returns True if the Collection is empty else it returns False. Syntax: AbstractCollection.isEmpty() Parameters: The method does not take any parameter. Return Value: The function returns T
    2 min read
    AbsractCollection iterator() Method in Java with Examples
    The iterator() method of Java AbstractCollection is used to return an iterator of the same elements as that of the Collection. The elements are returned in random order from what was present in the Collection. Syntax: Iterator iterate_value = AbstractCollection.iterator(); Parameters: The function d
    2 min read
    AbstractCollection remove() Method in Java with Examples
    The remove(Object O) method of Java AbstractCollection is to remove a particular element from a Collection. Syntax: AbstractCollection.remove(Object O) Parameters: The parameter O is of the type of Collection and specifies the element to be removed from the collection. Return Value: This method retu
    2 min read
    AbstractCollection size() Method in Java with Examples
    The size() method of Java AbstractCollection is used to get the size of the Collection or the number of elements present in the Collection.Syntax: AbstractCollection.size() Parameters: The method does not take any parameter.Return Value: The method returns the size or the number of elements present
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences