Java Code Examples for java.util.HashMap.merge()

The following are Jave code examples for showing how to use merge() of the java.util.HashMap class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: ACE_HackerRank   File: Solution.java   Source Code and License Vote up 6 votes
public static void main(String[] args) {
    HashMap<Integer, Integer> map = new HashMap<>();
    ArrayDeque<Integer> deque     = new ArrayDeque<>();
    
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    int m = scan.nextInt();
    int max = 0;
    
    for (int i = 0; i < n; i++) {
        /* Remove old value (if necessary) */
        if (i >= m) {
            int old = deque.removeFirst();
            if (map.get(old) == 1) {
                map.remove(old);
            } else {
                map.merge(old, -1, Integer::sum);
            }
        }
        
        /* Add new value */
        int num = scan.nextInt();
        deque.addLast(num);
        map.merge(num, 1, Integer::sum);
        
        max = Math.max(max, map.size());
    }
    
    scan.close();
    System.out.println(max);
}
 
Example 2
Project: HackerRank_solutions   File: Solution.java   Source Code and License Vote up 5 votes
public static void canCreate(String [] magazine, String [] ransom) {
    HashMap<String, Integer> usableWords = makeMap(magazine);
    for (int i = 0; i < ransom.length; i++) {
        if (usableWords.containsKey(ransom[i]) && usableWords.get(ransom[i]) > 0) {
            usableWords.merge(ransom[i], -1, Integer::sum); // uses the word
        } else {
            System.out.println("No");
            return;
        }
    }
    System.out.println("Yes");
}
 
Example 3
Project: HackerRank_solutions   File: Solution.java   Source Code and License Vote up 5 votes
private static HashMap<String, Integer> makeMap(String [] words) {
    HashMap<String, Integer> map = new HashMap<>();
    for (int i = 0; i < words.length; i++) {
        map.merge(words[i], 1, Integer::sum);
    }
    return map;
}
 
Example 4
Project: HackerRank_solutions   File: Solution.java   Source Code and License Vote up 5 votes
public static void main(String[] args) {
    HashMap<Integer, Integer> map = new HashMap<>();
    ArrayDeque<Integer> deque     = new ArrayDeque<>();
    
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    int m = scan.nextInt();
    int max = 0;
    
    for (int i = 0; i < n; i++) {
        /* Remove old value (if necessary) */
        if (i >= m) {
            int old = deque.removeFirst();
            if (map.get(old) == 1) {
                map.remove(old);
            } else {
                map.merge(old, -1, Integer::sum);
            }
        }
        
        /* Add new value */
        int num = scan.nextInt();
        deque.addLast(num);
        map.merge(num, 1, Integer::sum);
        
        max = Math.max(max, map.size());
    }
    
    scan.close();
    System.out.println(max);
}
 
Example 5
Project: ACE_HackerRank   File: Solution.java   Source Code and License Vote up 5 votes
public static void canCreate(String [] magazine, String [] ransom) {
    HashMap<String, Integer> usableWords = makeMap(magazine);
    for (int i = 0; i < ransom.length; i++) {
        if (usableWords.containsKey(ransom[i]) && usableWords.get(ransom[i]) > 0) {
            usableWords.merge(ransom[i], -1, Integer::sum); // uses the word
        } else {
            System.out.println("No");
            return;
        }
    }
    System.out.println("Yes");
}
 
Example 6
Project: ACE_HackerRank   File: Solution.java   Source Code and License Vote up 5 votes
private static HashMap<String, Integer> makeMap(String [] words) {
    HashMap<String, Integer> map = new HashMap<>();
    for (int i = 0; i < words.length; i++) {
        map.merge(words[i], 1, Integer::sum);
    }
    return map;
}