Java 集合框架(List、Set、Map、Iterator、Stack、Properties)
生活随笔
收集整理的这篇文章主要介绍了
Java 集合框架(List、Set、Map、Iterator、Stack、Properties)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
文章目录
- 1. ArrayList
- 2. LinkedList
- 3. HashSet
- 4. TreeSet
- 5. Iterator、ListIterator
- 6. HashMap
- 7. TreeMap
- 8. Stack
- 9. Properties 类
- 读写简单 数据库
相关文献:https://www.runoob.com/java/java-collections.html
1. ArrayList
- 类似动态数组
2. LinkedList
- 链表
3. HashSet
- 哈希集合,无序
4. TreeSet
- 树set,有序
5. Iterator、ListIterator
- ListIterator 可以修改元素,可以双向遍历,是 Iterator 的扩展
6. HashMap
// HashMapHashMap hm = new HashMap();hm.put("Michael", 18);hm.put("Ming", 19);Set set = hm.entrySet();Iterator i = set.iterator();while(i.hasNext()){Map.Entry me = (Map.Entry) i.next();System.out.print(me.getKey() + ":");System.out.println(me.getValue());}int age = ((Integer)hm.get("Michael")).intValue();hm.put("Michael", age+2);i = set.iterator();while(i.hasNext()){Map.Entry me = (Map.Entry) i.next();System.out.print(me.getKey() + ":");System.out.println(me.getValue());}输出:
Ming:19 Michael:18 Ming:19 Michael:207. TreeMap
// TreeMapTreeMap tm = new TreeMap();tm.put(18, "Michael");tm.put(19, "Ming");tm.put(0, "Java");// valuesCollection col = tm.values();Iterator it1 = col.iterator();while(it1.hasNext()){System.out.println(it1.next());}// keySetCollection col1 = tm.keySet();Iterator it2 = col1.iterator();while(it2.hasNext()){System.out.println(it2.next());}// entrySet, K V 对Collection col2 = tm.entrySet();Iterator it3 = col2.iterator();while(it3.hasNext()){System.out.println(it3.next());}输出:
Java Michael Ming 0 18 19 0=Java 18=Michael 19=Ming8. Stack
- Stack 继承于 Vector,Vector 与 ArrayList 类似
输出:
[] 入栈:2 [2] 入栈:4 [2, 4] 入栈:1 [2, 4, 1] 出栈:1 [2, 4] 出栈:4 [2] 出栈:2 [] 异常:java.util.EmptyStackException9. Properties 类
// Properties : k v 都是字符串的 HashtableProperties capitals = new Properties();capitals.put("中国", "北京");capitals.put("日本", "东京");// capitals.put("美国", "华盛顿");Set states = capitals.keySet();String country;Iterator it4 = states.iterator();while(it4.hasNext()){country = (String) it4.next();System.out.println(country + " : " + capitals.getProperty(country));}String str = capitals.getProperty("美国", "not found");//若没有key,返回默认值 not foundSystem.out.println(str);输出:
中国 : 北京 日本 : 东京 not found读写简单 数据库
- 特别适合做简单数据库
输出:
这是第1次使用本程序!总结
以上是生活随笔为你收集整理的Java 集合框架(List、Set、Map、Iterator、Stack、Properties)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: LeetCode 2195. 向数组中追
- 下一篇: Java 变量、数据类型