hashmap

an anonymous user · February 23, 2021 · Java
import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    public static void main(String[] args) {
        String[] s= new String[] {"ab", "ba", "ca", "ab", "bc", "ab", "ba", "ac", "bc", "ab", "ca", "ac", "ba"};
        HashMap<String, Integer> map = new HashMap<>();
        map = creaHashMap(s);
    }

    public static HashMap<String, Integer> creaHashMap (String[] s){
        HashMap<String,Integer> mappaRicorrenze = new HashMap<>();
        for (String temp: s) {
            if (mappaRicorrenze.containsKey(temp)){
                int val=(mappaRicorrenze.get(temp))+1;
                mappaRicorrenze.replace(temp, val);
            }
            else {
                mappaRicorrenze.put(temp, 1);
            }
        }
        return mappaRicorrenze;
    }
}

Comments

Please sign up or log in to contribute to the discussion.