java学习map集合训练
题目
1、手动输入一串无序的英文字母,输出每个字母的出现的个数
2、根据8月17日练习题,使用hashmap实现购物车功能
3、某中学刚结束期末考试,一班五名同学的成绩出来了,成绩包括语文、数学、英语、综合、体育、音乐,根据学生姓名查询该学生所有成绩。
4、已知一个 HashMap
5、实现省市区三级查询,例如吉林省———长春市———朝阳区,一个省有多个市,一个市有多个区,输入省名称,显示所有的市名称,每个市又显示出所有的区(最后做,不强制要求)
解答:
第一题: package homework; import java.util.HashMap; import java.util.HashSet; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); String[] mm = s.split(""); HashMapmap = new HashMap (); for (int i = 1; i < mm.length; i++) { if (map.get(mm[i])!=null) { map.put(mm[i],map.get(mm[i]) + 1); } else { map.put(mm[i], 1); } } for (String d:map.keySet()) { System.out.println(d+"="+map.get(d)); } } } 第二题: package homework; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; public class ShangCheng { public static void main(String[] args) { ArrayList list = liebiao(); for (ShangP s : list) { System.out.println(s.toString()); } System.out.println("**价格高于100的有**"); for (ShangP n : list) { if (n.getPrice() > 100) { System.out.println(n); } } System.out.println("**产地是北京的有**"); for (ShangP n : list) { if (n.getAddress().equals("北京")) { System.out.println(n); } } System.out.println("**排序**"); Collections.sort(list); for (ShangP n : list) { System.out.println(n.toString()); } System.out.println("**请抽奖**"); chou(); System.out.println("**请购物**"); double sum = 0; HashMap str=new HashMap (); while (true) { boolean isok=false; System.out.print("请输入购买物品"); String name = new Scanner(System.in).next(); System.out.print("请输入购买数量"); int num = new Scanner(System.in).nextInt(); for (ShangP l : list) { if (l.getName().equals(name)) { gouwuche gg = new gouwuche(name, num, l.getPrice()); str.put(name, gg); System.out.println("是否继续购买(是/结账)"); String s = new Scanner(System.in).next(); Set > entrySet = str.entrySet(); if (s.equals("结账")) { for (Entry st:entrySet) { sum+=(st.getValue().getPrice())*(st.getValue().getNum()); isok=true; } } break; } } if(isok){ System.out.println("您需要支付" + sum); } } } public static void chou() { ArrayList list = liebiao(); int a = (int) (Math.random()*list.size()); System.out.println("抽到了"+list.get(a)); System.out.println(); } public static ArrayList liebiao() { ArrayList list = new ArrayList (); SW a1 = new SW("食物1", 12, "好", "大龙厂", "浙江", 9, 90); SW a2 = new SW("食物2", 12, "好", "大龙厂", "浙江", 9, 90); SW a3 = new SW("食物3", 12, "好", "大龙厂", "浙江", 9, 90); SW a4 = new SW("食物4", 12, "好", "大龙厂", "浙江", 9, 90); SW a5 = new SW("食物5", 12, "好", "大龙厂", "浙江", 9, 90); DQ b1 = new DQ("空调1", 10000, "好", "美的", "黑龙江", 220, 2500, 300); DQ b2 = new DQ("空调2", 10000, "好", "美的", "黑龙江", 220, 2500, 300); DQ b3 = new DQ("空调3", 10000, "好", "美的", "黑龙江", 220, 2500, 300); DQ b4 = new DQ("空调4", 10000, "好", "美的", "黑龙江", 220, 2500, 300); DQ b5 = new DQ("空调5", 10000, "好", "美的", "黑龙江", 220, 2500, 300); FZ c1 = new FZ("巴黎世家1", 1000, "优", "巴黎世家工厂", "广州", "涤纶", "棉麻", "布,水,线"); FZ c2 = new FZ("巴黎世家2", 1000, "优", "巴黎世家工厂", "广州", "涤纶", "棉麻", "布,水,线"); FZ c3 = new FZ("巴黎世家3", 1000, "优", "巴黎世家工厂", "广州", "涤纶", "棉麻", "布,水,线"); FZ c4 = new FZ("巴黎世家4", 1000, "优", "巴黎世家工厂", "广州", "涤纶", "棉麻", "布,水,线"); FZ c5 = new FZ("巴黎世家5", 1000, "优", "巴黎世家工厂", "北京", "涤纶", "棉麻", "布,水,线"); WJ d1 = new WJ("泡泡机1", 200, "差", "江南皮革厂", "江苏", "皮革", "益智玩具", 3); WJ d2 = new WJ("泡泡机2", 200, "差", "江南皮革厂", "北京", "皮革", "益智玩具", 3); WJ d3 = new WJ("泡泡机3", 200, "差", "江南皮革厂", "江苏", "皮革", "益智玩具", 3); WJ d4 = new WJ("泡泡机4", 200, "差", "江南皮革厂", "江苏", "皮革", "益智玩具", 3); WJ d5 = new WJ("泡泡机5", 200, "差", "江南皮革厂", "江苏", "皮革", "益智玩具", 3); list.add(a1); list.add(a2); list.add(a3); list.add(a4); list.add(a5); list.add(b1); list.add(b2); list.add(b3); list.add(b4); list.add(b5); list.add(c1); list.add(c2); list.add(c3); list.add(c4); list.add(c5); list.add(d1); list.add(d2); list.add(d3); list.add(d4); list.add(d5); return list; } } 第三题,package homework2; public class Student { private double YW; private double SX; private double Y; private double ZH; private double TY; private double YY; public Student(double yW, double sX, double y, double zH, double tY, double yY) { super(); YW = yW; SX = sX; Y = y; ZH = zH; TY = tY; YY = yY; } /** * @return the yW */ public double getYW() { return YW; } /** * @param yW the yW to set */ public void setYW(double yW) { YW = yW; } /** * @return the sX */ public double getSX() { return SX; } /** * @param sX the sX to set */ public void setSX(double sX) { SX = sX; } /** * @return the y */ public double getY() { return Y; } /** * @param y the y to set */ public void setY(double y) { Y = y; } /** * @return the zH */ public double getZH() { return ZH; } /** * @param zH the zH to set */ public void setZH(double zH) { ZH = zH; } /** * @return the tY */ public double getTY() { return TY; } /** * @param tY the tY to set */ public void setTY(double tY) { TY = tY; } /** * @return the yY */ public double getYY() { return YY; } /** * @param yY the yY to set */ public void setYY(double yY) { YY = yY; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Student [YW=" + YW + ", SX=" + SX + ", Y=" + Y + ", ZH=" + ZH + ", TY=" + TY + ", YY=" + YY + "]"; } } package homework2; import java.util.HashMap; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; public class Test2 { public static void main(String[] args) { System.out.println(132); HashMap list=new HashMap (); list.put("一号", new Student(90, 99, 90, 99, 90, 99)); list.put("二号", new Student(90, 99, 90, 99, 90, 99)); list.put("三号", new Student(90, 99, 90, 99, 90, 99)); list.put("四号", new Student(90, 99, 90, 99, 90, 99)); list.put("五号", new Student(90, 99, 90, 99, 90, 99)); String name=new Scanner(System.in).next(); Set > entrySet = list.entrySet(); for(Entry ent:entrySet) { if(name.equals(ent.getKey())) System.out.println(ent); } } } 第四题: package homework4; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class User implements Comparable { private String name; private int age; public User(String name, int age) { super(); this.name = name; this.age = age; } /** * @return the name */ public String getName() { return name; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public int getAge() { return age; } /** * @param age * the age to set */ public void setAge(int age) { this.age = age; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } public User() { super(); } public HashMap sortt(HashMap hash) { Set > entrySet = hash.entrySet(); // 套一个List集合 List > list = new ArrayList >(entrySet); Collections.sort(list,new Comparator >() { @Override public int compare(Entry o1, Entry o2) { return o2.getValue().age-o1.getValue().age; } }); LinkedHashMap linkedHashMap = new LinkedHashMap (); for(Entry entity : list){ linkedHashMap.put(entity.getKey(), entity.getValue()); System.out.println(entity.getKey()); } return linkedHashMap; } private User getValue() { return null; } @Override public int compareTo(User o) { if (this.getAge() > o.getAge()) return -1; else if (this.getAge() < o.getAge()) return 1; else return 0; } } package homework4; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class Testuser { public static void main(String[] args) { User u1=new User("王", 12); User u2=new User("张", 16); User u3=new User("孙", 10); HashMap hash=new HashMap<>(); hash.put(1, u1); hash.put(2, u2); hash.put(3, u3); User u=new User(); Set > entrySet = u.sortt(hash).entrySet(); for(Entry d:entrySet) { System.out.println(d); } } // public static void main(String[] args) { // LinkedHashMap map = new LinkedHashMap // map.put("1","aaa"); // map.put("3","bbb"); // map.put("2","ccc"); // map.put("5","ddd"); // List(); > infoIds =new ArrayList // //排序 // Collections.sort(infoIds, new Comparator>(map.entrySet()); >() { // public int compare(Map.Entryo1, Map.Entry // String p1 = o1.getKey(); // String p2 = o2.getKey();; // return Integer.valueOf(p2)-Integer.valueOf(p1);//如果要升序, 改为return Integer.valueOf(p1)-Integer.valueOf(p2); // } // }); // //转换成新map输出 // LinkedHashMapo2) { newMap = new LinkedHashMap // // for(Map.Entry(); entity : infoIds){ // newMap.put(entity.getKey(), entity.getValue()); // System.out.println(entity.getKey()); // } // } } 第五题: package homework5; public class City { private String name; /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } public City(String name) { super(); this.name = name; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "City [name=" + name + "]"; } } package homework5; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class Test { public static void main(String[] args) { //**注意map中元素不可重复** //区级list集合 ArrayListli11=new ArrayList (); ArrayList li12=new ArrayList (); //市级对应区级,map集合 HashMap > li21=new HashMap >(); HashMap > li22=new HashMap >(); //区级,list集合 ArrayList >> li31=new ArrayList >>(); //省级,map集合 HashMap >>> list=new HashMap >>>(); li11.add("船营区"); li11.add("丰满区"); li21.put("长春市",li11); li12.add("船营区"); li12.add("丰满区"); li22.put("吉林市",li12); li31.add(li21); li31.add(li22); list.put("吉林省", li31); Set >>>> entrySet = list.entrySet(); for(Entry >>> d:entrySet) { System.out.println(d); } } }