java集合:map及其常用子类

常用方法

Map

public V put(K key, V value):
将指定的值与此映射中的指定键关联 ,返回此键之前对应的值,第一次添加此键则返回null
public void clear(): 
从此映射中移除所有映射关系
public V remove(Object key): 
如果存在指定键,则删除此映射,返回此键对应的值,如果不存在指定键,返回null
public boolean containsKey(Object key): 
如果此映射包含指定键的映射关系,则返回 true
public boolean containsValue(Object value):
如果此映射将一个或多个键映射到指定值,则返回 true
 public boolean isEmpty():
如果此映射未包含键-值映射关系,则返回 true。
public Set<Map.Entry<K,V>> entrySet():
将此映射中包含的映射关系以set类型返回
public V get(Object key): 
返回指定键所对应的值,如果不包含该键则返回null
public Set<K> keySet() : 
将此映射中包含的键以set类型返回
public Collection<V> values():
将此映射包含的值返回
public int size(): 
返回此映射中的映射数

Collections


public static <T> void sort(List<T> list) : 
将给定列表元素的自然顺序升序排序
public static <T> int binarySearch(List<?> list,T key):
在给定列表中以二分查找获得指定元素的索引
public static <T> T max(Collection<?> coll) 
根据元素的自然顺序返回指定集合的最大元素
public static void reverse(List<?> list) 
将指定列表中的元素顺序反转
public static void shuffle(List<?> list) 
将指定列表的元素顺序随机打乱
public static <T> void sort(List<T> list) : 
按给定列表元素的自然顺序进行升序排序
public static <T> void sort(List<T> list, Comparator<T> c)
根据指定比较器产生的顺序对指定列表进行排序

 

Map简介


键值对,一个映射不能包含重复的键,一个键只能映射到一个值,键相同值覆盖

 

Map接口和Collection接口的不同


        Map是双列的,Collection是单列的
        Map的键唯一,Collection的子体系Set是唯一的
        Map集合的数据结构针对键有效,跟值无关;Collection集合的数据结构是针对元素有效


HashMap和Hashtable的区别

HashMap:允许null值和null键,线程不安全,效率高,不同步
HashTable:不允许null值和null键,线程安全,效率低,同步

HashMap与LinkedHashMap:

HashMap: 允许null值与null键,元素无序,键唯一
LinkedHashMap:允许null值与null键,元素有序,且键唯一。

map的遍历方式:

HashMap<Student, Integer> students = new HashMap<>();
    Student hanli = new Student("hanli", 23);
        Student guili = new Student("guili", 22);
        Student zhangxiaofan = new Student("zhangxiaofan", 12);
        students.put(hanli,1005);
        students.put(guili,1006);
        students.put(zhangxiaofan,1008);

1.键找值:keySet()方法

 Set<Student> keySet = students.keySet();
        for(Student s : keySet){
            System.out.println(s.getName()+"---"+s.getAge()+"---"+students.get(s));
        }


2.键值对对象:entrySet()方法
  

 Set<Map.Entry<Student, Integer>> entries = students.entrySet();
        for(Map.Entry<Student,Integer> entry: entries){
            System.out.println(entry.getKey().getName()+"---"
                    +entry.getKey().getAge()+"---"+entry.getValue());
        }

TreeMap:

不允许null键,线程不安全,效率高,不同步

键的数据结构是二叉树,可保证键的排序和唯一。
排序同TreeSet,分为自然排序和比较器排序。


集合嵌套:


1.HashMap套HashMap:例

 HashMap<String, Integer> jichu = new HashMap<>();
        HashMap<String, Integer> jiuye = new HashMap<>();


        jichu.put("张三",20);
        jichu.put("李四",22);
        jiuye.put("王五",21);
        jiuye.put("赵六",23);

        HashMap<String, HashMap<String, Integer>> westos = new HashMap<>();
        westos.put("基础班",jichu);
        westos.put("就业班",jiuye);

        Set<String> key = westos.keySet();
        for(String s : key ){
            System.out.println(s);
            HashMap<String, Integer> stu = westos.get(s);
            Set<Map.Entry<String, Integer>> entries = stu.entrySet();

            for(Map.Entry<String, Integer> entry:entries){
                System.out.println("\t"+entry.getKey()+"---"+entry.getValue());
            }
        }


2.HashMap套ArrayList:例

ArrayList<Student> jichu = new ArrayList<>();
        ArrayList<Student> jiuye = new ArrayList<>();


        jichu.add(new Student("张三",20));
        jichu.add(new Student("李四",22));
        jiuye.add(new Student("王五",21));
        jiuye.add(new Student("赵六",23));

        HashMap<String, ArrayList<Student>> westos = new HashMap<>();
        westos.put("基础班",jichu);
        westos.put("就业班",jiuye);

        Set<String> key = westos.keySet();
        for(String s :key){
            System.out.println(s);
            ArrayList<Student> students = westos.get(s);
            for(Student stu : students){
                System.out.println("\t"+stu.getName()+"---"+stu.getAge());
            }
        }

集合工具类Collections模拟斗地主洗牌和发牌并对牌进行排序的代码实现:

package homework.day0804;

import org.westos.day0804.MyComparator;

import java.util.*;

public class Doudizhu2 {
    public static void main(String[] args) {

        //新牌
        HashMap<Integer, String> newPoker = getPoker();
       
        //有序号的牌
        ArrayList<Map.Entry<Integer, String>> poker = new ArrayList<>();


        //玩家和底牌
        TreeMap<Integer, String> a = new TreeMap<>(new MyComparator());
        TreeMap<Integer, String> b = new TreeMap<>(new MyComparator());
        TreeMap<Integer, String> c = new TreeMap<>(new MyComparator());
        TreeMap<Integer, String> d = new TreeMap<>(new MyComparator());

        Set<Map.Entry<Integer, String>> entries = newPoker.entrySet();
        for(Map.Entry<Integer, String> entry : entries){

            poker.add(entry);
        }

        //洗牌
        Collections.shuffle(poker);
        Collections.shuffle(poker);
        Collections.shuffle(poker);

        //发牌
        for (int i=0;i<poker.size();i++){

            Map.Entry<Integer, String> one =poker.get(i);

            if(i>=poker.size()-3){
                d.put(one.getKey(),one.getValue());
            }else if(i%3 == 0){
                a.put(one.getKey(),one.getValue());
            }else if(i%3 == 1){
                b.put(one.getKey(),one.getValue());
            }else {
                c.put(one.getKey(),one.getValue());
            }
        }
        //看牌
        look("a",a);
        look("b",b);
        look("c",c);
        look("d",d);

    }
    public static void look(String name,Map<Integer,String> map){
        System.out.println("玩家"+name);
        Set<Integer> key = map.keySet();
        for (Integer i : key){
            System.out.print(map.get(i)+" ");
        }
        System.out.println();
    }

    public static HashMap<Integer,String > getPoker(){

        ArrayList<String> pokerList = new ArrayList<>();

        String[] hua = {"方块","梅花","红桃","黑桃"};

        String[] zi = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        for(int i=0;i<zi.length;i++){
            for(int j=0;j<hua.length;j++){
                pokerList.add(hua[j].concat(zi[i]));
            }
        }
        HashMap<Integer, String> poker = new HashMap<>();
        for(int i=0;i<52;i++){
            poker.put(i,pokerList.get(i));
        }
        poker.put(52,"小王");
        poker.put(53,"大王");

        return poker;

    }
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值