1.1 Set.add Method--Add an object to the Set collection
public static void Main (string[] args) {
Set set = new HashSet (); Defining Set Collection Objects
Set.add (New Date ()); To add a Date object to the collection
Set.add ("Apple"); To add an object to the collection
Set.add ("book");
Set.add ("Apple");
Set.add (New Integer (3));
int size = Set.size (); Gets the size of the collection
System.out.println ("The size of the set set is:" + size); Output the collection size
}
Running this example, because the set collection does not allow duplicate elements, the second time adding an Apple object to the collection does not add success, and the output set has a length of 4.
1.2 Set.contains Method--Determines whether the set collection contains the specified object
public static void Main (string[] args) {
Set set = new HashSet ();
Set.add (New Date ());
Set.add ("Apple");
Set.add (New Socket ());
Boolean contains = Set.contains ("Apple");
if (contains) {
System.out.println ("Set set contains string Apple");
} else {
System.out.println ("Set collection does not contain string Apple");
}
}
1.3 Set.isempty Method--determine if the Set collection object is empty
public static void Main (string[] args) throws Interruptedexception {
SYSTEM.OUT.PRINTLN ("Create Set Collection Object"); Output hint information
Set set = new HashSet (); Creating a Set Collection Object
System.out.println ("The IsEmpty method return value for the set set is:" +set.isempty ()); Determines whether the collection is empty
System.out.println ("Add content to set set");
Set.add ("Apple"); To add an object to the collection
Set.add ("Well-equipped computer");
Set.add ("book");
System.out.println ("The IsEmpty method return value for the set set is:" +set.isempty ()); Determines whether the collection is empty
}
1.4 Set.remove Method--Removes the specified object from the set set
public static void Main (string[] args) {
Set set = new HashSet (); Defining Set Objects
Set.add ("Apple"); To add an object to the collection
Set.add ("Well-equipped computer");
Set.add ("book");
SYSTEM.OUT.PRINTLN ("Set size is:" + set.size ()); Output Collection Size
Set.remove ("book"); Remove a collection element
System.out.println ("removed from set set \" Book\ "");
SYSTEM.OUT.PRINTLN ("Set size is:" + set.size ());
}
1.5 Set.toarray Method--Create an array with all the objects in the Set collection
public static void Main (string[] args) {
Set set = new HashSet (); Defining Set Collection Objects
Set.add ("Apple"); To add an object to the collection
Set.add ("Computer");
Set.add ("book");
Set.add (New Date ());
object[] ToArray = Set.toarray (); Gets the array form of the collection
System.out.println ("The length of the array is:" +toarray.length); Output array length
}
The run result is "the length of the array is: 4".
Stores all the contents of the set collection using the specified array.
-
public static void Main (string[] args) {
set s ET = new HashSet (); //define Set collection
set.add ("Apple"); //Add Object
set.add ("Computer") to the collection;
set.add ("book");
set.add ("String is also an object, not a basic data type");
string[] Strarray = new string[6]; //defines a string array of length 6
String[] ToArray = (string[]) Set.toarray (strarray); //Convert the collection to a string array as
system.out.println (" The length of the array is: "+ toarray.length); //output array length
for (string string:toarray) { //looping through string array
system.out.println (String); //Output string array contents
}
}
-
Set Collection class