问题1
public class q1 {
public static void main(String[] args) {
Map<String, Double> m = new HashMap();
m.put("Tom", 100.0);
m.put("Jack", 200.0);
m.put("Mary", 1300.0);
m.put("Coco", 400.0);
Set<String> keySet = m.keySet();
System.out.println(keySet);
for (String key : keySet) {
double value = m.get(key);
System.out.println("姓名:" + key + " 工资:" + value);
}
m.remove("Tom");
if (m.containsKey("Jack"))
m.put("Jack", m.get("Jack") + 1000);
System.out.println(m.get("Jack"));
Set<Map.Entry<String, Double>> entries = m.entrySet();
for (Map.Entry e : entries) {
String key = e.getKey() + "";
double vlaue = (double) e.getValue();
if (vlaue < 1000) {
m.put(key, m.get(key) * 1.2);
}
}
System.out.println(m);
}
}
问题2
public class Student {
public String name;
public int age;
public Student() {
};
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class q2 {
public static void main(String[] args) {
Map<Student, String> m = new HashMap();
m.put(new Student("张三", 20), "河南郑州");
m.put(new Student("李四", 22), "湖北武汉");
m.put(new Student("王五", 18), "广东深圳");
m.put(new Student("赵六", 21), "四川成都");
Set<Student> students = m.keySet();
for(Student s : students){
String name = s.name;
int age = s.age;
System.out.printf("名字:%s 年龄:%d 地址:%s\n",name,age,m.get(s));
}
Collection<String> values = m.values();
Iterator<String> it = values.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
Set<Map.Entry<Student, String>> entries = m.entrySet();
for(Map.Entry e : entries){
Student key = (Student) e.getKey();
String value = (String) e.getValue();
System.out.printf("名字:%s 年龄:%d 地址:%s\n",key.name,key.age,value);
}
}
}
问题3
public class q3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
Map<String,Integer> m = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
boolean q1 = (s.charAt(i)>='a' && s.charAt(i) <= 'z');
boolean q2 = (s.charAt(i)>='A' && s.charAt(i) <= 'Z');
boolean q3 = (s.charAt(i)>='0' && s.charAt(i) <= '9');
if(q1 || q2 || q3){
String c = s.charAt(i)+"";
if(m.containsKey(c))
m.put(c,m.get(c)+1);
else
m.put(c,1);
}
}
System.out.println(m);
}
}