Home Can anyone explain the logic behind this code?
Reply: 0

Can anyone explain the logic behind this code?

Karthik K
Karthik K Published in 2018-01-13 06:39:00Z

The code below inserts the values in my HashMap named map2 even before I use the put function to insert the array using the specific key. I tried commenting the last line and it works even without it. Can anyone explain what happening in this code?

public static int findShortestLength(int input[]) {
    HashMap<Integer, Integer> map = new HashMap<>();
    HashMap<Integer, Integer[]> map2 = new HashMap<>();
    int max = 0;
    for (int i = 0; i < input.length; i++) {
        map.put(input[i], map.getOrDefault(input[i], 0) + 1);
        max = Math.max(max, map.get(input[i]));
        if (map2.get(input[i]) == null) {
            map2.put(input[i], new Integer[2]);
        Integer[] tempRange = map2.get(input[i]);
        if (tempRange[0] == null) {
            tempRange[0] = i;
        tempRange[1] = i;
        // map2.put(input[i],tempRange); Same result using this line
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.515056 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO