asked 179k views
4 votes
For removing all the elements from a collection, What is the correct method?

1) clear()
2) removeAll()
3) deleteAll()
4) empty()

asked
User Lolo Me
by
8.7k points

1 Answer

3 votes

Final answer:

To remove all elements from a collection, the correct method is typically clear(). The removeAll() method is less efficient and requires a specific collection to remove, while deleteAll() and empty() are not standard methods.

Step-by-step explanation:

To remove all elements from a collection in most programming languages and frameworks, the correct method is usually clear(). This method empties the collection of all its elements, resulting in a collection with a size of zero. For instance, in Java, ArrayList and HashSet both have a clear() method that can be called to remove all elements. The removeAll() method is also available in Java and requires a specific collection of items to remove, typically leaving the collection empty if you pass it the collection itself. However, this is less efficient than clear(). The options deleteAll() and empty() are not standard methods for removing all elements from a collection in standard programming libraries.

answered
User Aswin
by
8.5k points