java Map练习
程序员文章站
2022-05-18 18:57:46
描述学生,map容器,学生为键,地址为值,获取map中的元素。 public class MapDemo { public static void main(String[] args) { HashMap hm = new HashMap
描述学生,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(); } }
下一篇: ElasticSearch 简介