HashSet is not synchronized. If more than one thread wants to access it at the same time then it must be synchronized externally.
This code shows the use of HashSet. This will identify the number of duplicate words in a String. The String is passed as command line arguments.
Add() method of the HashSet add the object into the storage if it is not already present.
import java.util.*;
public class FindDups {
public static void main(String[] args) {
Set s = new HashSet();
for(int i=0; i
System.out.println("Duplicate detected : " + args[i]);
}
System.out.println(s.size() + " distinct words detected : " + s );
}
}
Run the program: C:\> java FindDups i came i came i conquered
Output Screen :
Duplicate detected: i
Duplicate detected: i
4 distinct words detected : [came,saw,conquered,i]
No comments:
Post a Comment