zoukankan      html  css  js  c++  java
  • java学习map集合训练

    题目

    1、手动输入一串无序的英文字母输出每个字母的出现的个数

    2、根据817日练习题使用hashmap实现购物车功能

    3、某中学刚结束期末考试一班五名同学的成绩出来了成绩包括语文数学英语综合体育音乐根据学生姓名查询该学生所有成绩

    4、已知一个 HashMap<IntegerUser>集合, User nameString)和 ageint)属性。请写一个方法实现对HashMap 的排序功能,该方法接收 HashMap<IntegerUser>为形参,返回类型为 HashMap<IntegerUser>,要求对 HashMap 中的 User age 倒序进行排序。排序时 key=value 键值对不得拆散。

    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("");
            
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            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<ShangP> 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<String, gouwuche> str=new HashMap<String, gouwuche>();
            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<Entry<String, gouwuche>> entrySet = str.entrySet();
                        if (s.equals("结账")) {
                            for (Entry<String, gouwuche> st:entrySet) {
                                sum+=(st.getValue().getPrice())*(st.getValue().getNum());
                                isok=true;
                            }
    
                        }
    
                        break;
                    }
    
                }
                if(isok){
            System.out.println("您需要支付" + sum);
                }
            }
    
        }
    
    
    
        public static void chou() {
            ArrayList<ShangP> list = liebiao();
            int a = (int) (Math.random()*list.size());
            
            System.out.println("抽到了"+list.get(a));
            System.out.println();
    
        }
    
        public static ArrayList<ShangP> liebiao() {
            ArrayList<ShangP> list = new ArrayList<ShangP>();
            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<String, Student> list=new HashMap<String, Student>();
            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<Entry<String, Student>> entrySet = list.entrySet();
            for(Entry<String, Student> 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<User> {
        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<Integer, User> sortt(HashMap<Integer, User> hash) {
    
            Set<Entry<Integer, User>> entrySet = hash.entrySet();
    
            // 套一个List集合
            List<Entry<Integer, User>> list = new ArrayList<Entry<Integer, User>>(entrySet);
    
            Collections.sort(list,new Comparator<Entry<Integer, User>>() {
    
                @Override
                public int compare(Entry<Integer, User> o1, Entry<Integer, User> o2) {
                    return o2.getValue().age-o1.getValue().age;
                }
                
            });
            LinkedHashMap<Integer, User> linkedHashMap = new LinkedHashMap<Integer, User>();
            for(Entry<Integer,User> 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<Integer, User> hash=new HashMap<>();
        hash.put(1, u1);
        hash.put(2, u2);
        hash.put(3, u3);
        
        
        User u=new User();
        
        
        Set<Entry<Integer, User>> entrySet = u.sortt(hash).entrySet();
        for(Entry<Integer, User> d:entrySet)
        {
            System.out.println(d);
        }
    }
        
    //    public static void main(String[] args) {
    //        LinkedHashMap<String,String> map = new LinkedHashMap<String,String>();
    //        map.put("1","aaa");
    //        map.put("3","bbb");
    //        map.put("2","ccc");
    //        map.put("5","ddd");
    //        List<Map.Entry<String, String>> infoIds =new ArrayList<Map.Entry<String, String>>(map.entrySet());
    //        //排序
    //        Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
    //            public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
    //                String p1 =  o1.getKey();
    //                String p2 = o2.getKey();;
    //                return Integer.valueOf(p2)-Integer.valueOf(p1);//如果要升序, 改为return Integer.valueOf(p1)-Integer.valueOf(p2);
    //            }
    //        });
    //       //转换成新map输出
    //        LinkedHashMap<String, String> newMap = new LinkedHashMap <String, String>();
    //
    //        for(Map.Entry<String,String> 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集合
            ArrayList<String> li11=new ArrayList<String>();
            ArrayList<String> li12=new ArrayList<String>();
            
            //市级对应区级,map集合
            HashMap<String,ArrayList<String>> li21=new HashMap<String,ArrayList<String>>();
            HashMap<String,ArrayList<String>> li22=new HashMap<String,ArrayList<String>>();
            
            //区级,list集合
            ArrayList<HashMap<String,ArrayList<String>>> li31=new ArrayList<HashMap<String,ArrayList<String>>>();
            
            //省级,map集合
            HashMap<String, ArrayList<HashMap<String,ArrayList<String>>>> list=new HashMap<String,ArrayList<HashMap<String,ArrayList<String>>>>();
            li11.add("船营区");
            li11.add("丰满区");
            li21.put("长春市",li11);
            li12.add("船营区");
            li12.add("丰满区");
            li22.put("吉林市",li12);
            li31.add(li21);
            li31.add(li22);
            list.put("吉林省", li31);
            Set<Entry<String, ArrayList<HashMap<String, ArrayList<String>>>>> entrySet = list.entrySet();
            for(Entry<String, ArrayList<HashMap<String, ArrayList<String>>>> d:entrySet)
            {
                System.out.println(d);
            }
            
            
        }
    }
  • 相关阅读:
    MySQL——MySQL用户与数据库的关系
    Kafka——Kakfa的设计思想
    Hibernate建表——将MySQL的JSON列映射到Java字段(Jpa/Hibernate——Java)
    Spring Boot——Apache Commons包作用说明
    word转html工具
    html导出word
    html导出pdf
    获取dubbo-admin.war(自己maven编译)
    ajax提交表单中文呈现乱码解决
    JVM优化
  • 原文地址:https://www.cnblogs.com/lumc5/p/15161533.html
Copyright © 2011-2022 走看看