list、map、set、for-each、Iterator综合练习1
来源:互联网 发布:网络剧2017 古装 编辑:程序博客网 时间:2024/05/05 19:54
package com.lrq;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;/** * * @author 李瑞琦 * 使用List和Map存放多个图书信息,遍历并输出。 * 其中图书属性:编号,名称,单价,出版社; 使用商品编号作为Map中的key。 * */public class Test01 { public static void main(String[] args) { // 声明list集合 List<Book> list = new ArrayList<Book>(); //实例化并赋初值Book对象 Book book1 = new Book(101, "西游记", 10.1, "清华出版社"); Book book2 = new Book(002, "水浒传", 10.2, "北大出版社"); Book book3 = new Book(003, "红楼梦", 10.3, "人大出版社"); Book book4 = new Book(004, "三国演义", 10.4, "地质大学出版社"); //往list里面存值 list.add(book1); list.add(book2); list.add(book3); list.add(book4); //遍历集合并输出,在知道是list集合的前提下可以直接用foreach遍历, //在不知道是list还是set的前提下可以优先考虑Iterator迭代器 for (Iterator ite = list.iterator(); ite.hasNext();) { Book book = (Book) ite.next(); System.out.println(book); } //声明一个map集合 Map< Integer, Book> map = new HashMap<Integer,Book>(); //实例化并赋初值Book对象 Book book5 = new Book(101, "活着", 10.1, "清华出版社"); Book book6 = new Book(002, "骆驼祥子", 10.2, "北大出版社"); Book book7 = new Book(003, "茶馆", 10.3, "人大出版社"); Book book8 = new Book(004, "大学", 10.4, "地质大学出版社"); //往map中存放数据 map.put(1, book1); map.put(2, book2); map.put(3, book3); map.put(4, book4); //在foreach中使用entry来遍历map for (Entry<Integer, Book> entry : map.entrySet()) { System.out.println(entry.getValue()); } }}package com.lrq;/** * JAVABean * @author 图书类 * */public class Book { private int id; private String name; private double price; private String pressName; public Book() { super(); // TODO Auto-generated constructor stub } public Book(int id, String name, double price, String pressName) { super(); this.id = id; this.name = name; this.price = price; this.pressName = pressName; } @Override public String toString() { return "Book [id=" + id + ", name=" + name + ", price=" + price + ", pressName=" + pressName + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((pressName == null) ? 0 : pressName.hashCode()); long temp; temp = Double.doubleToLongBits(price); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Book other = (Book) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (pressName == null) { if (other.pressName != null) return false; } else if (!pressName.equals(other.pressName)) return false; if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price)) return false; return true; } public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } public String getPressName() { return pressName; } public void setPressName(String pressName) { this.pressName = pressName; }}
阅读全文
0 0
- list、map、set、for-each、Iterator综合练习1
- List, Map, Set与Iterator的使用
- 4.1 Collection,List,Set,Iterator,Map
- Java容器(List、Map、Set、Iterator)
- 4.1 Collection,List,Set,Iterator,Map
- 集合Collection,List,Set,Map,Iterator,Entry
- list、set、map集合的综合应用
- for,list,iterator,map的访问
- 比较器练习(list,set,map...)
- list,set练习和Map简述
- List、Set及Map的读取数据---Iterator遍历
- java类集(List/Set/Map/Iterator)(Enumeration/Vector/Stack/Properties)
- 类集框架:List、Set、Map、Iterator、Comparator(重点)
- 集合框架总结——Collection、Iterator、List、Set、Map
- java集合 - Collection - List - Set - Map - Iterator 学习笔记
- map,set,iterator
- map set iterator使用
- Iterable,(for each)与Iterator
- CR LF 回车 换行
- Max Sum HDU
- ubuntu 16.04+cuda8.0的安装
- 区块链解读5-名词解释
- asp.net Excel导入和导出
- list、map、set、for-each、Iterator综合练习1
- jsp页面按yyyy-MM-dd HH:mm:ss格式显示Date类型的数据
- 有线电视网 洛谷p1273
- hibernate三种查询方式
- 【SSLGZ 2677】2017年8月10号提高组T2 飞行
- 秒杀系统设计-转载
- 欢迎使用CSDN-markdown编辑器
- C语言入门:插入排序(代码实现,而不是排序方法阐述)
- webstorm编辑器使用babel自动转化es6