本帖最后由 hejinzhong 于 2014-8-29 06:32 编辑
- 这里是描述商品的JavaBean和map集合存储信息,相当简单部分
- 类:product
- /*
- * 建立一个javaBean形式的Product
- */
- public class Product implements Serializable{
- private String id;
- private String name;
- private String price;
- private String numb;
-
-
- public Product() {
- super();
- }
-
- public Product(String id, String name, String price, String numb) {
- this.id = id;
- this.name = name;
- this.price = price;
- this.numb = numb;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPrice() {
- return price;
- }
- public void setPrice(String price) {
- this.price = price;
- }
- public String getNumb() {
- return numb;
- }
- public void setNumb(String numb) {
- this.numb = numb;
- }
- }
- 类:DB
- public class DB {
-
- //这里将map作为成员变量
- private static Map<String, Product> map = new TreeMap<String, Product>();
- /*
- 问题就在这里,如何用hashMap让map集合中的商品按下面的编号进行存储,键为编号
- */
- static{
- map.put("1", new Product("1", "衬衫", "38", "5"));
- map.put("2", new Product("2", "短裤", "68", "2"));
- map.put("3", new Product("3", "板鞋", "88", "1"));
- map.put("4", new Product("4", "袜子", "18", "8"));
- }
-
- public static Product findById(String id){
- return map.get(id);
- }
-
- public static Map<String, Product> findAll(){
- return map;
- }
- }
复制代码
|