使用Iterator遍历hashMap

  1 package com.ls.service;
  2
  3 import java.util.*;
  4 import com.ls.domain.Book;
  5 import com.sun.media.sound.AlawCodec;
  6 public class MyCartService {
  7
  8     HashMap<String,Book> hm=new HashMap<String, Book>();
  9     //Add2 books
 10
 11     public void addBook(String id){
 12         if(hm.containsKey(id)){
 13         Book book =hm.get(id);
 14         int shoppingNum=book.getShoppingNum();
 15         book.setShoppingNum(shoppingNum+1);
 16         }else{
 17         hm.put(id, new BookService().getBookById(id));
 18         }
 19     }
 20
 21
 22
 23     //Add books
 24     public void addBook(String id,Book book){
 25         if(hm.containsKey(id)){
 26         System.out.println("hm.containsKey(id)");
 27         int shoppingNum=book.getShoppingNum();
 28         book.setShoppingNum(shoppingNum+1);
 29         }else{
 30             System.out.println("xxx");
 31             hm.put(id, book);
 32         }
 33     }
 34
 35
 36
 37     //Delete books
 38     public void deleteBook(String id){
 39         hm.remove(id);
 40     }
 41
 42
 43
 44
 45     //Update books/myCart   numbers
 46
 47     public void updateBook(String id,String num){
 48         //取出ID对应book
 49         Book book =hm.get(id);
 50         book.setShoppingNum(Integer.parseInt(num));
 51
 52     }
 53
 54
 55     //显示购物车中的所有商品信息
 56     public ArrayList showMyCart(){
 57
 58                   ArrayList<Book> alBook=new ArrayList<Book>();
 59
 60     //遍历hashMap
 61
 62
 63     Iterator iterator =hm.keySet().iterator();
 64     while(iterator.hasNext()){
 65     //key
 66     String id=(String)iterator.next();
 67     //Book
 68     Book book=hm.get(id);
 69     alBook.add(book);
 70         }
 71         return alBook;
 72     }
 73
 74     //返回该购物车的总价
 75     public float getToatlPrice(){
 76         float totalPrice=0.0f;
 77
 78         //得到总价格
 79         ArrayList<Book> al =new ArrayList<Book>();
 80
 81         Iterator iterator=hm.keySet().iterator();
 82         while(iterator.hasNext()){
 83             //取出书号
 84
 85             String bookId=(String)iterator.next();
 86             //取出书号对应的bookbean
 87             Book book=hm.get(bookId);
 88     totalPrice+=book.getPrice()*book.getShoppingNum();
 89
 90         }
 91
 92
 93         return totalPrice;
 94     }
 95
 96     //Clear books/myCart
 97     public void cleanBook(){
 98         hm.clear();
 99     }
100
101
102 }
时间: 2024-08-05 15:44:45

使用Iterator遍历hashMap的相关文章

遍历 HashMap 的 5 种最佳方式,我不信你全知道!

原文地址:https://www.javaguides.net/2020/03/5-best-ways-to-iterate-over-hashmap-in-java.html 作者:Ramesh Fadatare 翻译:高行行 https://www.toutiao.com/a6803887957418705420/ 在本文中,我们将通过示例讨论在 Java 上遍历 HashMap 的五种最佳方法. 使用 Iterator 遍历 HashMap EntrySet 使用 Iterator 遍历

遍历hashMap的两种方式

第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object val = entry.getValue(); } 效率高,以后一定要使用此种方式!第二种: Map map = new HashMap(); I

遍历HashMap的四种方法

转自:http://blog.csdn.net/onlyonecoder/article/details/8514443 public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("1", "value1"); map.put("2", "value2"

用for和while遍历HashMap效率测试

import java.util.Calendar; import java.util.HashMap; import java.util.Map; import java.util.Iterator; public class EntryTest { public static void main(String[] args) { Map<Integer, String> map = new HashMap<Integer, String>(); for (int i = 0;

java遍历hashMap、hashSet、Hashtable

一.遍历HashMap Map<Integer, String> map = new HashMap<Integer, String>(); 方法一:效率高 for(Entry<Integer, String> entry:map.entrySet()){ System.out.println(entry.getKey()+"="+entry.getValue()); } 方法二: for (Integer key : map.keySet()) {

使用多种方式实现遍历HashMap

今天讲解的主要是使用多种方式来实现遍历HashMap取出Key和value,首先在java中如果想让一个集合能够用for增强来实现迭代,那么此接口或类必须实现Iterable接口,那么Iterable究竟是如何来实现迭代的,在这里将不做讲解,下面主要讲解一下遍历过程. //定义一个泛型集合 Map<String, String> map = new HashMap<String, String>(); //通过Map的put方法向集合中添加数据 map.put("001&

Java遍历HashMap并修改(remove)(转载)

遍历HashMap的方法有多种,比如通过获取map的keySet, entrySet, iterator之后,都可以实现遍历,然而如果在遍历过程中对map进行读取之外的操作则需要注意使用的遍历方式和操作方法. public class MapIteratorTest { private static Map<Integer, String> map = new HashMap<Integer, String>(); public static void main(String[]

遍历HashMap的几种方式

1 package com.test; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Map; 6 7 public class Test { 8 9 public static void main(String[] args) throws Exception { 10 11 Map<String,String> map = new HashMap<String,String&

遍历HashMap的四种方式

public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("1", "value1"); map.put("2", "value2"); map.put("3", "value3"); //第一种:普遍使用,二次