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



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. How to sort an Arraylist in some given order? Say,
View Tutorial          By: Arunava Chatterjee at 2015-03-07 04:17:51

2. To 25
void *memset( void *buffer, int ch, s

View Tutorial          By: Doniv at 2011-12-15 10:10:52

3. it really helped me...thank you...
View Tutorial          By: ram at 2015-01-22 20:12:01

4. Wow
View Tutorial          By: Ajmalnafi at 2012-06-30 01:48:04

5. what if the delimiter is "(quotation mark)?
View Tutorial          By: hiakoto at 2013-02-18 16:15:20

6. I get an error " javax.mail" does not ex
View Tutorial          By: bhavin at 2009-02-03 05:50:08

7. Very good example! Thanks so much!
View Tutorial          By: Chrissy at 2010-04-14 22:37:56

8. Hi,
i am working on ocap xlet project. here

View Tutorial          By: swati at 2012-07-09 08:39:13

9. i m new in java plz send some programs of java usi
View Tutorial          By: supriyo at 2009-09-02 02:03:38

10. what is the output
View Tutorial          By: Raja at 2012-12-28 11:47:48