Properties Class
Import Java.io.filereader;import java.util.enumeration;import java.util.properties;/* * Properties class, key-value pairs collection, is a subclass of Hashtable * but can only store string key value pairs */public class test07{public static void Main (string[] args) throws Exception { Test1 (); Test2 (); }//Properties basic usage public static void Test1 () {//Create a Properties attribute Collection property P = new Propertie S (); Add attribute and attribute values P.setproperty ("username", "Tom"); P.setproperty ("Sex", "male"); P.setproperty ("Hobby", "Learning Java Programming"); Gets the property value based on the property name String hobby = P.getproperty ("hobby"); System.out.println ("hobby:" + hobby); Traverse Properties Collection System.out.println ("****** traversal Properties collection"); Enumeration E = P.propertynames ();//Gets an enumeration instance of the property name while (E.hasmoreelements ()) {String key = (Strin g) e.nextelement (); String value = P.getproperty (key); String value= (String) p.get (key); SYSTEM.OUT.PRINTLN (key + ":" + value); } System.out.println (P); }//View system Properties public static void Test2 () {//Get the property of the system Properties P = system.getproperties (); Traverse output system Property Information Enumeration E = P.propertynames (); while (E.hasmoreelements ()) {string key = (string) e.nextelement (); String value = P.getproperty (key); SYSTEM.OUT.PRINTLN (key + ":" + value); }}//Load property information in file public static void Test3 () throws Exception {//create file input stream FileReader reader = NE W FileReader ("Student.properties"); Create a Properties collection, save Attribute Information Property P=new properties (); Use the load () method to load the properties file P.load (reader); SYSTEM.OUT.PRINTLN (P); }}
Run as follows:
Hobby: Learning Java Programming
Traversing the Properties collection
Sex: Male
Hobby: Learning Java Programming
Username:tom
{hobby= Learning Java programming, sex= Male, Username=tom}
Java.runtime.name:Java (TM) SE Runtime Environment
Sun.boot.library.path:d:\program Files (x86) \java\jdk1.8.0_25\bin
Java.vm.version:25.25-b02
Java.vm.vendor:Oracle Corporation
java.vendor.url:http://java.oracle.com/
Path.separator:;
Java.vm.name:Java HotSpot (TM) 64-bit Server VM
File.encoding.pkg:sun.io
User.script:
User.country:CN
Sun.java.launcher:SUN_STANDARD
Sun.os.patch.level:Service Pack 1
Java.vm.specification.name:Java Virtual Machine Specification
User.dir:e:\workspace\study
Java.runtime.version:1.8.0_25-b18
Java.awt.graphicsenv:sun.awt.Win32GraphicsEnvironment
Java.endorsed.dirs:d:\program Files (x86) \java\jdk1.8.0_25\lib\endorsed
Os.arch:amd64
Java.io.tmpdir:c:\users\admini~1\appdata\local\temp\
Line.separator:
Java.vm.specification.vendor:Oracle Corporation
User.variant:
Os.name:Windows 7
Sun.jnu.encoding:GBK
Java.library.path:d:\program Files (x86) \java\jdk1.8.0_25\bin; C:\Windows\Sun\Java\bin; C:\Windows\system32; C:\Windows; C:\Windows\System32; H:\ Android Development \sdk\platform-tools;.
Java.specification.name:Java Platform API Specification
java.class.version:52.0
Sun.management.compiler:HotSpot 64-bit Tiered Compilers
os.version:6.1
User.home:c:\users\administrator
User.timezone:
Java.awt.printerjob:sun.awt.windows.WPrinterJob
File.encoding:GBK
java.specification.version:1.8
User.name:Administrator
Java.class.path:e:\workspace\study\bin; C:\Users\Administrator\Desktop\HanziToPinyinTest\libs\pinyin4j-2.5.0.jar
java.vm.specification.version:1.8
Sun.arch.data.model:64
Java.home:d:\program Files (x86) \java\jdk1.8.0_25
sun.java.command:Test07
Java.specification.vendor:Oracle Corporation
User.language:zh
Awt.toolkit:sun.awt.windows.WToolkit
java.vm.info:mixed mode
Java.version:1.8.0_25
Java.ext.dirs:d:\program Files (x86) \java\jdk1.8.0_25\lib\ext; C:\Windows\Sun\Java\lib\ext
Sun.boot.class.path:d:\program files (x86) \java\jdk1.8.0_25\lib\resources.jar;d:\program files (x86) \Java\jdk1.8.0 _25\lib\rt.jar;d:\program files (x86) \java\jdk1.8.0_25\lib\sunrsasign.jar;d:\program files (x86) \java\jdk1.8.0_25\ Lib\jsse.jar;d:\program files (x86) \java\jdk1.8.0_25\lib\jce.jar;d:\program files (x86) \java\jdk1.8.0_25\lib\ Charsets.jar;d:\program files (x86) \java\jdk1.8.0_25\lib\jfr.jar;d:\program files (x86) \java\jdk1.8.0_25\classes
Java.vendor:Oracle Corporation
File.separator:\
java.vendor.url.bug:http://bugreport.sun.com/bugreport/
Sun.cpu.endian:little
Sun.io.unicode.encoding:UnicodeLittle
Sun.desktop:windows
Sun.cpu.isalist:amd64
Collections Tool Class
Import Java.util.arraylist;import java.util.collections;import java.util.list;/* * Collections Tool class for manipulating collections * Compare arrays tool class, used to manipulate array */public class test01{public static void Main (string[] args) { //Create collection list List = NE W ArrayList (); Adds data list.add ("I") to the collection; List.add ("Love"); List.add ("You"); List.add ("Itany"); List.add ("Welcome"); List.add ("to"); List.add ("Itany"); The maximum and minimum values in the output collection are System.out.println ("Maximum:" +collections.max (list)); SYSTEM.OUT.PRINTLN ("Minimum Value:" +collections.min (list)); Sort System.out.println in ascending order ("before sorting:" +list); Collections.sort (list); System.out.println ("After sorting:" +list); Reverses the set order, that is, descending //collections.reverse (list); System.out.println ("Descending:" +list); Two-point lookup int index=collections.binarysearch (list, "to"); System.out.println ("to the position in the collection:" +index);} }
Run the following example:
Maximum Value: you
Minimum value: I
Before sorting: [I, Love, you, Itany, welcome, to, Itany]
After sorting: [I, Itany, Itany, love, to, welcome, you]
To position in the collection: 4
Just analyze the difference between collection and collections:
1, Java.util.Collection is a
Collection Interface (a top-level interface of a collection class ) 。 It provides a common interface method for basic manipulation of collection objects. The collection interface has many specific implementations in the Java class Library. The meaning of the collection interface is to provide a maximum unified operation for a variety of specific sets, and its direct inheritance interface has a list and set.Collection
├list
│├linkedlist
│├arraylist
│└vector
│└stack
└set2. Java.util.Collections is a wrapper class (Tool class/helpclass)。 It contains a variety of information about collection operations.
static Polymorphic method。 This class
cannot instantiate, just like a
a tool class, which is used to sort, search, and line Cheng the elements in the collection, serving the collection framework of Java.
Java-properties Class and Collections tool class