/**
* 1.尝试Map<Boy,ArrayList<GirlFriend>>
* 2.尝试Map<Student,HashSet<Book>>
* 3.尝试ArrayList(你看过的电视剧)<ArrayList<Role人物>>
* 4.假如有以下email数据“aa@sohu.com,bb@163.com,cc@sina.com”现需要把email中的用户部分和邮件地址部分分离,分离后以键值对应的方式放入HashMap?
* key value
* aa sohu.com
* bb 163.com
* cc sina.com
*/
package com.qf.home;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class Home3 {
public static void main(String[] args) {
Map<Home3.Boy, ArrayList<Home3.GirlFriend>> boyMaps = new HashMap<Home3.Boy, ArrayList<Home3.GirlFriend>>();
Home3.Boy boy1 = new Home3().new Boy("至尊宝", 18);
Home3.Boy boy2 = new Home3().new Boy("夕阳武士", 19);
ArrayList<Home3.GirlFriend> girlFriends1 = new ArrayList<Home3.GirlFriend>();
ArrayList<Home3.GirlFriend> girlFriends2 = new ArrayList<Home3.GirlFriend>();
Home3.GirlFriend girlFriend1 = new Home3().new GirlFriend("紫霞", 18, "18815276543");
Home3.GirlFriend girlFriend2 = new Home3().new GirlFriend("青霞", 19, "18815276542");
Home3.GirlFriend girlFriend3 = new Home3().new GirlFriend("兰霞", 20, "18815276541");
girlFriends1.add(girlFriend1);
girlFriends1.add(girlFriend2);
girlFriends2.add(girlFriend3);
girlFriends2.add(girlFriend3);
boyMaps.put(boy1, girlFriends1);
boyMaps.put(boy2, girlFriends2);
// 第一种:普遍使用,二次取值
System.out.println("通过Map.keySet遍历key和value:");
for (Boy key : boyMaps.keySet()) {
System.out.println("key= " + key + " and value= " + boyMaps.get(key));
}
// 第二种
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<Home3.Boy, ArrayList<Home3.GirlFriend>>> it = boyMaps.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Home3.Boy, ArrayList<Home3.GirlFriend>> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
// 第三种:推荐,尤其是容量大时
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry<Home3.Boy, ArrayList<Home3.GirlFriend>> entry : boyMaps.entrySet()) {
System.out.println("key=" + entry.getKey() + "value=" + entry.getValue());
}
// 第四种
System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
for (ArrayList<Home3.GirlFriend> girlFriends: boyMaps.values()) {
System.out.println("value= " + girlFriends);
}
Map<Home3.Student, HashSet<Home3.Book>> stuMaps = new TreeMap<Home3.Student, HashSet<Home3.Book>>();
Home3.Student student1 = new Home3().new Student("至尊宝", 18, "男");
Home3.Student student2 = new Home3().new Student("夕阳武士", 20, "男");
HashSet<Home3.Book> books1 = new HashSet<Home3.Book>();
HashSet<Home3.Book> books2 = new HashSet<Home3.Book>();
Home3.Book book1 = new Home3().new Book("仙剑奇侠传", 200.7);
Home3.Book book2 = new Home3().new Book("天外飞仙", 190.2);
Home3.Book book3 = new Home3().new Book("轩辕剑", 199.2);
books1.add(book1);
books1.add(book2);
books2.add(book3);
books2.add(book3);
stuMaps.put(student1, books1);
stuMaps.put(student2, books2);
// 第一种:普遍使用,二次取值
System.out.println("通过Map.keySet遍历key和value:");
for (Student key: stuMaps.keySet()) {
System.out.println("key= " + key + " and value= " + stuMaps.get(key));
}
// 第二种
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<Home3.Student, HashSet<Home3.Book>>> iterator = stuMaps.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Home3.Student, HashSet<Home3.Book>> entry = iterator.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
// 第三种:推荐,尤其是容量大时
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry<Home3.Student, HashSet<Home3.Book>> entry: stuMaps.entrySet()) {
System.out.println("key=" + entry.getKey() + "value=" + entry.getValue());
}
// 第四种
System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
for (HashSet<Home3.Book> books: stuMaps.values()) {
System.out.println("value= " + books);
}
}
class Boy {
private String name;
private int age;
public Boy(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 String toString() {
return "Boy [name=" + name + ", age=" + age + "]";
}
}
class GirlFriend {
private String name;
private int age;
private String number;
public GirlFriend(String name, int age, String number) {
this.name = name;
this.age = age;
this.number = number;
}
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;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "GirlFriend [name=" + name + ", age=" + age + ", number=" + number + "]";
}
}
class Student implements Comparable {
private String name;
private int age;
private String sex;
public Student(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}
}
class Book {
private String name;
private double price;
public Book(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book [name=" + name + ", price=" + price + "]";
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/qq_18291623/article/details/47026253