2021-05-11

1.创建map文件夹
建立Java HashMapDemo

public class HashMapDemo {
    public static void main(String[] args) {
        HashMap map = new HashMap<>();
        map.put("henan" , "河南" );  // entry 实例
        map.put("hebei" , "河北" );
        map.put("hubei" , "湖北" );
        map.put("hubei" , "湖北1" );  // 键重复会覆盖掉原有的值
       /* map.put("null" , "空1");
        map.put(null , "空2");
        map.put( null , null );*/
        System.out.println(map);

        for (Map.Entry m : map.entrySet() ) {
            System.out.println(m);
        }
        for (String  k : map.keySet() ) {
            System.out.println( k + "=" + map.get(k));
        }

        for ( String v : map.values() ) {
            System.out.println(v);
        }

再在文件夹中建立TreeMapDemo

public class TreeMapDemo {
    public static void main(String[] args) {
        TreeMap map = new TreeMap<>();
        map.put("henan" , "河南" );  // entry 实例
        map.put("hebei" , "河北" );
        map.put("hubei" , "湖北" );
        map.put("hubei" , "湖北1" );  // 键重复会覆盖掉原有的值
       /* map.put("null" , "空1");
        map.put(null , "空2");
        map.put( null , null );*/
        System.out.println(map);

        for (Map.Entry m : map.entrySet() ) {
            System.out.println(m);
        }
        for (String  k : map.keySet() ) {
            System.out.println( k + "=" + map.get(k));
        }

        for ( String v : map.values() ) {
            System.out.println(v);
        }

你可能感兴趣的:(2021-05-11)