Programming in Java Programming in Java: Using The Set Interface Using The Set Interface (Contd.)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

4/20/2021

Programming in Java Programming in Java

Using the Set Interface He can implement a Using the Set Interface (Contd.)
Scenario: set of functionalities Scenario (Contd.):
to create a chat group
dynamically from the
list of available
contacts. To implement this functionality, you can create
Needs to a collection of contacts.
develop a chat But, to ensure the uniqueness of the contacts
application. in the chat group, you need to create the
He can add or remove
members from the collection using the Set interface.
chat group as per the
user preference.

A developer

Ver 1.0 Slide 1 of 10 Ver 1.0 Slide 2 of 10

Programming in Java Programming in Java

Using the Set Interface (Contd.) Using the Set Interface (Contd.)
The following figure shows the class hierarchy of the java.util The package, java.util, provides the Iterator interface to
package, which implements the Set interface. traverse through the set collection.
The reference of the Iterator interface can be obtained using the
iterator() method of the Set interface.
The Iterator interface contains various methods, such as
hasNext() and next(), which help to traverse the set collection.

Ver 1.0 Slide 3 of 10 Ver 1.0 Slide 4 of 10

1
4/20/2021

Programming in Java Programming in Java

Working with the HashSet Class Working with the HashSet Class (Contd.)
The HashSet class provides the implementation of the Set Consider the following code snippet to create a collection using the
interface that enables you to create a set in which insertion is faster HashSet class:
because it does not sort the elements. import java.util.*;
class Contacts
Some of the commonly used constructors of the HashSet class
{
are: /* code to create contacts */
HashSet() }
HashSet(Collection<? extends E> c) public class ChatGroup
HashSet(int initialCapacity) {
public static void main(String args[])
Some of the commonly used methods of the HashSet class are:
{
boolean add(E e) Contacts a=new Contacts();
void clear() Contacts b=new Contacts();
boolean remove(Object o) Contacts c=new Contacts();
int size() HashSet<Contacts> hs=new HashSet<Contacts>();
hs.add(a);

Ver 1.0 Slide 5 of 10 Ver 1.0 Slide 6 of 10

Programming in Java Programming in Java

Working with the HashSet Class (Contd.) Working with the HashSet Class (Contd.)
hs.add(b); Java also provides support for non generic collections.
hs.add(c);
You can use the non generic HashSet collection to store any type
}
} of objects, as shown in the following code snippet:
Contacts a=new Contacts();
You can use the following code snippet to remove the Contacts HashSet hs=new HashSet();
object from the HashSet object: hs.add(a);
hs.remove(c); hs.add("String object");
You can use the following code snippet to find the number of hs.add(new Integer(3));
objects available in the HashSet object:
hs.size();
You can use the following code snippet to traverse through the
HashSet object:
Iterator i = hs.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
Ver 1.0 Slide 7 of 10 Ver 1.0 Slide 8 of 10

2
4/20/2021

Programming in Java Programming in Java

Working with the TreeSet Class Working with the TreeSet Class (Contd.)
The TreeSet class provides the implementation of the Set Consider the code given in the embedded document to add and
interface that enables you to create a sorted set of objects. remove the objects to/ from a TreeSet collection:
The insert or add object process is slow as it sorts the objects on
every insertion. Microsoft Word
Document
Some of the constructors of the TreeSet class are:
TreeSet()
TreeSet(Collection<? extends E> c)
Some of the methods of the TreeSet class are:
boolean add(E e)
void clear()
boolean remove(Object o)
int size()

Ver 1.0 Slide 9 of 10 Ver 1.0 Slide 10 of 10

You might also like