Java novice learning 2021-2-1 record the daily learning content (if there is any infringement, please contact to delete!!!)

2021-2-1

1.Map collection

Insert picture description here
Insert picture description here

2. Common methods of Map collection

Insert picture description here

3. Get function of Map collection

Insert picture description here

package com.wc.MapDemo;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author wc
 * @Date: 2021/02/01/16:57
 */
public class MapDemo1 {
    
    
    public static void main(String[] args) {
    
    
        Map<String, String> map=new HashMap<>();
        map.put("张三","李四");
        map.put("王五","赵柳");
        map.put("skr","阿伟");
        //根据对应的键获取值,不存在就返回null
        System.out.println(map.get("张三"));
        //获取所有键集合
        Set<String> keySet = map.keySet();
        for (String s : keySet) {
    
    
            System.out.println(s);
        }
        //获取所有值集合
        Collection<String> values = map.values();
        for (String value : values) {
    
    
            System.out.println(value);
        }
        System.out.println(map);
    }
}

4. The traversal of the Map collection (case enumeration)

package com.wc.MapDemo;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author wc
 * @Date: 2021/02/01/16:57
 */
public class MapDemo1 {
    
    
    public static void main(String[] args) {
    
    
        Map<String, String> map=new HashMap<>();
        map.put("张三","李四");
        map.put("王五","赵柳");
        map.put("skr","阿伟");
        
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
    
    
            String value = map.get(key);
            System.out.println(key+","+value);
        }
    }
}

Case study

package com.wc.MapDemo;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author wc
 * @Date: 2021/02/01/16:57
 */
public class MapDemo2 {
    
    
    public static void main(String[] args) {
    
    
      HashMap<String, Student> hashMap=new HashMap<>();

      Student student=new Student("思琪",250);
      Student student1=new Student("日辰",18);
      Student student2=new Student("橙子",20);

      hashMap.put("zz",student);
      hashMap.put("帅气",student1);
      hashMap.put("勇敢",student2);

        Set<String> set = hashMap.keySet();
        for (String keySet : set) {
    
    
            Student value = hashMap.get(keySet);
            System.out.println(keySet+","+value.getName()+","+value.getAge());
        }

        System.out.println("-----------------------------");

        Set<Map.Entry<String, Student>> entrySet = hashMap.entrySet();
        for (Map.Entry<String, Student> entry : entrySet) {
    
    
            String key = entry.getKey();
            Student value = entry.getValue();
            System.out.println(key+","+value.getName()+","+value.getAge());
        }
    }
}

Case study
Insert picture description here

package com.wc.MapDemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author wc
 * @Date: 2021/02/01/16:57
 */
public class MapDemo3 {
    
    
    public static void main(String[] args) {
    
    
        HashMap<String, String> hm1 = new HashMap<>();
        hm1.put("张三", "李四");
        hm1.put("王五", "赵柳");
        hm1.put("skr", "阿伟");

        ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
        arrayList.add(hm1);
        for (HashMap<String, String> hashMap : arrayList) {
    
    
            Set<String> set = hashMap.keySet();
            for (String keySet : set) {
    
    
                String value = hashMap.get(keySet);
                System.out.println(keySet+","+value);
            }

        }
    }
}

Case study
Insert picture description here

package com.wc.MapDemo;

import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;

/**
 * @author wc
 * @Date: 2021/02/02/9:19
 */
public class StringBuilderDemo {
    
    
    public static void main(String[] args) {
    
    

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串:");
        String line = scanner.nextLine();

        HashMap<Character, Integer> hm = new HashMap<>();
        //遍历数组,得到每一个字符,并根据每个字符找到对应的value
        for (int i = 0; i < line.length(); i++) {
    
    
            char key = line.charAt(i);
            Integer value = hm.get(key);
            //如果返回null,说明该字符串不存在,1作为存储值
            if (value == null) {
    
    
                hm.put(key, 1);
                //如果不为null,说明该字符串存在,value作为存储值,每有一个加1
            } else {
    
    
                value++;
                hm.put(key, value);
            }
        }
        //遍历HashMap并拼接
        StringBuilder sb = new StringBuilder();
        for (Character key : hm.keySet()) {
    
    
            Integer value = hm.get(key);
            sb.append(key).append("(").append(value).append(")");
        }
        //StringBuilder类是一个可变的字符序列。
        String result = sb.toString();
        System.out.println(result);
    }
}

5.Collections

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49221590/article/details/113524018