Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class MyClass {
- public static void main(String args[]) {
- Scanner scanner = new Scanner(System.in);
- int numberOfPieces = Integer.parseInt(scanner.nextLine());
- Map bigMap = new LinkedHashMap<String,LinkedHashMap<String,String>>(numberOfPieces);
- for (int i = 0; i < numberOfPieces; i++) {
- String nextPiece = scanner.nextLine();
- String[] stringArr = nextPiece.split("\\|");
- LinkedHashMap<String,String> composerAndKey = new LinkedHashMap<String,String>();
- composerAndKey.put(stringArr[1], stringArr[2]);
- bigMap.put(stringArr[0], composerAndKey);
- }
- while(true) {
- String nextCommand = scanner.nextLine();
- if (nextCommand.equals("Stop")) {
- Iterator<LinkedHashMap<String,LinkedHashMap<String,String>>> itr = bigMap.entrySet().iterator();
- while (itr.hasNext()) {
- Map.Entry pair = (Map.Entry)itr.next();
- LinkedHashMap<String,String> composerAndKeyMap = (LinkedHashMap<String,String>)pair.getValue();
- Iterator<Map.Entry<String,String>> composerAndKeyIterator = composerAndKeyMap.entrySet().iterator();
- Map.Entry composerAndKey = (Map.Entry)composerAndKeyIterator.next();
- System.out.printf("%s -> Composer: %s, Key: %s\n",pair.getKey(),composerAndKey.getKey(),composerAndKey.getValue());
- }
- break;
- }
- String[] stringArr = nextCommand.split("\\|");
- switch(stringArr[0]) {
- case "Add" : if (bigMap.containsKey(stringArr[1])) { System.out.printf("%s is already in the collection!\n", stringArr[1]); } else { LinkedHashMap<String, String> composerAndKey = new LinkedHashMap<String,String>(); composerAndKey.put(stringArr[2], stringArr[3]); bigMap.put(stringArr[1], composerAndKey); System.out.printf("%s by %s in %s added to the collection!\n", stringArr[1], stringArr[2], stringArr[3]);} break;
- case "Remove" : if(bigMap.containsKey(stringArr[1])) { bigMap.remove(stringArr[1]); System.out.printf("Successfully removed %s!\n",stringArr[1]); } else { System.out.printf("Invalid operation! %s does not exist in the collection.\n",stringArr[1]); } break;
- case "ChangeKey" : if (bigMap.containsKey(stringArr[1])) { LinkedHashMap<String, String> composerAndKey = (LinkedHashMap<String,String>)bigMap.get(stringArr[1]); Iterator<String> iterator = composerAndKey.keySet().iterator(); composerAndKey.put(iterator.next(),stringArr[2]); System.out.printf("Changed the key of %s to %s!\n",stringArr[1], stringArr[2]);} else {System.out.printf("Invalid operation! %s does not exist in the collection.\n",stringArr[1]);} break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement