Java教程

Java自学-Map集合

本文主要是介绍Java自学-Map集合,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Java自学-Map集合

1、Map集合概述和特点

image-20210818184721272

IDEA导包快捷键:alt+enter

package lesson01;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/18 18:47
 * FileName: MapDemo01
 * Description:
 */
public class MapDemo01 {
    public static void main(String[] args) {
        Map<String,String> map=new HashMap<String,String>();

        map.put("01","蔡伟1号");
        map.put("02","蔡伟2号");
        map.put("03","蔡伟3号");
        map.put("03","蔡伟4号");//修改值。

        System.out.println(map);
    }
}

2、Map集合的基本功能

image-20210818185418264

package lesson01;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/18 18:54
 * FileName: MapDemo02
 * Description:
 */
public class MapDemo02 {
    public static void main(String[] args) {
        Map<String,String> map=new HashMap<String,String>();

        map.put("张无忌","赵敏");
        map.put("郭靖","黄蓉");
        map.put("杨过","小龙女");

//        System.out.println(map.remove("郭靖"));
//        System.out.println(map.remove("郭襄"));

//        map.clear();

//        System.out.println(map.containsKey("郭靖"));
//        System.out.println(map.containsKey("郭襄"));

//        System.out.println(map.containsValue("小龙女"));
//        System.out.println(map.containsValue("小龙"));


//        System.out.println(map.isEmpty());

        System.out.println(map.size());
        System.out.println(map);
    }
}

3、Map集合的获取功能

image-20210818190401989

package lesson01;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/18 19:04
 * FileName: MapDemo03
 * Description:
 */
public class MapDemo03 {
    public static void main(String[] args) {
        Map<String,String> map=new HashMap<String,String>();

        map.put("张无忌","赵敏");
        map.put("郭靖","黄蓉");
        map.put("杨过","小龙女");

//        System.out.println(map.get("张无忌"));
//        System.out.println(map.get("张三"));

//        Set<String> set = map.keySet();
//        for(String key:set){
//            System.out.println(key);
//        }

//        Collection<String> values = map.values();
//        for(String s:values){
//            System.out.println(s);
//        }
    }
}

4、Map集合的遍历(方式1)

image-20210818190926216

IDEA格式化快捷键:Ctrl+alt+l

package lesson02;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/18 19:10
 * FileName: MapDemo01
 * Description:
 */
public class MapDemo01 {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();

        map.put("张无忌", "赵敏");
        map.put("郭靖", "黄蓉");
        map.put("杨过", "小龙女");

        Set<String> set = map.keySet();
        for (String s : set) {
            String s1 = map.get(s);
            System.out.println(s + "," + s1);
        }
    }
}

5、Map集合的遍历(方式2)

image-20210818191413451

package lesson02;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/18 19:14
 * FileName: MapDemo02
 * Description:
 */
public class MapDemo02 {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();

        map.put("张无忌", "赵敏");
        map.put("郭靖", "黄蓉");
        map.put("杨过", "小龙女");

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

6、HashMap集合练习之键是String值是Student

image-20210818191839081

Student.java

package lesson03;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/18 19:19
 * FileName: Student
 * Description:
 */
public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(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;
    }
}

HashMapDemo.java

package lesson03;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/18 19:19
 * FileName: HashMapDemo
 * Description:
 */
public class HashMapDemo {
    public static void main(String[] args) {
        Map<String,Student> map=new HashMap<String,Student>();

        Student s1 = new Student("蔡伟1号",20);
        Student s2 = new Student("蔡伟2号",21);
        Student s3 = new Student("蔡伟3号",23);

        map.put("01",s1);
        map.put("02",s2);
        map.put("03",s3);

        //1.键找值
        Set<String> set = map.keySet();
        for(String s:set){
            Student student = map.get(s);
            System.out.println(s+","+student.getName()+","+student.getAge());
        }

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

        //2.键值对对象找键和值
        Set<Map.Entry<String, Student>> entries = map.entrySet();
        for(Map.Entry<String, Student> s:entries){
            String key = s.getKey();
            Student value = s.getValue();
            System.out.println(key+","+value.getName()+","+value.getAge());
        }
    }
}

7、HashMap集合练习之键是Student值是String

image-20210818192909199

Student.java

package lesson04;

import java.util.Objects;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/18 19:29
 * FileName: Student
 * Description:
 */
public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(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 boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

HashMapDemo.java

package lesson04;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/18 19:29
 * FileName: HashMapDemo
 * Description:
 */
public class HashMapDemo {
    public static void main(String[] args) {
        HashMap<Student, String> map = new HashMap<Student, String>();

        Student s1 = new Student("蔡伟1号", 2);
        Student s2 = new Student("蔡伟2号", 3);
        Student s3 = new Student("蔡伟3号", 4);
        Student s4 = new Student("蔡伟3号", 4);

        map.put(s1, "上海");
        map.put(s2, "南通");
        map.put(s3, "武汉");
        map.put(s4, "北京");

//        1.键找值。
        Set<Student> sets = map.keySet();
        for (Student s : sets) {
            String t = map.get(s);
            System.out.println(s.getName() + "," + s.getAge() + "," + t);
        }
    }
}

8、集合嵌套之ArrayList嵌套HashMap

image-20210818194236973

ArrayListIncludeHashMapDemo

package lesson05;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/18 19:43
 * FileName: ArrayListIncludeHashMapDemo
 * Description:
 */
public class ArrayListIncludeHashMapDemo {
    public static void main(String[] args) {
        ArrayList<HashMap<String,String>> array = new ArrayList<HashMap<String,String>>();

        HashMap<String,String> map1=new HashMap<String,String>();
        map1.put("孙策","大乔");
        map1.put("周瑜","小乔");
        array.add(map1);

        HashMap<String,String> map2=new HashMap<String,String>();
        map2.put("郭靖","黄蓉");
        map2.put("杨过","小龙女");
        array.add(map2);

        HashMap<String,String> map3=new HashMap<String,String>();
        map3.put("赛尔号","蔡伟");
        map3.put("我","你");
        array.add(map3);

        for(HashMap<String,String> s:array){
            Set<String> set = s.keySet();
            for(String t:set){
                String s1 = s.get(t);
                System.out.println(t+","+s1);
            }
        }
    }
}

9、集合嵌套之HashMap嵌套ArrayList

image-20210818195409113

HashMapIncludeArrayList

package lesson05;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/18 19:53
 * FileName: HashMapIncludeArrayList
 * Description:
 */
public class HashMapIncludeArrayList {
    public static void main(String[] args) {
        HashMap<String,ArrayList<String>> map=new HashMap<String,ArrayList<String>>();

        ArrayList<String> array1 = new ArrayList<>();
        array1.add("诸葛亮");
        array1.add("赵云");
        map.put("三国演义",array1);

        ArrayList<String> array2 = new ArrayList<>();
        array2.add("孙悟空");
        array2.add("猪八戒");
        map.put("西游记",array2);

        ArrayList<String> array3 = new ArrayList<>();
        array3.add("武松");
        array3.add("鲁智深");
        map.put("水浒传",array3);

        Set<String> set = map.keySet();
        for(String key:set){
            System.out.println(key);
            ArrayList<String> value = map.get(key);
            for(String s:value){
                System.out.println("\t"+s);
            }
        }
    }
}

10、统计字符串中每个字符出现的次数

image-20210818200536543

image-20210818200650636

package lesson05;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/18 20:07
 * FileName: HashMapDemo
 * Description:
 */
public class HashMapDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String line = sc.nextLine();

//        HashMap<Character, Integer> map = new HashMap<>();
        TreeMap<Character, Integer> map = new TreeMap<>();
        for (int i = 0; i < line.length(); i++) {
            char c = line.charAt(i);
            Integer value = map.get(c);

            if(value==null){
                map.put(c,1);
            }else{
                value++;
                map.put(c,value);
            }
        }

        StringBuilder sb = new StringBuilder();
        Set<Character> set = map.keySet();
        for(Character key:set){
            Integer value = map.get(key);
            sb.append(key).append("(").append(value).append(")");
        }
        String s = sb.toString();
        System.out.println(s);
    }
}

11、Collections概述和使用

image-20210818202149816

package lesson01;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/18 20:22
 * FileName: CollectionsDemo01
 * Description:
 */
public class CollectionsDemo01 {
    public static void main(String[] args) {
        List<Integer> list=new ArrayList<Integer>();

        list.add(20);
        list.add(30);
        list.add(10);
        list.add(50);
        list.add(40);

//        Collections.sort(list);
//        Collections.reverse(list);
        Collections.shuffle(list);
        
        System.out.println(list);
    }
}

12、ArrayList集合存储学生并排序

image-20210818202730525

package lesson01;

import com.sun.security.jgss.GSSUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/18 20:27
 * FileName: CollectionsDemo02
 * Description:
 */
public class CollectionsDemo02 {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();

        Student s1 = new Student("赛尔号", 1);
        Student s2 = new Student("蔡伟1号", 10);
        Student s3 = new Student("蔡伟2号", 20);
        Student s4 = new Student("蔡伟3号", 30);

        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);

        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num = s1.getAge() - s2.getAge();
                int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
                return num2;
            }
        });

        for (Student s : list) {
            System.out.println(s.getName() + "," + s.getAge());
        }
    }
}
这篇关于Java自学-Map集合的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!