打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java Map练习

描述学生,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();    }}

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Student
java基础第十四篇之Map
Java开发之集合框架Map
派生类
ASP.NET Web API快速入门介绍
Java中如何取出Map集合中的元素
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服