Java/Set

From Meshplex

Jump to: navigation, search
Image:Java_programming.jpg
Introduction to Java
Overview of Java
How to Setup Java on your PC
Java Data Types, Variables,and Arrays
Java Operators
Java Packages, Classes, and Interfaces
Java Program
Java Modifiers
Java Control Statements
Java Exception Handling
Java Object Oriented Programming
Java Wrappers
Java Strings
Java Math
Java Arrays
Java Random Numbers
Java Date and Time
Java Regular Expressions
Java Collections
* List
* Java Set
* Java Comparators
* Java Iterators
* Java Map
* Java Legacy Classes and Interfaces
Java Generics
Java Thread and Multithreading
Java IO and file Handling
Java Network Programming
Java RMI
Java Image, Audio, Video
Java GUI
Java Applets
Java Internationalization
JDBC
Java and XML

Contents

[edit] Java Set

It as per its name, defines a set. It again as List extends collection and it doesn’t allow the duplicity of elements. It doesn’t define any method of its own like the List Interface.


import java.util.LinkedHashSet;
import java.util.Set;
 
 
public class SetExample {
 
	public static void main(String args[]) {
		Set set = new LinkedHashSet();
		set.add("1");
		set.add("1");
		set.add("2");
 
		System.out.println(set);
	}
}
Output generated after executing the above code

[1, 2]

And it is not like 

[1, 1, 2]

[edit] How to retreive elements from a Set

We have to use Iterators to access the elements in a Set. Though Iterators will be explained in a different section but we will still give you one example which will show how to access the elements in a Set.
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
 
 
public class SetIteratorExample {
 
	public static void main(String args[]) {
		Set set = new LinkedHashSet();
		set.add("1");
		set.add("2");
		set.add("3");
		set.add("4");
 
		Iterator iter = set.iterator();
 
		for(;iter.hasNext();) {
			String element = (String) iter.next();
			System.out.println(element);
		}
	}
}


Iterators are mainly inner classes with the concrete class like ArrayList, LinkedList, TreeSet, LinkedHashSet etc. Main purpose of it making as an inner class within these class and not as a different implement class because these implements are specific to their concrete classes.

[edit] Java SortedSet Interface

A set that further guarantees that its iterator will traverse the set in ascending element order, sorted according to the natural ordering of its elements (see Comparable), or by a Comparator provided at sorted set creation time. Several additional operations are provided to take advantage of the ordering. We can call first( ) method to get the first element and last ( ) to get the last element of the set. headSet ( ) is used to find the subset that starts with the first element in the set and tailSet( ) to get the subset that ends the set.

import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
 
public class SortedSetExample {
 
	public static void main(String[] args) {
		SortedSet set = new TreeSet(); // TreeSet implements SortedSet.
		                               // Otherwise this line would have
		                               // given a compiler error saying
		                               // could not type cast TreeSet to
		                               // SortedSet.
		set.add("b");
		set.add("c");
		set.add("a");
 
		for (Iterator iter = set.iterator(); iter.hasNext();) {
			String element = (String) iter.next();
			System.out.println(element);
		}
	}
}

Output of the above code

a
b
c

[edit] Java TreeSet

This Class Implements Set Interface that uses Tree structure for the storage of the elements in the set. The objects in this are stored in the ascending order and the access to the elements in this type of arrangement is very quick and efficient and it gives us the feasibility to store a large number of data with this tree structure.

TreeSet has the following constructors:

TreeSet( ): This constructor create the default empty tree.

TreeSet(Collection c) : This builds a tree set that contains the elements of c.

TreeSet(Comparator comp): This constructor constructs an empty tree set that will be sorted according to the comparator specified by comp.

TreeSet(SortedSet ss): This constructor builds a tree set that contains the elements of ss.

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
 
public class TreeSetExample {
 
	public static void main(String[] args) {
		Set set = new TreeSet(new DescendingComparator());
		set.add("1");
		set.add("2");
		set.add("3");
		set.add("4");
		set.add("3");
		System.out.println(set);
	}
}
 
class DescendingComparator implements Comparator {
 
	public int compare(Object arg0, Object arg1) {
 
		String str1 = (String) arg0;
		String str2 = (String) arg1;
		return -(str1.compareTo(str2));
	}
 
}

[edit] Java HashSet

This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
 
 
public class HashSetExample {
 
	public static void main(String args[]) {
		Set set = new HashSet();
		set.add("c");
		set.add("a");
		set.add(null);
		set.add("b");
 
		for(Iterator iter = set.iterator(); iter.hasNext();) {
			String element = (String) iter.next();
			System.out.println(element);
		}
	}
}

Output of the above program

null
b
c
a

[edit] Java LinkedHashSet

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set.

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
 
 
public class LinkedHashSetExample {
 
	public static void main(String[] args) {
		Set set = new LinkedHashSet();
		set.add("c");
		set.add("b");
		set.add("a");
 
		for(Iterator iter = set.iterator(); iter.hasNext();) {
			String element = (String) iter.next();
			System.out.println();
		}
	}
}


Output of the above program

c
b
a