一. Properties (最重要,经常使用,读取配置文件) 为HashTabel的子类
Properties prop = new Properties();
prop.load(new FileInputStream(new File("AAA"))); //这里是2种加载读取文件方式
p.load(PropertiesUtil.class.getResourceAsStream("menu.txt")); (注:这里都要使用Try/Catch语句)
p.put("白菜", "35"); //这个方法只是将数据写入内存
prop.store(new FileOutputStream(new File("AAA")), null); //将内容写入文件
String psw = prop.getProperty(user); //根据键查询对应的值
Set<Object> set = prop.keySet(); //迭代法查键
Iterator it = set.iterator();
while(it.hasNext()){
Object o = it.next();
System.out.println(o);
}
Collection col = prop.values(); //迭代法查值
Iterator itt = col.iterator();
while(itt.hasNext()){
Object o = itt.next();
System.out.println(o);
}
二.LinkedList (注:在不考虑线性安全时,建议都使用ArrayList,效率更高点)
//迭代器查询链表LinkedList
LinkedList<String> list = new LinkedList<String>();
list.add("asd");
list.add("ddd");
list.add("qqq");
System.out.println(list.size());
System.out.println(list.get(2));
Iterator<String> it = list.iterator();
while(it.hasNext()){
String str = it.next();
System.out.println(str);
}
三.HashMap
HashMap<String,String> hash = new HashMap<String,String>();
hash.put("asd", "qwe");
hash.put("asb", "sss");
还可以用Get方法获得值
Set<String> set = hash.keySet();
Iterator<String> itt = set.iterator();
while(itt.hasNext()){
String ss = itt.next();
System.out.println(ss);
}
Collection col = hash.values();
Iterator it = col.iterator();
while(it.hasNext()){
Object o = it.next();
System.out.println(o);
}
//直接查值
Collection<String> coll= hash.values();
System.out.println(coll);
总结: 其他常用集合可以直接查询JDK使用,都比较简单,重点是记住他们对应的接口,就可以知道使用方法。
如下:
ArrayList/Vector >>List
HashSet/TreeSet >>Set Collection(接口) add()方法
Propeties/HashTable
TreeMap/HashMap Map(接口) put()方法