JAVA中Map,List,set集合的学习总结

java中Object 是所有类的基类 :如下图所示

在这里插入图片描述

一. Map集合的使用

  1. 常用方法
size() 获取集合中名值对的数量

put(key k, value v ) 添加元素
get( k ) 获取键对应的值

remove( key)键对应的元素
clear()清空

contains(key)  查询集合中是否包含某个K   
contains(value)  查询集合中是否包含某个value值

keySet()获取所有的键
values()  获取所有的value值



  1. 案例
    public static void main(String[] args) {

        HashMap<String, String> students = new HashMap<>();
        students.put("name", "小明");
        students.put("age", "18");
        students.put("hobby", "football");

        System.out.println(students.size());

        // 遍历展示所有值
        for (String key : students.keySet()) {
            System.out.println("key = " + key + " and value = " + students.get(key));
        }

        System.out.println("=================================");

        System.out.println(students.get("age"));
        System.out.println(students.containsKey("name"));
        System.out.println("=================================");
        System.out.println(students.remove("hobby"));
        System.out.println(students.size());

    }

二. Set 的使用

  1. 常用方法
size() 获取元素数量

add(obj)添加元素

clear() 清空
remove(obj)删除指定对象

contatins(obj)查询,如果集合中含有该元素则返回true
  1. 案例
  public static void main(String[] args) {

        Set<String> set = new HashSet<>();
        set.add("牛奶");
        set.add("面包");
        set.add("火腿");

        System.out.println(set.size());

        System.out.println(set.size());

        System.out.println("==========1遍历方式===================");
        for (String s : set) {
            System.out.println(s);
        }

        System.out.println("==========2遍历方式===================");

        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }

    }

三. List的使用

  1. 常用方法
size()获取元素数量

add(obj)添加元素
get(int index)获取指定索引的元素

remove(int index)移除指定索引位置的元素
remove(obj)移除指定元素
clear()清空

contains(obj)查询,若集合中有该元素,则返回true

for的形式:
	forint i=0;i<arr.size();i++{...}

foreach的形式: 
	forint i:arr){...}

iterator的形式:
	Iterator it = arr.iterator();
	while(it.hasNext()){ object o =it.next(); ...}

  1. 案例
    public static void main(String[] args) {

        List<String> list = new ArrayList<>();
        list.add("jim");
        list.add("tom");
        list.add("jerry");

        System.out.println(list.size());

        list.add(1, "小明");
        System.out.println(list.size());

        System.out.println("==========1遍历方式===================");

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        System.out.println("==========2遍历方式===================");
        for (String l : list) {
            System.out.println(l);
        }

        System.out.println("==========3遍历方式===================");

        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }
    }

参考文章

  • 13
    点赞
  • 85
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值