map 常见方法

常见方法:
1、添加:
	1、V put(K key, V value)    (可以相同的key值,但是添加的value值会覆
盖前面的,返回值是前一个,如果没有就返回null)                                          
	2、putAll(Map<? extends K,? extends V> m)  从指定映射中将所有映射关
系复制到此映射中(可选操作)。
2、删除
	1、remove()    删除关联对象,指定key对象
	2、clear()     清空集合对象
3、获取
     1:value get(key); 可以用于判断键是否存在的情况。当指定的键不存在的时候,返
回的是null。
 
3、判断:
	1、boolean isEmpty()   长度为0返回true否则false
    2、boolean containsKey(Object key)  判断集合中是否包含指定的key
3、boolean containsValue(Object value)  判断集合中是否包含指定的value
4、长度:
Int size()


Map<String, Integer> map1 = new HashMap<String, Integer>();
        map1.put("jack", 20);
        map1.put("rose", 18);
        map1.put("lucy", 17);
        map1.put("java", 25);
        System.out.println(map1);

        /**
         * 添加
         */
System.out.println(map1.put("jack",30)); // 添加同一key,会返回原来值20
Map<String,Integer> map2 = new HashMap<String,Integer>();
map2.put("张三丰",100);
map2.put("虚竹",20);
System.out.println("map2:"+map2);
map1.putAll(map2);
System.out.println("map1:"+map1);
        /**
         * 删除
         */
System.out.println("value:"+map1.remove("java"));
map1.clear();
System.out.println("map1:"+map1);
        /**
         * 获取
         */
System.out.println("value:"+map1.get("jack"));
System.out.println("map.size:"+map1.size());
        /**
         * 判断
         */
System.out.println("isEmpty:"+map1.isEmpty());  // 长度为0:true,否则false
System.out.println("containsKey:"+map1.containsKey("jack"));
System.out.println("containsValue:"+map1.containsValue(20));
        /**
         * 获取key和value
         */
Collection<Integer> vs = map1.values();
for (Integer integer : vs) {
      System.out.println(integer);
}

Collection<String> strings = map1.keySet();
for(String string : strings) {
       System.out.println(string);
}
        /**
         * HashMap
         */
HashMap<Person,String> hm = new HashMap<Person,String>();
hm.put(new Person("jack",20),"1001");
hm.put(new Person("rose",18),"1002");
hm.put(new Person("lucy",19),"1003");
hm.put(new Person("hmm",17),"1004");
hm.put(new Person("ll",25),"1005");
System.out.println(hm);
// 覆盖
System.out.println(hm.put(new Person("rose",18),"1006"));
System.out.println(hm);
        /**
         * TreeMap
         */
        TreeMap<String,Integer> tree = new TreeMap<String,Integer>();
        tree.put("张三",19);
        tree.put("李四",20);
        tree.put("王五",21);
        tree.put("赵六",22);
        tree.put("周七",23);
        tree.put("张三",24);
        System.out.println(tree);
        System.out.println("张三".compareTo("李四"));


/**
 * HashMap嵌套HashMap
*/
public class Test5 {
    public static void main(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("张三",20);
        map.put("李四",22);
        HashMap<String,Integer> map1 = new HashMap<>();
        map1.put("王五",21);
        map1.put("赵六",23);
        HashMap<String,HashMap<String,Integer>> mapmax = new HashMap<>();
        mapmax.put("基础班",map);
        mapmax.put("就业班",map1);
        for (Map.Entry<String,HashMap<String,Integer>> entry : mapmax.entrySet()){
            // entry.getValue();
            System.out.println(entry.getKey());
            for (Map.Entry<String,Integer> entry1 : entry.getValue().entrySet()){
                System.out.println(entry1.getKey()+"="+entry1.getValue());
            }
        }
    }

    @Test
    public void test(){
        HashMap<String,String> map = new HashMap<>();
        map.put("周瑜","小乔");
        map.put("吕布","貂蝉");
        HashMap<String,String> map1 = new HashMap<>();
        map1.put("郭靖","黄蓉");
        map1.put("杨过","小龙女");
        HashMap<String,String> map2 = new HashMap<>();
        map2.put("令狐冲","任盈盈");
        map2.put("林平之","岳灵珊");
        ArrayList<HashMap<String,String>> list = new ArrayList<>();
        list.add(map);
        list.add(map1);
        list.add(map2);
        for (HashMap<String,String> hs : list) {
            for(Map.Entry<String,String> entry : hs.entrySet()){
                System.out.println(entry.getKey()+"="+entry.getValue());
            }
        }
    }
}

-------------------------------------------------------------------------------------------------
public class Test4 {
    public static void main(String[] args) {
        // 遍历之键找值
        HashMap<Phone,String> map = new HashMap<>();
        map.put(new Phone("Apple",7000),"美国");
        map.put(new Phone("Sony",5000),"日本");
        map.put(new Phone("Huawei",6000),"中国");
//        for(Phone phone : map.keySet()) {
//            phone.getBrand();
//            phone.getPrice();
//            System.out.println(phone.getBrand()+"="+phone.getPrice());
//        }

        // 建议使用entrySet
        for(Map.Entry<Phone,String> entry : map.entrySet()){
            entry.getKey();
            entry.getValue();
            System.out.println(entry.getKey().getBrand()+"="+entry.getKey().getPrice()+"="+entry.getValue());
        }
    }

}

class Phone{
    private String Brand;
    private int Price;
    public Phone(String brand, int price) {
        Brand = brand;
        Price = price;
    }
    public String getBrand() {
        return Brand;
    }
    public void setBrand(String brand) {
        Brand = brand;
    }
    public int getPrice() {
        return Price;
    }
    public void setPrice(int price) {
        Price = price;
    }
}

Map<String,String> map = new HashMap<String,String>();
        // 给map添加元素
        map.put("星期一","Monday");
        map.put("星期日","Sunday");
        System.out.println(map);
        System.out.println(map.put("星期一","Mon"));
        System.out.println(map);

        String en = map.get("星期日");
        System.out.println(en);
        // 根据key删除元素,会返回对应value
        String value = map.remove("星期日");
        System.out.println(value);
        System.out.println(map);


Map<String,String> map = new HashMap<>();
        // 给map中添加元素
        map.put("邓超","孙俪");
        map.put("李晨","范冰冰");
        map.put("刘德华","柳岩");
        // 获取map中所有key与value的对应关系
        Set<Map.Entry<String,String>> entrySet = map.entrySet();
        // 遍历Set集合
        Iterator<Map.Entry<String,String>> it = entrySet.iterator();
        while (it.hasNext()) {
            Map.Entry<String,String> entry = it.next();
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"="+value);
        }
        //map集合不能直接使用迭代器或者foreach进行遍历,转成Set之后就可以使用了

        Map<Integer,String> map = new HashMap<Integer,String>();
        map.put(1,"a");
        map.put(2,"b");
        map.put(3,"c");
        map.get(1);

        // 遍历方法一:
        Iterator<Integer> iterator = map.keySet().iterator();
        while(iterator.hasNext()){
            int key = iterator.next();
            String value = map.get(key);
        }

        for(Integer key : map.keySet()){
            String value = map.get(key);
            System.out.println(value);
        }

        // 遍历方法二:
        Iterator<Map.Entry<Integer,String>> iter = map.entrySet().iterator();
        Map.Entry<Integer,String> en;
        while(iter.hasNext()) {
            en = iter.next();
            int key = en.getKey();
            String value = en.getValue();
            System.out.println(key+"="+value);
        }

        for(Map.Entry<Integer,String> entry : map.entrySet()) {
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"="+value);
        }

class Person {
    private String name;
    private int age;
    Person() {

    }
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public int hashCode() {
        return this.name.hashCode() + age * 37;
    }
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Person) {
            Person p = (Person) obj;
            return this.name.equals(p.name) && this.age == p.age;
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        return "Person@name:" + this.name + " age:" + this.age;
    }
}

map 转 list

List<BillCoinTypeUserProperty> billCoinTypeUserPropertyList = new ArrayList<>(map.values());

Java lambda list转换map时,把多个参数拼接作为key

Map<String, Parts> partsMap = synList.stream().collect(Collectors.toMap(k -> k.getOe()+k.getOeId()+k.getPartGroupId()+k.getStdPartId()+k.getBrandCode(), part -> part));
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值