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

 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; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值