- C
6 B - equals 里边的判断
equals方法流程有问题
hashCode方法的修饰符应该为public
if(this.name.equals(w.name) && this.salary==w.salary && this.age == w.age)
hashCode方法里面 return后面不可+salary 因为salary是double类型 不可以直接加
添加第三个对象时:Set实现类无序,无下标,所以第三个实现形式语法有问题。
8.package day25.t1;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class TestQ10_8 {
public static void main(String[] args) {
Set<Worker> set =new HashSet<Worker>();
set.add(new Worker("tom",18 , 2000));
set.add(new Worker("tom",18 , 2000));
set.add(new Worker("jerry",18 , 2000));
for(Worker w2 :set){
System.out.println(w2);
}
}
}
class Worker{
String name;
Integer age;
Double salary;
public Worker(){}
public Worker(String name, int age, double salary){
this.name=name;
this.age=age;
this.salary=salary;
}
public int hashCode(){
return this.name.hashCode()+this.age.hashCode()+this.salary.hashCode();
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Worker other = (Worker) obj;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (salary == null) {
if (other.salary != null)
return false;
} else if (!salary.equals(other.salary))
return false;
return true;
}
@Override
public String toString() {
return this.name+"\t"+this.age+"\t"+this.salary;
}
}
运行效果图:
9 . 1. 覆盖 存入
2. 1 删除
3. 得到一个键对应的内容 一个键 key
4. Set set
5. value() Collection
10.
package day25.t1;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class TestQ10_10 {
public static void main(String[] args) {
Scanner s1 =new Scanner(System.in);
Map<String , String> map =new<String , String> HashMap();
map.put("1930", "乌拉圭");
map.put("1934", "意大利");
map.put("1938", "意大利");
map.put("1950", "乌拉圭");
map.put("1954", "德国");
map.put("1958", "巴西");
map.put("1962", "巴西");
map.put("1966", "英格兰");
map.put("1970", "巴西");
map.put("1974", "德国");
map.put("1978", "阿根廷");
map.put("1982", "意大利");
map.put("1986", "阿根廷");
map.put("1990", "德国");
map.put("1994", "巴西");
map.put("1998", "法国");
map.put("2002", "巴西");
map.put("2006", "意大利");
Set<String> set =new HashSet<String>();
set=map.keySet();
System.out.println(set);
System.out.println("请输入要查询的年份");
String s = s1.next();
if(set.contains(s)){
String s3=map.get(s);
System.out.println(s+s3);
}else{
System.out.println("此年没有举行世界杯");
}
}
}
效果图:
效果图2:
11.
package day25.t1;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class TestQ10_11 {
public static void main(String[] args) {
Map<String, String> map =new HashMap<String,String>();
map.put("Tom","CoreJava");
map.put("John","Oracle");
map.put("Susan","Oracle");
map.put("Jerry","JDBC");
map.put("Jim","Unix");
map.put("Kevin","JSP");
map.put("Lucy","JSP");
map.put("Allen", "JDBC");
map.put("Lucy", "CoreJava");
Set<String> set = new HashSet<String>();
set=map.keySet();
for(String s : set){
System.out.println(s+"\t"+map.get(s));
}
System.out.println("----------------------------");
for(String s: set){
if(map.get(s).equals("JSP")){
System.out.println(s+"\t"+map.get(s));
}
}
}
}
效果图:
12. B
package day25.t1;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class TestQ10_10 {
public static void main(String[] args) {
Scanner s1 =new Scanner(System.in);
Map<String , String> map =new<String , String> HashMap();
map.put("1930", "乌拉圭");
map.put("1934", "意大利");
map.put("1938", "意大利");
map.put("1950", "乌拉圭");
map.put("1954", "德国");
map.put("1958", "巴西");
map.put("1962", "巴西");
map.put("1966", "英格兰");
map.put("1970", "巴西");
map.put("1974", "德国");
map.put("1978", "阿根廷");
map.put("1982", "意大利");
map.put("1986", "阿根廷");
map.put("1990", "德国");
map.put("1994", "巴西");
map.put("1998", "法国");
map.put("2002", "巴西");
map.put("2006", "意大利");
Set<String> set =new HashSet<String>();
set=map.keySet();
System.out.println(set);
System.out.println("请输入要查询的年份");
String s = s1.next();
if(set.contains(s)){
String s3=map.get(s);
System.out.println(s+s3);
}else{
System.out.println("此年没有举行世界杯");
}
System.out.println("请输入要查询的球队名称");
String s6 = s1.next();
boolean flag=false;
for (String s5 : set) {
if (map.containsValue(s6)) {
if (map.get(s5).equals(s6)) {
System.out.println(s5);
flag =true;
}
}
}
if(flag == false){
System.out.println("没有获得过世界杯");
}
}
}
运行效果图1:
运行效果图2:
package day25.t1;
import java.util.Scanner;
public class TestQ10_14 {
public static void main(String[] args) {
Scanner s =new Scanner(System.in);
System.out.println("请输入一段字符");
String str =s.nextLine();
for(int i = 0; i<str.length();i++){
if(str.indexOf(str.charAt(i))==i){
int count =0;
for(int j =0 ; j<str.length();j++){
if(str.charAt(j)==str.charAt(i)){
count++;
}
}
System.out.println(str.charAt(i)+"出现了"+count+"次");
}
}
}
}
效果图: