循环遍历HashMap的较快方法

来源:互联网 发布:echo 数字数组 编辑:程序博客网 时间:2024/04/30 04:42
package MAIN;import Book.*;import java.util.*;public class slove {public static void main(String[] args) {Scanner in = new Scanner(System.in);int n = in.nextInt();String name; int val, num;HashMap<Book, Integer> str = new HashMap<Book, Integer>();for(int i = 0; i < n; ++i) {name = in.next();val = in.nextInt();num = in.nextInt();Integer second = Integer.valueOf(val) * Integer.valueOf(num);Book first = new Book(name, val, num);str.put(first, second);}int ans = getSum(str);System.out.println(ans);}public static int getSum(HashMap rhs) {int sum = 0;Iterator it = rhs.entrySet().iterator();while(it.hasNext()) {Map.Entry<Book, Integer> entry = (Map.Entry<Book, Integer>)it.next();Integer value = entry.getValue();sum += (int)value;}return sum;}}

0 0