Properties类和Collections工具类

Properties类import java.io.FileNotFoundException;import java.io.FileReader;import java.util.Enumeration;import java.util.Properties;/* * Properties类,键值对集合,是Hashtable的子类 * 但只能存储字符串键值对 */public class Test07{(String[] args) throws Exception{test3();}(){// 创建一个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);}(){// 获取系统的属性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);}}() throws Exception{//创建文件输入流FileReader reader = new FileReader(“student.properties”);//创建一个Properties集合,保存属性信息Properties p=new Properties();//使用load()方法加载属性文件p.load(reader);System.out.println(p);}}Collections工具类import javaimport javaimport java/* * 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);}}

与一个赏心悦目的人错肩,真真实实的活着,也就够了。

Properties类和Collections工具类

相关文章:

你感兴趣的文章:

标签云: