标签:java collections properities
import java.io.FileReader; import java.util.Enumeration; import java.util.Properties; /* * Properties类,键值对集合,是Hashtable的子类 * 但只能存储字符串键值对 */ public class Test07 { public static void main(String[] args) throws Exception { test1(); test2(); } // Properties基本用法 public static void test1() { // 创建一个Properties属性集合 Properties p = new Properties(); // 添加属性和属性值 p.setProperty("username", "tom"); p.setProperty("sex", "男"); p.setProperty("hobby", "学习Java编程"); // 根据属性名获取属性值 String hobby = p.getProperty("hobby"); System.out.println("hobby:" + hobby); // 遍历Properties集合 System.out.println("******遍历Properties集合"); Enumeration e = p.propertyNames();// 获取属性名的枚举实例 while (e.hasMoreElements()) { String key = (String) e.nextElement(); String value = p.getProperty(key); // String value=(String) p.get(key); System.out.println(key + ":" + value); } System.out.println(p); } // 查看系统属性 public static void test2() { // 获取系统的属性 Properties p = System.getProperties(); // 遍历输出系统属性信息 Enumeration e = p.propertyNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); String value = p.getProperty(key); System.out.println(key + ":" + value); } } // 加载文件中的属性信息 public static void test3() throws Exception { //创建文件输入流 FileReader reader = new FileReader("student.properties"); //创建一个Properties集合,保存属性信息 Properties p=new Properties(); //使用load()方法加载属性文件 p.load(reader); System.out.println(p); } }
运行如下:
hobby:学习Java编程
******遍历Properties集合
sex:男
hobby:学习Java编程
username:tom
{hobby=学习Java编程, sex=男, 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:\安卓开发\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
import java.util.ArrayList; import java.util.Collections; import java.util.List; /* * Collections工具类,用来操作集合 * 对比Arrays工具类,用来操作数组 */ public class Test01 { public static void main(String[] args) { // 创建集合 List list = new ArrayList(); // 向集合中添加数据 list.add("i"); list.add("love"); list.add("you"); list.add("itany"); list.add("welcome"); list.add("to"); list.add("itany"); //输出集合中的最大值和最小值 System.out.println("最大值:"+Collections.max(list)); System.out.println("最小值:"+Collections.min(list)); //按升序排列 System.out.println("排序前:"+list); Collections.sort(list); System.out.println("排序后:"+list); //把集合顺序反转,即降序 //Collections.reverse(list); //System.out.println("降序:"+list); //二分查找 int index=Collections.binarySearch(list, "to"); System.out.println("to在集合中的位置:"+index); } }
运行实例如下:
最大值:you
最小值:i
排序前:[i, love, you, itany, welcome, to, itany]
排序后:[i, itany, itany, love, to, welcome, you]
to在集合中的位置:4
正好分析一下Collection和Collections的区别:
JAVA-Properties类和Collections工具类
标签:java collections properities
原文地址:http://blog.csdn.net/yayun0516/article/details/46048475