Wednesday, October 25, 2006

Generics, for loop, autoboxing of JDK 5


package test50;

import java.util.*;
/**
* Test 1. generics; 2. new for loop; 3. autoboxing
* Task: count frequency of the words appearing in array
*/
public class Autoboxing {

public static void main(String[] args) {
String[] ars = {"as", "ae", "ac", "ad", "as", "ac"};
Map< String, Integer> m = new TreeMap<String, Integer>(); //1

for (String word : ars) { //2
Integer freq = m.get(word);
int ff = freq == null ? 1 : freq + 1;
m.put(word, ff); //3
}

System.out.println(m);
}
}
//output: {ac=2, ad=1, ae=1, as=2}

0 Comments:

Post a Comment

<< Home