Skip to content

Instantly share code, notes, and snippets.

@lfborjas
Created November 15, 2011 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfborjas/1365695 to your computer and use it in GitHub Desktop.
Save lfborjas/1365695 to your computer and use it in GitHub Desktop.
/**Programa que genera un arreglo con tamaño aleatorio de enteros aleatorios
e imprime un histograma de frecuencias
*/
//clase de java para generar números aleatorios:
import java.util.Random;
//sirve para representar colecciones tabulares
import java.util.HashMap;
//contiene distintas utilidades para colecciones
import java.util.Collections;
//para poder iterar sobre el mapa
import java.util.Map.Entry;
import java.util.Arrays;
public class Histograma{
public static void main (String [] args){
HashMap<Character, Integer> tabla = new HashMap<Character, Integer>();
String input = "this is an example for huffman encoding";
for(char c: input.toCharArray()){
if(!tabla.containsKey(c))
tabla.put(c, new Integer(1));
else
tabla.put(c, tabla.get(c)+1); //reemplazar el valor viejo por su sucesor
}
System.out.printf("Podés ver todos los valores en una colección con este método: %s\n", tabla.values());
for(Entry<Character, Integer> fila: tabla.entrySet()){
System.out.printf("%s: %s\n",fila.getKey(), fila.getValue());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment