Java之集合Map遍曆

來源:互聯網
上載者:User

標籤:java   map   遍曆   

/** * 1.嘗試Map<Boy,ArrayList<GirlFriend>> * 2.嘗試Map<Student,HashSet<Book>> * 3.嘗試ArrayList(你看過的電視劇)<ArrayList<Role人物>> * 4.假如有以下email資料“[email protected],[email protected],[email protected]”現需要把email中的使用者部分和郵件地址部分分離,分離後以索引值對應的方式放入HashMap? * keyvalue * aasohu.com * bb163.com * ccsina.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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";}@Overridepublic int compareTo(Object o) {// TODO Auto-generated method stubreturn 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;}@Overridepublic String toString() {return "Book [name=" + name + ", price=" + price + "]";}}}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Java之集合Map遍曆

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.