Professional Documents
Culture Documents
*;
class HashSetDemo1{
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(c==' ')
continue;
if(set.add(c)==false)
return false;
}
return true;
}
Output:
string has unique characters
Demonstration of HashMap – count the characters in a string
import java.util.*;
class HashMapDemo
{
static void check(String s)
{
HashMap<Character,Integer> map=new HashMap<Character,Integer>();
for(int i=0;i<s.length();i++)
{char c=s.charAt(i);
if(!map.containsKey(c))
map.put(c,1);
else
map.put(c,map.get(c)+1);
}
Output
count of e : 1
count of h : 1
count of l : 2
count of o : 1