Comment on Tutorial - Using StringTokenizer in Java By Emiley J



Comment Added by : Anush

Comment Added at : 2012-07-16 08:30:58

Comment on Tutorial : Using StringTokenizer in Java By Emiley J
@pooja: this method should work, although i'm sure it isn't the most efficient one. str is your string.

public void doIt(String str) {
Hashtable<String, Integer> hTable = new Hashtable<String, Integer>();
String next = null;
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens()) {
next = st.nextToken();
if (hTable.containsKey(next)) {
hTable.put(next, hTable.get(next) + 1);
} else {
hTable.put(next, 1);
}
}
Collection<Integer> coll = hTable.values();
Object[] integerArray = coll.toArray();
Arrays.sort(integerArray, Collections.reverseOrder());
Set<Object> mySet = new LinkedHashSet<Object>(Arrays
.asList(integerArray));
for (Object i : mySet) {
for (Map.Entry<String, Integer> entry : hTable.entrySet()) {
if (i.equals(entry.getValue())) {
System.out.println(entry.getKey());
}
}
}
}


View Tutorial



Ask a Question

Subscribe to Tutorials

Related Tutorials

Program using concept of byte long short and int in java

Update contents of a file within a jar file

Tomcat and httpd configured in port 8080 and 80

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java

Java program using Method Overriding

Archived Comments

1. great example for beginners. Thank you
View Tutorial          By: kumar at 2011-11-24 08:15:14

2. can i have a piece of program so that i can improv
View Tutorial          By: miniboss at 2010-11-12 06:09:41

3. Excellent example Jagan. By the way I have a quest
View Tutorial          By: Shrihas at 2012-01-07 14:12:48

4. nice
View Tutorial          By: srfds at 2012-05-28 09:25:15

5. Hi Divya, Obviously some other application is usin
View Tutorial          By: Ramlak at 2008-05-01 22:16:12

6. good
View Tutorial          By: epuri at 2008-09-09 01:05:56

7. I am able to iterate the list.Now i want to print
View Tutorial          By: akanksha at 2012-09-26 05:25:12

8. NICE EXPLANATION >>>THANK UUUUUU
View Tutorial          By: PraVeen at 2012-07-05 10:12:25

9. Real Life example of using Volatile
*******

View Tutorial          By: Mayank at 2011-01-25 15:44:21

10. Hai,
I have stil problem with generate XML

View Tutorial          By: Priyan at 2009-04-06 12:08:42