Java=Map集合,集合的嵌套,斗地主发牌案例,冒泡排序算法

1.Map集合(和Collection没有直接的联系)
2.集合的嵌套
3.斗地主发牌案例
4.冒泡排序算法(a.算法过程 b.算法的代码实现)   

 

一,Map集合

什么是Map集合:
	Collection集合称为单列集合,Map集合称为双列集合
Map集合的特点:
	a.Collection每个元素单独存在(单列),Map每个元素成对存在(双列)
    b.Map集合键必须是唯一的,值是可以重复的 
    c.Collection<E>中泛型只有一个,Map<K,V>中泛型有两个(其中K代表键的类型,V代表值的类)    

Map的3个常用实现类以及其特点

Map接口有三个常见的实现类:
	HashMap: 底层采用哈希表结构, 无序
    LinkedHashMap:底层采用链表+哈希表结构,有序
    TreeMap: 底层采用红黑树结构,无序(但是键有自然顺序) 
    重点: Map中为了保证键的唯一性,如果键是自定义类型,必须重写键的hashCode和equals方法

Map接口定义的通用方法

增: V put(K 键,V 值); 添加一个键值对,返回null
删: V remove(K 键);根据键去删除键值对,返回被删除的键值对的值
改: V put(K 键,V 值); 添加一个重复的键时,该方法变成修改,返回修改前的值
查: V get(K 键); 根据键获取对应的值
其他:
	public boolean containsKey(Object 键); 判断Map中是否包含该键
    public boolean containsValue(Object 值); 判断Map中是否包含该值   
        
使用Map中通用方法:
public class TestMap {
    public static void main(String[] args) {
        //1.创建一个Map的实现类对象
        HashMap<String,Integer> map = new HashMap<String, Integer>();
        //2.添加几个
        map.put("张",18);
        map.put("四",28);
        map.put("五",38);
        map.put("六",48);
        map.put("前",8);
        map.put("王",88);
        //3.打印

        System.out.println(map);
        //4.删除
        Integer v1 = map.remove("五");
        System.out.println(v1);
        System.out.println(map);
        //5.获取
        Integer v2 = map.get(张三");
        System.out.println(v2);
        System.out.println(map);
        //6.修改,也是调用put
        Integer v3 = map.put("前", 9);
        System.out.println(v3);
        System.out.println(map);
        //7.判断
        boolean b1 = map.containsKey("七");
        System.out.println(b1);

        boolean b2 = map.containsValue(18);
        System.out.println(b2);
    }
}	

Map的遍历

方式一:

第一种方式称为:以键找值
public class TestMap1 {
    public static void main(String[] args) {
        //第一种遍历方式:以键找值
        //1.创建一个Map的实现类对象
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        //2.添加几个
        map.put("三", 18);
        map.put("四", 28);
        map.put("五", 38);
        map.put("六", 48);
        map.put("妻", 8);
        map.put("八", 88);
        //3.获取所有的键
        Set<String> keys = map.keySet();
        //4.遍历这个keys集合
        for (String key : keys) {
            //5.以键找值
            Integer value = map.get(key);
            System.out.println(key + "..." + value);
        }
    }
}    

遍历方式二

第二种方式称为:键值对方式
public class TestMap02 {
    public static void main(String[] args) {
        //第一种遍历方式:以键找值
        //1.创建一个Map的实现类对象
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        //2.添加几个
        map.put("三", 18);
        map.put("四", 28);
        map.put("五", 38);
        map.put("六", 48);
        //Map集合遍历的第二种方式:键值对方式
        //3.获取Map中所有的键值对
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        //4.遍历这个entries集合
        for (Map.Entry<String, Integer> entry : entries) {
            //5.从entry中取出键和值
     
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值