Java's collection class is a particularly useful tool class. Java's collection can be roughly seven systems: Set, list, and map. Set represents a disordered and non-repeated set (for example: the Set set is similar to a jar. when an object is added to the Set set, the Set set cannot remember the order of this element, so the elements in it cannot be repeated ); list indicates an ordered and repeated set (for example, a list set is very similar to an array. It can remember the order of each element added, but the list length is variable ); map represents a set with ing relationships (for example, it is also like a jar, but each element in it is composed of two values ). After jdki.5, Java has added a set of Queue Systems, representing the implementation of a set of queues.
A Java set is like a container. We can "throw" multiple objects (actually object references, but usually called objects) into the container. Before jdki.5, the Java set will lose the Data Types of all objects in the container, and treat all objects as objects. After adding generics from jdki.5, the Java Collection can remember the data types of objects in the container, so that you can write more concise and robust code.
Java's collection classes are derived from two interfaces: Collection and map. coflection and map are the root interfaces of the Java Collection framework. These two interfaces contain some subinterfaces or implementation classes. Figure 7.1 shows the inheritance tree of the collection interface, sub-interface and its implementation class, and Figure 7.2 shows the inheritance tree of the map system.
Are we all proficient in seeing so many interface objects? Of course you don't need it anymore. Some interfaces are left behind by history and are not applicable to current programming. Others are only applicable to environments with special requirements, if you encounter this problem, you can easily check the document. We can see that the Common Implementation classes of the Set, list, and map APIs are hashset, arraylist, and hashap (covered by the gray area ). Next, we will focus on some set objects that are frequently used in programming.
1. Collection and iterator Interfaces
The collection interface is the parent interface of the list, set, and queue interfaces. The methods defined in this interface can be used to operate both the Set set and the list and queue sets. The collection interface defines the following methods to operate collection elements (these methods are all from the javaapi documentation. For more information, see the source documentation ):
Blean add (Object O): This method is used to add an element to the set. Returns true if the set object is changed.
Boolean addall (collection C): This method adds all elements in set C to the specified set. Returns true if the set object is changed.
Void dear (): clears all elements in the set and changes the length of the set to 0.
Boolean contains (Object O): returns whether the set contains the specified element.
Boolean containsall (collection C): returns whether the set contains all elements in set C.
Boolean isempty (): whether the returned set is null. Returns true if the set length is 0; otherwise, returns felse.
Iterator (): returns an iterator object used to traverse elements in the set.
"Boolean remove (objeot O): Delete the specified Element O in the set. When the set contains one or more elements O, these elements are deleted and true is returned.
Boolean removeall (collection C): removes all elements in set C from the set (equivalent to using the set that calls this method to subtract the set C ), if one or more elements are deleted. this method returns true.
Boolean retainall (coiiection C): Removes elements not contained in set C from the set (equivalent to getting the set that calls this method into the intersection of the set and set C ), if this operation changes the set of calls to this method, this method returns true.
Int size (): This method returns the number of elements in the set.
Object [] toarray (): This method converts a set into an array, and all set elements are converted into corresponding array elements.
The lterator interface hides the underlying details of various collection implementation classes and provides an application with a unified programming interface for Traversing collection elements. The iterator interface defines the following three methods:
"Boolean hasnext (): returns true if the aggregated element to be iterated has not been traversed.
Object next (): returns the next element in the set.
Void remove (): deletes the elements returned by the last next method in the set.
Note: When iterator is used to iteratively Access Collection set elements, the elements in the collection set cannot be changed.
Only the collection elements returned by the last next method can be deleted through the Remove Method of iterator. Otherwise
Java · util. concurrentmodificationexception. You can also use foreach to access the elements in the Set, which is not described here.
2. Set Interface
Next Article → Java set Introduction 2