View RSS Feed

Advanced Java

Generic Set in Java

Rate this Entry
by , 02-18-2012 at 04:45 PM (826 Views)
It is also possible to generify the java.util.Set. Set instances in these cases might of a specific type.

Instances of that type in such cases could be inserted or read, from that set.

Usage of generic Set is explained here.
Java Code: This is the code to explain usage of generic Set
Set<String> set = new HashSet<String>;
Adding Elements to a Generic Set

To a set, specific String type is added by using this code.
Java Code: This is the code to add specific String type in a Set
Set<String> set = new HashSet<String>;
String string1 = "a string";
set.add(string1);
Iterating a Generic Set

To iterate over generic Set, use this code:
Java Code: This is the code to iterate over generic Set
Set<String> set = new HashSet<String>;
Iterator<String> iterator = set.iterator();
while(iterator.hasNext()){
  String aString = iterator.next();
}

Submit "Generic Set in Java" to Facebook Submit "Generic Set in Java" to Digg Submit "Generic Set in Java" to del.icio.us Submit "Generic Set in Java" to StumbleUpon Submit "Generic Set in Java" to Google

Tags: generic set Add / Edit Tags
Categories
Generics

Comments