In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to use the Collection interface in Java", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the Collection interface in Java" article.
Method definition
Let's think about it first. if the company wants us to encapsulate some utility classes that operate on arrays or linked lists, what functions do we need to encapsulate? You might as well count its size, add, delete, change, check, empty or check whether it contains a piece of data, and so on. The collection interface is to extract these common operations to make them more comprehensive and generic, so now let's take a look at the methods of its source code.
/ / returns the length of the collection, or Integer.MAX_VALUEint size () if the length is greater than Integer.MAX_VALUE; / / returns trueboolean isEmpty () if the total number of elements in the collection is 0; / / determines whether the collection contains the specified elements based on the equals () method boolean contains (Object o); / / returns an array Object [] toArray () that contains all the elements in the set / / similar to the previous one, except that the type conversion T [] toArray (T [] a) has been added; / / an element is added to the collection, which returns true if it is joined successfully, or falseboolean add (E e) if the join fails, or if the collection itself no longer contains the same element; / / removes a single instance of the specified element from the collection boolean remove (Object o) / / return trueboolean containsAll (Collection c) if the collection contains all the elements in the specified collection; / / add all elements in the specified collection to the collection, but in the meantime, if the specified collection changes, unexpected things may occur boolean addAll (Collection c); / / keep only the elements contained in the specified collection boolean retainAll (Collection c); / / empty the collection void clear () / / this method is abstracted to ensure that all subclasses override this method to ensure the correct behavior of equals boolean equals (Object o); / / the above int hashCode (); / / this method provides a default implementation in JDK1.8 and deletes the eligible element default boolean removeIf (Predicate it) {/ / record the current size int I = r.length in the form of Iterator While (it.hasNext ()) {int cap = r.roomth; / / r is not long enough. Continue to allocate if (I = = cap) {/ / expansion mode is cap+cap/2+1, that is, 1.5x expansion int newCap = cap+ (cap > > 1) + 1 / / exceeded the maximum capacity, MAX_ARRAY_SIZE=Integer.MAX_VALUE-8 if (newCap-MAX_ARRAY_SIZE > 0) / / reset the value of cap newCap = hugeCapacity (cap + 1); / / expand the capacity of r to r = Arrays.copyOf (r, newCap) } / / assignment, enter the next cycle r [ionization +] = (T) it.next ();} / / since the previous capacity expansion is 1.5 times, and then set it to the same return as r (I = = r.length)? R: Arrays.copyOf (r, I);} private static int hugeCapacity (int minCapacity) {if (minCapacity)
< 0) // 超过了最大正整数,也就是负数 throw new OutOfMemoryError ("Required array size too large"); return (minCapacity >MAX_ARRAY_SIZE)? Integer.MAX_VALUE: MAX_ARRAY_SIZE;} / / is similar to the toArray () method, so I won't repeat it any more. You can check the source code public T [] toArray (T [] a) {/ /...}
In addition to that, let's look at how AbstractCollection implements the toString method.
It is accomplished by stitching the toString of each element through StringBuilder, which is not very complicated.
Let's look at the source code.
Public String toString () {Iterator it = iterator (); if (! It.hasNext () return "[]"; StringBuilder sb = new StringBuilder (); sb.append ('['); for (;) {E = it.next (); sb.append (e = = this? "(this Collection)": e); if (! It.hasNext () return sb.append (']'). ToString (); sb.append (','). Append ('');}} above is about "how to use Collection interface in Java". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.