Java基础之Properties类、Conllections集合工具类

二次信任 提交于 2019-11-27 00:09:00

Properties时Hashtable的子类,主要进行属性的操作,属性的最大特点是利用字符串设置key与value。

public class Properties extends Hashtable<Object,Object>在使用Properties类时,不需要设置泛型
public Object setProperty(String key, String value)
public String getProperty(String key)//如果key不存在,返回null
public String getProperty(String key, String defaultValue)//如果key不存在,返回默认值
 1  Properties properties = new Properties();
 2         properties.setProperty("BJ","北京");
 3         properties.setProperty("SH","上海");
 4         properties.setProperty("SZ","深圳");
 5         try {
 6             //使用Properties类,将设置的信息写到指定磁盘文件中
 7             properties.store(new FileOutputStream(new File("E:"+File.separator+"area.properties")),"zhushi");
 8         } catch (IOException e) {
 9             e.printStackTrace();
10         }
11 
12         //通过文件流读取属性内容
13         try {
14             properties.load(new FileInputStream(new File("E:"+File.separator+"area.properties")));
15             System.out.println(properties.getProperty("BJ"));
16         } catch (IOException e) {
17             e.printStackTrace();
18         }

Collections集合工具类

@SafeVarargs
public static <T> boolean addAll(Collection<? super T> c,
                                              T... elements)//添加数据到集合中
public static <T extends Comparable<? super T>> void sort(List<T> list)//对集合中欧冠元素排序
public static void reverse(List<?> list)//反转集合中的元素
解释:Collection与Collections的区别?  》Collection是集合操作的接口  》Collections是集合操作的工具类,可以进行List、Map、Set集合的操作
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!