Java语法基础50题训练(下)

Posted Wecccccccc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java语法基础50题训练(下)相关的知识,希望对你有一定的参考价值。

题目1:
HashMap集合存储学生对象并遍历。

需求:
创建一个HashMap集合,键是学生对象(Student),值是居住地(String)。存储多个键值对象,并遍历。

要求:
保证键的唯一性:如果学生对象的成员变量值相同,我们就认为是同一个对象。

代码如下:

package HashMapPrintPack02;

public class Student {
    private String name;
    private int age;

    public Student() {
    }

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

    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 boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }
}

package HashMapPrintPack02;

import java.util.HashMap;
import java.util.Set;

public class HashMapDemo {
    public static void main(String[] args)
    {
        HashMap<Student,String> hs = new HashMap<Student,String>();

        Student s1 = new Student("Tom",30);
        Student s2 = new Student("Jack",28);
        Student s3 = new Student("Lily",17);
        Student s4 = new Student("Tom",30);

        hs.put(s1,"西安");
        hs.put(s2,"北京");
        hs.put(s3,"上海");
        hs.put(s4,"深圳");

        Set<Student> keySet = hs.keySet();
        for (Student key:keySet)
        {
            String value = hs.get(key);
            System.out.println(key.getName()+","+key.getAge()+","+value);
        }
    }

}

题目2:
ArrayList集合存储HashMap元素并遍历。

需求:
创建一个ArrayList集合,存储三个元素,每一个元素都是HashMap,每一个HashMap的键和值都是String,并遍历。

代码如下:

package ArrayListAndHashMapPack01;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class ArrayListIncludeHashMapDemo {
    public static void main(String[] args)
    {
        ArrayList<HashMap<String,String>> array = new ArrayList<HashMap<String, String>>();

        HashMap<String,String> hm1 = new HashMap<String,String>();
        hm1.put("Tom","Jack");
        hm1.put("Bom","Lily");
        array.add(hm1);

        HashMap<String,String> hm2 = new HashMap<String,String>();
        hm1.put("小明","小红");
        hm1.put("小兰","小绿");
        array.add(hm2);

        HashMap<String,String> hm3 = new HashMap<String,String>();
        hm1.put("Mike","Zliy");
        hm1.put("Bo","Db");
        array.add(hm3);

        for(HashMap<String,String> hm:array)
        {
            Set<String> keySet = hm.keySet();
            for (String key:keySet)
            {
                String value = hm.get(key);
                System.out.println(key+","+value);
            }
        }
    }
}

题目3:
HashMap集合存储ArrayList元素并遍历。

需求:
创建一个HashMap集合,存储三个键值对元素,每一个键值对元素的键是String,值是ArrayList,每一个ArrayList的元素是String,并遍历。

代码如下:

package HashMapIncludeArrayListPack;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class HashMapIncludeArrayListDemo {
    public static void main(String[] args)
    {
        HashMap<String, ArrayList<String>> hm = new HashMap<String,ArrayList<String>>();

        ArrayList<String> sgyy = new ArrayList<String>();

        sgyy.add("赵云");
        sgyy.add("刘备");
        hm.put("三国演义",sgyy);


        ArrayList<String> xyj = new ArrayList<String>();

        xyj.add("唐僧");
        xyj.add("猴哥");
        hm.put("西游记",xyj);


        ArrayList<String> hlm = new ArrayList<String>();

        hlm.add("贾宝玉");
        hlm.add("林黛玉");
        hm.put("红楼梦",hlm);

        Set<String> keySet = hm.keySet();
        for (String key:keySet)
        {
            System.out.println(key);
            ArrayList<String> value = hm.get(key);
            for (String s:value)
            {
                System.out.println("\\t"+s);
            }
        }
    }

}

题目4:
统计字符串中每个字符串出现的次数。

需求:
键盘录入一个字符串,要求统计字符串中每个字符串出现的次数。
举例:
键盘录入"aababcabcdabcde",在控制台输出:“a(5)b(4)c(3)d(2)e(1)”

代码如下:

package CountStringNumPack;

import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;

public class HashMapDemp {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String line = sc.nextLine();

        HashMap<Character,Integer> hm = new HashMap<Character,Integer>();

        for (int i  = 0;i<line.length();i++)
        {
            char key = line.charAt(i);

            Integer value = hm.get(key);

            if (value==null)
            {
                hm.put(key,1);
            }
            else {
                value++;
                hm.put(key, value);
            }
        }

        StringBuilder sb = new StringBuilder();

        Set<Character> keySet = hm.keySet();
        for (Character key:keySet)
        {
            Integer value = hm.get(key);
            sb.append(key).append("(").append(value).append(")");
        }

        String ans = sb.toString();

        System.out.println(ans);
    }

}

package CountStringNumPack;


import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

public class HashMapDemp {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String line = sc.nextLine();

        TreeMap<Character,Integer> hm = new TreeMap<Character,Integer>();

        for (int i  = 0;i<line.length();i++)
        {
            char key = line.charAt(i);

            Integer value = hm.get(key);

            if (value==null)
            {
                hm.put(key,1);
            }
            else {
                value++;
                hm.put(key, value);
            }
        }

        StringBuilder sb = new StringBuilder();

        Set<Character> keySet = hm.keySet();
        for (Character key:keySet)
        {
            Integer value = hm.get(key);
            sb.append(key).append("(").append(value).append(")");
        }

        String ans = sb.toString();

        System.out.println(ans);
    }

}

题目5:
ArrayList存储学生对象并排序。

需求:
ArrayList存储学生对象,使用Collections对ArrayList进行排序。
要求:
按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序。

代码如下:

package CollectionsDemo02;

public class Student {
    private String name;
    private int age;

    public Student() {
    }

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

    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;
    }


}

package CollectionsDemo02;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class CollectionsDemo02 {
    public static void main(String[] args)
    {
        ArrayList<Student> array = new ArrayList<Student>();

        Student s1 = new Student("Tom",18);
        Student s2 = new Student("Jack",20);
        Student s3 = new Student("Bom",29);
        Student s4 = new Student("Lily",23);

        array.add(s1);
        array.add(s2);
        array.add(s3);
        array.add(s4);

        Collections.sort(array, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num = s1.getAge()-s2.getAge();
                int num2 = num==0?s1.getName().compareTo(s2.getName()):num;
                return num2;
            }
        });

        for (Student s:array)
        {
            System.out.println(s.getName()+","+s.getAge(2019年最新50道java基础部分面试题

202106小白训练营day02-java基础语法

java小白训练营day02-开发环境:基础语法:关键字+变量+常量+运算符+jUnit

50道最新java基础部分面试题

2019年最新50道java基础部分面试题

JSP 基础语法