ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

集合Collection接口和Map接口的共性方法

2022-01-14 22:02:54  阅读:86  来源: 互联网

标签:Map map System Collection put 接口 println out


一、集合Collection接口的方法有:

  1. boolean add(E e)
    该方法是在集合中添加一个元素E是代表泛型(就是由创建对象时确定数据类型),返回值是一个boolean值,添加成功时返回true,但是这个返回值一般不接收。
		Collection<String> col=new ArrayList<>();
        boolean ad = col.add("花花");
        System.out.println("ad-->"+ad);
        System.out.println("添加的元素为:"+col);

运行结果:
在这里插入图片描述

  1. boolean remove(Object o)
    如果存在的话, 从集合中移除指定元素。即使用该方法是可以同时删除一个或者多个元素e,且返回值为true,如果集合中没有该元素,则删除失败返回false。
		col.add("花花");
        col.add("草草");
        col.add("关羽");
        col.add("刘备");
        boolean rem = col.remove("草草");
        System.out.println("rem-->"+rem);
        System.out.println("集合元素为:"+col);

        boolean mov = col.remove("张飞");
        System.out.println("mov-->"+mov);
        System.out.println("集合元素为:"+col);

运行结果:
在这里插入图片描述

  1. int size()
    返回集合中的元素数。如果此集合包含的元素大于Integer.MAX_VALUE,则返回 Integer.MAX_VALUE
		col.add("花花");
        col.add("草草");
        col.add("关羽");
        col.add("刘备");
        int size = col.size();
        System.out.println("col集合的长度为:"+size);

运行结果:
在这里插入图片描述

  1. boolean contains(Object o)
    如果集合包含指定的元素o,则返回 true。即集合中是否存在数据和类型都相同的元素o,有返回true,否则返回flase。
		col.add("花花");
        col.add("草草");
        col.add("关羽");
        col.add("刘备");
        boolean cont = col.contains("草草");
        System.out.println("cont-->"+cont);
        boolean tain = col.contains("张飞");
        System.out.println("tian-->"+tain);

运行结果:
在这里插入图片描述
5. boolean isEmpty()
集合中是否有元素,有返回的是false,否则返回true

		Collection <String> list1=new ArrayList<>();
        list1.add("花花");
        list1.add("草草");
        boolean emp = list1.isEmpty();
        System.out.println("emp-->"+emp);
        Collection<String> arr=new ArrayList<>();
        boolean arremp = arr.isEmpty();
        System.out.println("arremp-->"+arremp);

运行结果:
在这里插入图片描述
以上只是列举了一些之后我会进行更深层次的代码实践展示的方法,并没有完全列举完。

二、Map接口的方法

Map是存放键值对的集合,一个键对应一个值;在一个Map对象中不允许存在键相同,但可以有值相同存在,即一个键对应一个值,一个中可以有不同的键,在Map中存放键值对的是一个Entry对象(只存放键值对)。Map集合是为了方便存取数据,在取数据时可以通过键Key来获取数据,也可以通过Entry对象来直接获取值(Value)。介绍几种主要的使用较多的方法:

  1. V put(K key, V value) 将指定的值与此映射中的指定键添加到该集合中。
    注意:
    (1)如果该集合中存在键key就会将新值覆盖旧值.
    (2) 返回值是和定义该Map对象时给定的value的类型一致,如果要添加的键在集合中存在就返回旧值,否则返回null。
		Map<String,Integer> map=new HashMap<>();
        Integer value1 = map.put("花少", 1);
        map.put("邓少", 2);
        System.out.println(value1);
        System.out.println(map);
        Integer value2 = map.put("花少", 11);
        System.out.println(value2);
        System.out.println(map);

运行结果:
在这里插入图片描述

  1. boolean isEmpty() 如果此Map集合是否为空,是就返回true,否则返回false
		Map<String,Integer> map=new HashMap<>();
        Integer value1 = map.put("花少", 1);
        map.put("邓少", 2);
        map.put("周少", 3);

        Map<String,Integer> colMap=new HashMap<>();
        System.out.println("map: "+map);
        System.out.println("colMap: "+colMap);
        boolean mapEmpty = map.isEmpty();
        boolean colMapEmpty = colMap.isEmpty();
        System.out.println("map是否为空:"+mapEmpty);
        System.out.println("colMap是否为空:"+colMapEmpty);

运行结果:
在这里插入图片描述

  1. int size() 返回此Map集合中的键-值对数量。
 		Map<String,Integer> map=new HashMap<>();
        Integer value1 = map.put("花少", 1);
        map.put("邓少", 2);
        map.put("周少", 3);

        Map<String,Integer> colMap=new HashMap<>();
        System.out.println("map: "+map);
        System.out.println("colMap: "+colMap);
        int mapSize = map.size();
        System.out.println("map的大小是:"+mapSize);
        int colMapSize = colMap.size();
        System.out.println("colMap的大小是:"+colMapSize);

运行结果:
在这里插入图片描述

  1. V remove(Object key) 如果存在键存在就将该键值对从Map集合中移除。
    注意:
    (1)remove方法会删除key所对应的value值对,删除成功返回值是删除的value值,否则返回null
    (2)在允许可以存储null的键和值的Map集合中(比如hashMap、linkedMap),返回null值不能说明该键值对在原集合中不存在,可能存在该value值就是null的情况
    (3)在允许可以存储null的键和值的Map集合中(比如hashMap、linkedMap),参数传递null也是允许的,而在允许可以存储null的键和值的Map集合中(比如hashTable)则会报错。
		Map<String,Integer> map=new HashMap<>();
        map.put("花少", 1);
        map.put("邓少", 2);
        map.put("周少", 3);
        map.put("叶少", 4);
        System.out.println("map: "+map);
        Integer value1 = map.remove("叶少");
        Integer value2 = map.remove("树叶");
        System.out.println("value1-->"+value1);
        System.out.println("value2-->"+value2);
        System.out.println("map:"+map);

运行结果:
在这里插入图片描述

  1. Set keySet() 返回此映射中包含的键的 Set 集合。
    V get(Object key) 返回指定键所对应的值,如果此Map集合中不包含该键的映射关系,则返回 null。
		Map<String,Integer> map=new HashMap<>();
        map.put("花少", 1);
        map.put("邓少", 2);
        map.put("周少", 3);
        map.put("叶少", 4);
        System.out.println("map: "+map);
        Set<String> set=map.keySet();
        for (String s : set) {
            System.out.println(s+": "+map.get(s));
        }
        String m="猪猪侠";
        System.out.println(m+": "+map.get(m));

运行结果:
在这里插入图片描述

  1. Set<Map.Entry<K,V>> entrySet() 返回此Map集合中包含的键值对的 Set 集合。
    注意:static interface Map.Entry<K,V> 键-值对。Entry是保存键值对的内部类,在Entry接口中有:
    (1) K getKey() 直接获取key键
    (2)V getValue()直接获取value值,
    (3) V setValue() 用指定的值替换与此项对应的值
		Map<String,Integer> map=new HashMap<>();
        map.put("花少", 1);
        map.put("邓少", 2);
        map.put("周少", 3);
        map.put("叶少", 4);
	    System.out.println("map: "+map);
        Set<Map.Entry<String,Integer>> entrySet=map.entrySet();
        for (Map.Entry<String, Integer> entry : entrySet) {
            System.out.println(entry.getKey()+": "+entry.getValue());
        }

运行结果:
在这里插入图片描述

  1. Collection values() 返回此映射中包含的值的 Collection 集合。
 		Map<String,Integer> map=new HashMap<>();
        map.put("花少", 1);
        map.put("邓少", 2);
        map.put("周少", 3);
        map.put("叶少", 4);
        System.out.println("map: "+map);
        Collection<Integer> list=map.values();
        System.out.println("list: "+list);

运行结果:
在这里插入图片描述

标签:Map,map,System,Collection,put,接口,println,out
来源: https://blog.csdn.net/Star_RainSea/article/details/122347213

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有