1.键盘录入一个字符串,求该字符串中每一个字符出现的次数。
要求:按照字母顺序打印
如: 录入的字符串为"apple",打印 a(1) e(1) l(1) p(2)
public class Demo4 {
public static void main(String[] args) {
//键盘录入
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串");
String str = sc.nextLine();
//创建treeMap存储字符【键】,个数【值】
TreeMap<Character, Integer> map = new TreeMap<>();
//遍历输入的字符串
//方法一:str.length(), str.charAt(i)得到每一个字符
for (int i = 0; i < str.length(); i++) {
//得到每一个字符
char ch = str.charAt(i);
//判断键在集合中是否存在
if(map.containsKey(ch)){
//如果存在,则获取对应的值
Integer value = map.get(ch);
//将值+1并存入集合中
map.put(ch,value+1);
}//否则不存在,则值为1,直接存入
else {
map.put(ch,1);
}
}
//方法二:s
/*char[] charArr = str.toCharArray();
for (int i = 0; i < charArr.length; i++) {
if(map.containsKey(charArr[i])){
//如果存在,则获取对应的值
Integer value = map.get(charArr[i]);
//将值+1并存入集合中
map.put(charArr[i],value+1);
}//否则不存在,则值为1,直接存入
else {
map.put(charArr[i],1);
}
}*/
//打印集合
//录入的字符串为"apple",打印 a(1) e(1) l(1) p(2)
Set<Map.Entry<Character, Integer>> entries = map.entrySet();
for (Map.Entry<Character, Integer> entry : entries) {
System.out.print(entry.getKey()+"("+entry.getValue()+")"+" ");
}
System.out.println();
}
}
打印结果:
-----------------------------------------------------------------
请输入字符串
asdagfasfas
a(4) d(1) f(2) g(1) s(3)
使用HashMap集合存储Student对象作为键,学生的家庭住址作为值
要求:要求学生的姓名和年龄不能重复
使用TreeMap集合存储Student对象作为键,学生的家庭住址作为值
要求:先按照年龄进行排序,再按照姓名进行排序
测试类:
public class Demo3 {
public static void main(String[] args) {
//1. 使用HashMap集合存储Student对象作为键,学生的家庭住址作为值
// 要求:要求学生的姓名和年龄不能重复
HashMap<Student, String> hashMap = new HashMap<>();
hashMap.put(new Student("张三",20),"武汉");
hashMap.put(new Student("李四",21),"孝感");
hashMap.put(new Student("王五",20),"武汉");
hashMap.put(new Student("张三",22),"安陆");
hashMap.put(new Student("张三",20),"汉川");
//键值对遍历
Set<Map.Entry<Student, String>> entries = hashMap.entrySet();
for (Map.Entry<Student, String> entry : entries) {
System.out.println(entry.getKey()+"..."+entry.getValue());
}
//2. 使用TreeMap集合存储Student对象作为键,学生的家庭住址作为值
// 要求:先按照年龄进行排序,再按照姓名进行排序
System.out.println("----------------------");
TreeMap<Student, String> treeMap = new TreeMap<>();
treeMap.put(new Student("zhangsan",20),"武汉");
treeMap.put(new Student("lisi",21),"孝感");
treeMap.put(new Student("wangwu",20),"武汉");
treeMap.put(new Student("zhangsan",22),"安陆");
treeMap.put(new Student("zhangsan",20),"汉川");
//键找值遍历
Set<Student> keys = treeMap.keySet();
for (Student key : keys) {
String value = treeMap.get(key);
System.out.println(key+"..."+value);
}
}
}
打印结果:
---------------------------------------------------------------
Student{name='王五', age=20}...武汉
Student{name='张三', age=22}...安陆
Student{name='张三', age=20}...汉川
Student{name='李四', age=21}...孝感
----------------------
Student{name='wangwu', age=20}...武汉
Student{name='zhangsan', age=20}...汉川
Student{name='lisi', age=21}...孝感
Student{name='zhangsan', age=22}...安陆
学生类
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
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 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;
}
@Override
public int compareTo(Student o) {
int result =this.age-o.age;
result = result == 0 ? this.getName().compareTo(o.getName()) : result;
return result;
}
}