count ocurrence of array elements in java

www.l‮ruttua‬i.com
count ocurrence of array elements in java
/**
 * @author lautturi.com
 * Java example: count the ocurrence of each item in java array
 */

import java.util.*;

public class Lautturi {

	public static void main(String[] args) {

		List asList = Arrays.asList("hello", "lautturi", "java", "python", "world", "java", "C", "java", "python",
				"Lautturi");
		Set<String> mySet = new HashSet<String>(asList);
		for (String s : mySet) {

			System.out.println(s + " " + Collections.frequency(asList, s));

		}

	}
}

output:

python 2
java 3
world 1
C 1
lautturi 1
Lautturi 1
hello 1
Created Time:2017-09-06 23:17:23  Author:lautturi