Java review ------------------ Chapter 7 java set,
Chapter 2 java set
Set list map
Set: unordered, not repeated;
List set: ordered, repeatable;
Map set: Key-value pair;
7.2 collection and iterator Interfaces
List <String> infoList = new ArrayList <String> ();
InfoList. add (null );
System. out. println (infoList. size (); // 1
System. out. println (infoList. toArray (); // [Ljava. lang. Object; @ 1df0a2a0
System. out. println (infoList. isEmpty (); // false
System. out. println (infoList); // [null]
7.2.1 use the itrator interface to traverse collection Elements
List <String> infoList = new ArrayList <String> ();
InfoList. add ("xiaoxiao ");
InfoList. add ("xiaoming ");
InfoList. add ("");
If (infoList! = Null &&! InfoList. isEmpty ()){
Iterator iterator = infoList. iterator ();
System. out. println (infoList. size (); // 3
While (iterator. hasNext ()){
// System. out. println (iterator. next (); // xiaoxiao xiaoming
String a = (String) iterator. next ();
If (a. equals ("xiaoxiao ")){
Iterator. remove ();
}
}
System. out. println (infoList); // [xiaoming,]
}
Determine the number of times an element appears in a collection:
System. out. print (Collections. frequency (infoList, "xiaoxiao "));
7.8.3 Synchronization Control
List <String> list = Collections. synchronizedList (new ArrayList <String> (); // creates a thread-safe set;
7.8.4 set an unchangeable set
// Set an empty unchangeable List object
List unmodifiableList = Collections. emptyList ();
UnmodifiableList. add ("qq"); // an error is returned.
// Create a set object with only one element that cannot be changed
Set unmodifiableSet = Collections. singleton ("da ");
// Set a common map object
Map map = new HashMap ();
Map. put ("qqq", 1 );
Map. put ("qq", 2 );
// Convert a map to an immutable map
Map unmodifiableMap = Collections. unmodifiableMap (map );