Sunday, 23 October 2016

What is AbstractSet In Java ?

Previously we Have discussed about

Here we learn about AbstractSet in Java ,but before that you must know What is Collection Interface In Java ?
Class AbstractSet<E>

Type Parameters:
E - the type of elements maintained by this set

All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>

Direct Known Subclasses:
ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, TreeSet

Declaration of AbstractSet:
public abstract class AbstractSet<E>
extends AbstractCollection<E>
implements Set<E>

AbstractSet class provides a skeletal implementation of the Set interface.
The process of implementing a set by extending this class is identical to that of implementing a Collection by extending AbstractCollection, except that all of the methods and constructors in subclasses of this class must obey the additional constraints imposed by the Set interface.

AbstractSet class does not override any of the implementations from the AbstractCollection class. It merely adds implementations for equals and hashCode methods.
This class is a member of the Java Collections Framework.

Constructor Summary of AbstractSet:

Constructor
Description
protected AbstractSet()
Sole constructor.

Method Summary of AbstractSet:

Modifier and Method Name
Description
boolean equals(Object o)
Compares the specified object with this set for equality.
int hashCode()
Returns the hash code value for this set.
boolean removeAll(Collection<?> c)
Removes from this set all of its elements that are contained in the specified collection (optional operation).
      
Example of AbstractSet:

import java.util.AbstractSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

class Employee extends AbstractSet<Integer> {

Set<Integer> s = new HashSet<Integer>();

@Override
public Iterator<Integer> iterator() {
return s.iterator();
}
@Override
public int size() {
return s.size();
}
@Override
public boolean add(Integer arg0) {
return s.add(arg0);
}
}
public class AbstractSetClassExample {

public static void main(String[] args) {
Employee e =new Employee();
for(int i=1;i<=5;i++){
e.add(i);
}
e.add(3); //Set Will Not add this element,because no duplicate elements are allowed in set.
System.out.println("AbstractSet Size : "+e.size());

System.out.println("\nTraversing AbstractSet using Iterator : ");
Iterator<Integer> it = e.iterator();
while(it.hasNext()){
System.out.println(it.next());
}

System.out.println("\nTraversing AbstractSet using Lambda Expression : ");
e.forEach( i -> System.out.println(i));
}

}
Program Output:

AbstractSet Size : 5

Traversing AbstractSet using Iterator : 
1
2
3
4
5

Traversing AbstractSet using Lambda Expression : 
1
2
3
4
5


Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment