Why use a set?
Object-oriented language (OSS) Stores objects to facilitate operations on multiple objects. A set of objects is the most common method for storing objects.
Collection class features:
A set is only used to store objects. The length of a set is variable. A set can store different types of objects. The composition and classification of the Collection framework are as follows:
The following are some of the most commonly used features:
List: The most common interface. This interface can be used to expand the Collection interface in large quantities and the content in it can be repeated.
Set: the Collection interface is not extended, and repeated content cannot be stored in it.
Map: Map is the largest interface for storing a pair of values, and elements in the interface appear in pairs, key-> value
Iterator: the output interface of the set. It is used to output the content in the set and can only perform one-way output from front to back.
Two interfaces of the Collection class:
Java Collection classes are derived from two interfaces;
Collection:
Set: duplicate objects cannot be stored.
List: stores duplicate objects in an ordered manner.
Queue: Queue
SortedSet: sorts the set data.
Map:
SortedMap: sorts the set data.
Import java. util. arrayList; import java. util. collection; import java. util. iterator; public class CollectionDemo {public static void main (String [] args) {Collection c = new ArrayList (); // add to add element c. add ("A"); c. add ("well"); c. add (true); c. add (1); System. out. println (c); Collection c2 = new ArrayList (); // addAll add all elements in a set to c2.addAll (c); System. out. println (c2); // remove the specified Element c2.remove ("A"); System. out. println (c2); // iteration of the Set element for (Iterator it = c. iterator (); it. hasNext ();) {System. out. println (it. next ());}}}