Java的Map集合和Collections

Map集合

概述

在这里插入图片描述

public class MapDemo {
    public static void main(String[] args) {
        Map<String,String> map=new HashMap<>();

        //V put(K key, V value) 将指定的值与映射中的指定键值关联
        map.put("name","zhangsan");
        map.put("age","18");
        map.put("age","18");

        //输出集合对象
        System.out.println(map);

    }
}

在这里插入图片描述

Map集合的基本功能

在这里插入图片描述

public class MapDemo2 {
    public static void main(String[] args) {
        Map<String,String> map=new HashMap<>();

        //V put(K key, V value) 将指定的值与映射中的指定键值关联
        map.put("name","lisi");
        map.put("age","20");

        //V remove(Object key) 根据键值删除键值对元素
        System.out.println(map.remove("name"));

        //void clear
//        map.clear();

        //boolean containsKey(Object key) 判断集合是否包含指定的键
        System.out.println(map.containsKey("name"));
        System.out.println(map.containsKey("age"));

        //boolean containsValue(自己学吧) 判断是否包含指定的值

        //boolean isEmpty() 判断集合是否为空
        System.out.println(map.isEmpty());

        //int size() 集合长度,也就是键值的个数
        System.out.println(map.size());

        System.out.println(map);
    }
}

在这里插入图片描述

Map集合的获取功能

在这里插入图片描述

public class MapDemo3 {
    public static void main(String[] args) {
        Map<String,String> map=new HashMap<>();

        map.put("name","wangwu");
        map.put("age","20");
        map.put("sex","male");

        //V get(Object key) 根据键获取值
        System.out.println(map.get("sex"));
        System.out.println("------------------");

        //Set<key> keySet() 获取所有键的集合
        Set<String> key= map.keySet();
        System.out.println(key);
        System.out.println("---------------");
        for (String s: key) {
            System.out.println(s);
        }

        //Collection(V) 获取有值的集合
        Collection<String> collection= map.values();
        System.out.println(collection);
        System.out.println("------------");
        for (String s: collection) {
            System.out.println(s);
        }
    }
}

Map集合遍历方式一

在这里插入图片描述
上一个代码中将set方法注释下的代码修改以下为代码即可

//Set<key> keySet() 获取所有键的集合
//遍历键的集合 获取到每一个键 用增强for循环实现
        for (String s: key) {
            //根据键查找值 用get()方法实现
            String s1 = map.get(s);
            System.out.println(s+","+s1);
        }

遍历方式二

在这里插入图片描述

public class MapDemo4 {
    public static void main(String[] args) {
        Map<String,String> map=new HashMap();

        map.put("name","maliu");
        map.put("age","50");
        map.put("sex","female");


        //获取所有键值对对象集合
        Set<Map.Entry<String, String>> entries = map.entrySet();
        //遍历
        for (Map.Entry<String, String> me: entries){
            //根据键值对对象获取键和值
            String key = me.getKey();
            String value = me.getValue();
            System.out.println(key+","+value);
        }
    }
}

在这里插入图片描述

Collections

在这里插入图片描述

public class CollectionsDemo1 {
    public static void main(String[] args) {
        List<Integer> list=new ArrayList<>();

        list.add(30);
        list.add(40);
        list.add(80);
        list.add(230);
        list.add(90);

        //public static <T extends Comparable<? super T>> void sort (List<T> list):将指定的列表升序排序
//        Collections.sort(list);

        //public static void reverse (List<?> list): 反转指定列表中的元素顺序
//        Collections.reverse(list);

        //public static void shuffle (List<?> list):使用默认的随机排列指定的列表
        Collections.shuffle(list);

        System.out.println(list);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值