Java review ------------------ Chapter 7 java set,

Source: Internet
Author: User

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 );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.