前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 集合框架(List、Set、Map、Iterator、Stack、Properties)

Java 集合框架(List、Set、Map、Iterator、Stack、Properties)

作者头像
Michael阿明
发布2021-09-06 10:06:15
2820
发布2021-09-06 10:06:15
举报
文章被收录于专栏:Michael阿明学习之路

文章目录

相关文献:https://www.runoob.com/java/java-collections.html

1. ArrayList

  • 类似动态数组
代码语言:javascript
复制
		ArrayList al = new ArrayList();
        System.out.println("al 的初始大小:" + al.size());
        // al 的初始大小:0
        al.add("C");
        al.add("A");
        al.add("E");
        al.add("B");
        al.add("D");
        al.add("F");

        al.add(1,"A2");
        System.out.println("al size: " + al.size());
        // al size: 7
        System.out.println("al 的内容:" + al);
        // al 的内容:[C, A2, A, E, B, D, F]

        al.remove("F");
        al.remove(2);
        System.out.println("al size: " + al.size());
        // al size: 5
        System.out.println("al 的内容:" + al);
        // al 的内容:[C, A2, E, B, D]

        ArrayList al1 = new ArrayList();
        al1.add(1);
        al1.add(2);
        al1.add(3);
        al1.add(4);
        System.out.println("al1 的内容:" + al1);
        // al1 的内容:[1, 2, 3, 4]
        Object o[] = al1.toArray();
        int sum = 0;
        for(int i = 0; i < o.length; ++i)
            sum += ((Integer)o[i]).intValue();
        System.out.println("sum: " + sum); // sum: 10

2. LinkedList

  • 链表
代码语言:javascript
复制
		LinkedList ll = new LinkedList();
        ll.add("F");
        ll.add("B");
        ll.add("D");
        ll.add("E");
        ll.add("C");
        ll.addLast("Z");
        ll.addFirst("A");
        ll.add(1, 1);
        System.out.println(ll);
        // [A, 1, F, B, D, E, C, Z]
        ll.remove("F");
        ll.remove(2);
        System.out.println(ll);
        // [A, 1, D, E, C, Z]
        ll.removeFirst();
        ll.removeLast();
        System.out.println(ll);
        // [1, D, E, C]
        ll.set(2, "changed");
        System.out.println(ll);
        // [1, D, changed, C]

3. HashSet

  • 哈希集合,无序
代码语言:javascript
复制
		HashSet hs = new HashSet();
        hs.add("A");
        hs.add("B");
        hs.add("C");
        hs.add(1);
        System.out.println(hs);
        // [A, 1, B, C] 无序

4. TreeSet

  • 树set,有序
代码语言:javascript
复制
		TreeSet ts = new TreeSet();
        ts.add("C");
        ts.add("B");
        ts.add("D");
        ts.add("A");
        ts.add("Aa");
        System.out.println(ts);
        // [A, Aa, B, C, D] 有序

5. Iterator、ListIterator

  • ListIterator 可以修改元素,可以双向遍历,是 Iterator 的扩展
代码语言:javascript
复制
		System.out.println(al);
        // [C, A2, E, B, D]
        Iterator it = al.iterator();
        while (it.hasNext()){
            Object obj = it.next();
            System.out.print(" "+ obj);
        } // 使用 iterator 遍历
        //  C A2 E B D
        System.out.println();

        ListIterator lit = al.listIterator();
        while(lit.hasNext()){
            Object obj = lit.next();
            lit.set(obj+"*");
        }
        System.out.println("打印修改后的内容:");
        it = al.iterator();
        while (it.hasNext()){
            Object obj = it.next();
            System.out.print(" "+ obj);
        } // C* A2* E* B* D*
        System.out.println();

        System.out.println("逆序输出:");
        while(lit.hasPrevious()){
            Object obj = lit.previous();
            System.out.print(" "+ obj);
        } // D* B* E* A2* C*
        System.out.println();

6. HashMap

代码语言:javascript
复制
		// HashMap
        HashMap 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());
        }

输出:

代码语言:javascript
复制
Ming:19
Michael:18
Ming:19
Michael:20

7. TreeMap

代码语言:javascript
复制
		// TreeMap
        TreeMap tm = new TreeMap();
        tm.put(18, "Michael");
        tm.put(19, "Ming");
        tm.put(0, "Java");
        // values
        Collection col = tm.values();
        Iterator it1 = col.iterator();
        while(it1.hasNext()){
            System.out.println(it1.next());
        }
        // keySet
        Collection 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());
        }

输出:

代码语言:javascript
复制
Java
Michael
Ming
0
18
19
0=Java
18=Michael
19=Ming

8. Stack

  • Stack 继承于 Vector,Vector 与 ArrayList 类似
代码语言:javascript
复制
class StackDemo{
    static void showpush(Stack st, int a){
        st.push(a);
        System.out.println("入栈:" + a);
        System.out.println(st);
    }
    static void showpop(Stack st){
        Integer a = (Integer) st.pop();
        System.out.println("出栈:" + a);
        System.out.println(st);
    }
    public static void main(String [] args){
        Stack st = new Stack();
        System.out.println(st);
        showpush(st,2);
        showpush(st,4);
        showpush(st,1);
        showpop(st);
        showpop(st);
        showpop(st);
        try{
            showpop(st);//空了,异常
        }
        catch (EmptyStackException e){
            System.out.println("异常:" + e);
        }
    }
}

输出:

代码语言:javascript
复制
[]
入栈:2
[2]
入栈:4
[2, 4]
入栈:1
[2, 4, 1]
出栈:1
[2, 4]
出栈:4
[2]
出栈:2
[]
异常:java.util.EmptyStackException

9. Properties 类

代码语言:javascript
复制
		// Properties : k v 都是字符串的 Hashtable
        Properties 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 found
        System.out.println(str);

输出:

代码语言:javascript
复制
中国 : 北京
日本 : 东京
not found

读写简单 数据库

  • 特别适合做简单数据库
代码语言:javascript
复制
class PropertiesFile{
    public static void main(String [] args){
        Properties settings = new Properties();
        try{
            settings.load(new FileInputStream("./count.txt"));
        }
        catch (Exception e){
            settings.setProperty("count", new Integer(0).toString());//没有文件就从0开始计数
        }
        int c = Integer.parseInt(settings.getProperty("count"))+1;
        System.out.println("这是第" + c + "次使用本程序!");
        settings.put("count", new Integer(c).toString());
        try{
            settings.store(new FileOutputStream("./count.txt"), "存储中");
        }
        catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}

输出:

代码语言:javascript
复制
这是第1次使用本程序!
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/03/01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1. ArrayList
  • 2. LinkedList
  • 3. HashSet
  • 4. TreeSet
  • 5. Iterator、ListIterator
  • 6. HashMap
  • 7. TreeMap
  • 8. Stack
  • 9. Properties 类
    • 读写简单 数据库
    相关产品与服务
    数据库
    云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档