Random Example in Java Using with HashMap

import java.util.ArrayList;
import java.util.HashMap;
import java.util.ListIterator;
import java.util.Random;

public class RandomGeneratorExample{
   
    static Random myRandomGenerator = new Random();
   
    public static void main(String[] args)
    {
        HashMap<Integer,Integer> myHashMap = new HashMap<Integer,Integer>();
       
        for(int i=0;i<1000;i++)
        {
            int generatedNumber = (myRandomGenerator.nextInt(10))+1;
           
            if(myHashMap.containsKey(generatedNumber))
            {
                int k = myHashMap.get(generatedNumber);
                k++;
                myHashMap.put(generatedNumber, k);
            }
            else
                myHashMap.put(generatedNumber,1 );
        }
        System.out.println(myHashMap);
    }
}


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email