Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common methods of Collection collection in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what are the common methods of Collection collection in Java, which are concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Boolean add (E e)

Add elements to the Collection collection.

2. Boolean addAll (Collection c)

Check whether the Collection collection contains all the objects of c, and return true if all objects are included, false otherwise.

6. Boolean equals (Object o)

To compare whether this collection collection is equal to the specified object is to compare whether the elements inside are equal, not whether the address is equal.

In the Object class, the bottom layer of the equals () method is "= =", that is, comparing whether the address is equal, but after inheritance, most classes override the method.

Because Collection is an interface, you cannot new objects directly, so you can only new one of its subclasses.

The following is the equals () method of the Object class, with the bottom layer using "= =".

Let's take a look at Vector, an implementation class of Collection.

Vector inherits AbstractList.

So its equals method calls the method of the parent class

Let's go in and have a look. It is really an overridden equals method

7. Int hashCode ()

Returns the hash code value of this Collection collection.

8. Boolean isEmpty ()

Check whether the Collection collection contains elements, and return true if it does not, false otherwise.

The difference between this method and the contains method is that this method determines whether the collection contains elements, that is, it returns true if there is no element, and false if there is one or more.

The method of contains is to determine whether the collection contains a specific element, return true if it contains a specific element, and false if it does not contain a specific element.

9. Iterator iterator ()

Returns the iterator that iterates over the elements of this collection collection

Note: the collection cannot be modified by adding or deleting in the collection during the iteration, otherwise an error will be reported.

If you want to manipulate the collection during the iteration, you can only use the methods in the iterator class.

10. Boolean remove (Object o)

Removes the specified element from the collection collection. If it exists in the collection and the deletion succeeds, it returns true, otherwise it returns false.

11. Boolean removeAll (Collection c)

Remove all elements in the c collection from the collection

12. Boolean retainAll (Collection c)

Only all elements in the c collection are retained in the collection

13. Int size ()

Returns the number of elements in the collection

14. Object [] toArray ()

Returns an array containing all the elements in this collection collection

What are the common methods of Collection collection in Java? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report