Repeated number with frequency

 


This example with  take a simple  java array  and print its element with repetition 

 count. 


import java.util.Arrays;

import java.util.Collections;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


public class Test{

  public static void main(String[] args) {

    String[] city = {"Bombay", "Delhi", "sydney", "Delhi", "paris", "Bombay", "ny"};

    List<String> asList = Arrays.asList(city);

    Map<String, Integer> map= new HashMap<String, Integer>();

    for(String s: city){

      map.put(s,Collections.frequency(asList,s));

    }

    System.out.println(map);


  }

}

      

      Program output :

      

      

       {Delhi=2, sydney=1, paris=1, Bombay=2, ny=1}

Repeated number with frequency Repeated number with  frequency Reviewed by Mukesh Jha on 4:10 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.