veronikaaa86

Miner Task

Feb 13th, 2018
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. package MapLambdaStream;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.LinkedHashMap;
  7.  
  8. public class P03_MinerTask {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.  
  12.         String resource = "";
  13.         int quantity = 0;
  14.  
  15.         LinkedHashMap<String, Integer> myMap = new LinkedHashMap<>();
  16.  
  17.         while (!"stop".equals(resource = reader.readLine())) {
  18.             quantity = Integer.parseInt(reader.readLine());
  19.  
  20.             if (myMap.containsKey(resource)) {
  21.                 myMap.put(resource, myMap.get(resource) + quantity);
  22.             } else {
  23.                 myMap.put(resource, quantity);
  24.             }
  25.         }
  26.  
  27.         myMap.forEach((key, value) -> System.out.printf("%s -> %s%n", key, value));
  28.         //myMap.entrySet().forEach(c -> System.out.printf("%s -> %s%n", c.getKey(), c.getValue()));
  29.  
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment