hashmap“键/值的方式”存取数据,使用collection接口的回调技术

来源:互联网 发布:javascript var 类型 编辑:程序博客网 时间:2024/05/29 12:09
 package com.fuxi.test.collection;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;/** *使用hashmap“键/值的方式”存取数据,使用collection接口的回调技术:即将该对象的引用赋给 *collection接口变量,该接口变量回调iterator()方法获取iterator对象(是存放了值) * @author Administrator * */public class HashMapTest {public static void main(String[] args) {Book book1 = new Book("12","j2se"),book2 = new Book("13","j2ee"),book3 = new Book("14","EJB");Map map = new HashMap();map.put(book1.number, book1);map.put(book2.number, book2);map.put(book3.number, book3);String key = "12";if(map.containsKey(key)){Book book = map.get(key);System.out.println(book.name+"有货");}int number = map.size();System.out.println("散列hashmap中有"+number+"个元素");Collection collection = map.values();Iterator it = collection.iterator();while(it.hasNext()){Book book = it.next();System.out.printf(book.name);System.out.println(book.number);}}}class Book{String name;String number; Book(String number,String name){this.name = name;this.number =number;}}
原创粉丝点击