1、使用List,map容器存放如下数据,并从map中取出“李四”
姓名:张三 年龄:18 体重:90 地址:北京
姓名:李四 年龄:28 体重:50 地址:上海
package MapHomeWork;
public class Person {
private String name;
private int age;
private int weight;
private String address;
public Person() {
}
public Person(String name, int age, int weight, String address) {
this.name = name;
this.age = age;
this.weight = weight;
this.address = address;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", weight=" + weight +
", address='" + address + '\'' +
'}';
}
}
package MapHomeWork;
import java.util.HashMap;
import java.util.Map;
public class MapDemo {
public static void main(String[] args) {
Map<String,Person> map1 = new HashMap<String,Person>();
map1.put("张三",new Person("张三",18,80,"北京"));
map1.put("李四",new Person("李四",20,60,"上海"));
System.out.println(map1.get("张三"));
}
}
package MapHomeWork;
import java.util.ArrayList;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
List<Person> list = new ArrayList<Person>();
list.add(new Person("张三",18,80,"北京"));
list.add(new Person("李四",20,60,"上海"));
}
}
1、假如有以下email数据
“aa@sohu.com,bb@163.com,cc@sina.com”,现需要把email中的用户部分和邮件地址部分分离,分离后以键值对应的方式放入HashMap?
package MapHomeWork;
import java.util.HashMap;
import java.util.Set;
public class EmailSplit {
public static void main(String[] args) {
String addresses = "aa@sohu.com,bb@163.com,cc@sina.com";
String[] address = addresses.split(",");
HashMap<String,String> hashMap = new HashMap<String, String>();
for(String str : address){
String[] flag = str.split("@");
hashMap.put(flag[0],flag[1]);
}
Set<String> keys = hashMap.keySet();
for(String key:keys){
System.out.println(key+"的地址是"+hashMap.get(key));
}
}
}