Java Notes-11

Source: Internet
Author: User

summary:synchronized and unsynchronized collections, Properties
-in Many applications, there is no need for synchronization, so the collections API doesnot provide it by default. Instead, you can create a synchronized version of any collection

Using the following methods of the collections class:

public static Collection synchronizedcollection (Collection c) public static Set Synchronizedset (Set s) public static List s Ynchronizedlist (list list) public static Map Synchronizedmap (map m) public static SortedSet Synchronizedsortedset ( SortedSet s) public static SortedMap Synchronizedsortedmap (SortedMap m)
-for example, the following shows how to create a threadsafe List

List List = new ArrayList (); List synclist = collections.synchronizedlist (list);
-this is important, so remember this! Although synchronized collections is Threadsafe, the Iterator s returned from them is not.

Synchronized (synclist) {Iterator Iterator = Synclist.iterator ();//do stuff with the Iterator here}
-the Java.util.concurrent.ConcurrentHashMap class is part of the Concurrency Utilities package and provides a Map that P Erforms well under multithreaded access.

-you can use the collections class to create read-only versions of any collection

public static Collection unmodifiablecollection (Collection c) public static Set Unmodifiableset (Set s) public static List U Nmodifiablelist (list list) public static Map Unmodifiablemap (map m) public static SortedSet Unmodifiablesortedset ( SortedSet s) public static SortedMap Unmodifiablesortedmap (SortedMap m)

-the Java.util.Properties class is a specialized hash table for strings. Properties is generally used to hold textual configuration data.

-any string values can stored as key/value pairs in a Properties table. However, the Convention is-use-a dot-separated naming hierarchy to-group property names into
Logical structures.

Properties Props = new properties ();p rops.setproperty ("Myapp.xsize", "");p rops.setproperty ("Myapp.ysize", "79");

String xsize = Props.getproperty ("Myapp.xsize");

You can save a Properties table to an outputstream using the Save () method. The property information was output in a flat ASCII format.

Props.save (System.out, "Application Parameters");

-the load () method reads the previously saved contents of a Properties object from an InputStream
FileInputStream fin, ..... Properties Props = new properties () Props.load (Fin);

-the Properties class also contains Storetoxml () and LoadFromXML () methods. These operate just like the Save () and load () methods but write a XML file like the following

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Properties SYSTEM "Http://java.sun.com/dtd/properties.dtd" ><properties><comment>my Properties</comment><entry key= "myapp.ysize" >79</entry><entry key= "MyApp.xsize" >52</ Entry></properties>

-the Java.lang.System class provides access to basic System environment information through the static System.getpropert IES () method. This method returns a properties table, contains system Properties.



Next:the Preference API

Java Notes-11

Related Article

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.