java Map练习

前提是你 提交于 2020-04-08 14:49:08

描述学生,map容器,学生为键,地址为值,获取map中的元素。

public class MapDemo {
    public static void main(String[] args) {
        HashMap<Student, String> hm = new HashMap<Student, String>();
        hm.put(new Student("lisi1", 1), "北京");
        hm.put(new Student("lisi2", 2), "上海");
        hm.put(new Student("lisi3", 3), "天津");
        hm.put(new Student("lisi4", 4), "武汉");
        //第一种取出方式 keySet
        Set<Student> keySet = hm.keySet();
        Iterator<Student> it = keySet.iterator();
        while (it.hasNext()) {
            Student stu = it.next();
            String addr = hm.get(stu);
            System.out.println(stu + "--" + addr);
        }
        //第二种取出方式 entrySet
        Set<Map.Entry<Student, String>> entrySet = hm.entrySet();
        Iterator<Map.Entry<Student, String>> i = entrySet.iterator();
        while (i.hasNext()) {
            Map.Entry<Student, String> stu = i.next();
            System.out.println(stu.getKey() + "--" + stu.getValue());
        }

    }
}

class Student implements Comparable<Student> {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Student))
            throw new ClassCastException("类型不匹配");
        Student s = (Student) obj;
        return this.name.equals(s.name) && this.age == s.age;
    }

    @Override
    public int hashCode() {
        return name.hashCode() + age * 34;
    }

    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 String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        int num = new Integer(this.age).compareTo(new Integer(o.age));
        if (num == 0) {
            return this.name.compareTo(o.name);
        }
        return num;
    }
}

 

排序年龄:

public class MapDemo {
    public static void main(String[] args) {
        TreeMap<Student, String> tm = new TreeMap<Student, String>();
        tm.put(new Student("lisi4", 1), "武汉");
        tm.put(new Student("lisi1", 4), "北京");
        tm.put(new Student("lisi3", 2), "天津");
        tm.put(new Student("lisi2", 3), "上海");

        Set<Map.Entry<Student, String>> entrySet = tm.entrySet();
        Iterator<Map.Entry<Student, String>> it = entrySet.iterator();
        while (it.hasNext()) {
            Map.Entry<Student, String> stu = it.next();
            System.out.println(stu.getKey() + "--" + stu.getValue());
        }

    }
}

class Student implements Comparable<Student> {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Student))
            throw new ClassCastException("类型不匹配");
        Student s = (Student) obj;
        return this.name.equals(s.name) && this.age == s.age;
    }

    @Override
    public int hashCode() {
        return name.hashCode() + age * 34;
    }

    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 String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        int num = new Integer(this.age).compareTo(new Integer(o.age));
        if (num == 0) {
            return this.name.compareTo(o.name);
        }
        return num;
    }
}

姓名排序:

public class MapDemo {
    public static void main(String[] args) {
        TreeMap<Student, String> tm = new TreeMap<Student, String>(new stuComparator());
        tm.put(new Student("lisi4", 1), "武汉");
        tm.put(new Student("lisi1", 4), "北京");
        tm.put(new Student("lisi3", 2), "天津");
        tm.put(new Student("lisi2", 3), "上海");

        Set<Map.Entry<Student, String>> entrySet = tm.entrySet();
        Iterator<Map.Entry<Student, String>> it = entrySet.iterator();
        while (it.hasNext()) {
            Map.Entry<Student, String> stu = it.next();
            System.out.println(stu.getKey() + "--" + stu.getValue());
        }

    }
}

class Student implements Comparable<Student> {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Student))
            throw new ClassCastException("类型不匹配");
        Student s = (Student) obj;
        return this.name.equals(s.name) && this.age == s.age;
    }

    @Override
    public int hashCode() {
        return name.hashCode() + age * 34;
    }

    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 String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        int num = new Integer(this.age).compareTo(new Integer(o.age));
        if (num == 0) {
            return this.name.compareTo(o.name);
        }
        return num;
    }
}

class stuComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        int num = o1.getName().compareTo(o2.getName());
        if (num == 0)
            return new Integer(o1.getAge()).compareTo(new Integer(o2.getAge()));
        return num;
    }
}

 

获取字符串("aabbbcccccdfff")中字母出现的次数:

希望打印结果:a(1)c(2)......

有映射关系先想到map集合。

1.将字符串转换成字符数组,因为要对每一个字母进行操作

2.定义map集合,打印结果有顺序,使用treemap

3.遍历字符数组,将每一个字母作为键去查map集合,返回null,就将该字母和1存入map集合中,返回不是null,获取次数加1,在存入集合,覆盖原来的键值对。

4.将map集合的数据变成指定的字符串形式返回。

public class Demo {
    public static void main(String[] args) {
        String s = charCount("aa+++___bbbcccccdfff");
        System.out.println(s);
    }

    public static String charCount(String str) {
        char[] chs = str.toCharArray();
        TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
        int count = 0;
        for (int i = 0; i < chs.length; i++) {
            if (!(chs[i]>='a' && chs[i]<='z' || chs[i]>='A' && chs[i]<='Z'))
                continue;
            Integer value = tm.get(chs[i]);
            if (value != null)
                count = value;
            count++;
            tm.put(chs[i], count);
            count = 0;
        }
        StringBuilder sb = new StringBuilder();
        Set<Map.Entry<Character, Integer>> entrySet = tm.entrySet();
        Iterator<Map.Entry<Character, Integer>> it = entrySet.iterator();
        while (it.hasNext()) {
            Map.Entry<Character, Integer> me = it.next();
            Character ch = me.getKey();
            Integer value = me.getValue();
            sb.append(ch + "(" + value + ")");
        }
        return sb.toString();
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!