Question

Methods enforced by the set interface: import java.util.Iterator; public interface Set<E> { /** Removes all of...

Methods enforced by the set interface:

import java.util.Iterator;

public interface Set<E> {
   
   /**
      Removes all of the elements from this set
   */
   void clear();
   
   /**
      Returns true if this set contains no elements
      @return true if this set contains no elements
   */
   boolean isEmpty();
   
   /**
      Returns the number of elements in this set (its cardinality)
      @return the number of elements in this set
   */
   int size();
   
   /**
      Returns an iterator over the elements in this set
      @return an iterator over the elements in this set
   */
   Iterator<E> iterator();

   /**
      Returns true if this set contains the specified element
      @param e element whose presence in this set is to be tested
      @return true if this set contains the specified element
   */
   boolean contains (E e);
   
   /**
      Adds the specified element to this set if it is not already present
      @param e element to be added to this set
      @return true if this set did not already contain the specified element
   */
   boolean add (E e);
   
   /**
      Removes the specified element from this set if it is present
      @param e element to be removed from this set, if present
      @return true if this set contained the specified element
   */
   boolean remove (E e);
}
   
0 0
Add a comment Improve this question Transcribed image text
Answer #1

EXPLAINATION TO THE ABOVE PROGRAM:

JAVA SET:
i. Set is an interface which extends Collection.
ii.   It is an unordered collection of objects in which duplicate values cannot be stored.
iii. Set has various methods to add, remove clear, size, etc to enhance the usage of it.
THE SET INTERFACE:
i.   Java Set interface is a member of the Java Collections Framework.
ii.   Unlike List, Set DOES NOT allow you to add duplicate elements.
iii.   Set allows us to add at most one null element only.
iv.   Set interface got one default method in Java 8: spliterator.
v.   Unlike List and arrays, Set does NOT support indexes or positions of it’s elements.
vi.   Set supports Generics and we should use it whenever possible. Using Generics with Set will avoid ClassCastException at run-time.
vii.   We can use Set interface implementations to maintain unique elements.
The Java platform contains three general-purpose Set implementations:
⦁   HashSet
⦁   TreeSet
⦁   LinkedHashSet
HashSet, which stores its elements in a hash table, is the best-performing implementation; however it makes no guarantees concerning the order of iteration.
TreeSet, which stores its elements in a red-black tree, orders its elements based on their values; it is substantially slower than HashSet.
LinkedHashSet, which is implemented as a hash table with a linked list running through it, orders its elements based on the order in which they were inserted into the set .
SET INTERFACE OPERATIONS:

Set Interface operations are use to implement some tasks like:
⦁   The size operation returns the number of elements in the Set.
⦁   The add method adds the specified element to the Set if it is not already present and returns a boolean indicating whether the element was added.
⦁   The remove method removes the specified element from the Set if it is present and returns a boolean indicating whether the element was present.
⦁   The iterator method returns an Iterator over the Set.
Iterating Elements:
In Java, Iterator is an interface available in Collection framework in java. util package. It is a Java Cursor used to iterate a collection of objects. It is used to traverse a collection object elements one by one.We import "Java.util.Iterator" package to perform this process.
The same as with lists, although possible to iterate with for and enhanced-for loops, it's better to use the Java Collections' Iterator.
JAVA SET METHODS:
1.   int size(): to get the number of elements in the Set.
2.   boolean isEmpty(): to check if Set is empty or not.
3.   boolean contains(Object o): Returns true if this Set contains the specified element.
4.   Iterator iterator(): Returns an iterator over the elements in this set. The elements are returned in no particular order.
5.   Object[] toArray(): Returns an array containing all of the elements in this set. If this set makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
6.   boolean add(E e): Adds the specified element to this set if it is not already present (optional operation).
7.   boolean remove(Object o): Removes the specified element from this set if it is present (optional operation).
8.   boolean removeAll(Collection c): Removes from this set all of its elements that are contained in the specified collection (optional operation).
9.   boolean retainAll(Collection c): Retains only the elements in this set that are contained in the specified collection (optional operation).
10.   void clear(): Removes all the elements from the set.
11.   Iterator iterator(): Returns an iterator over the elements in this set.

SET INTERFACE BULK OPERATIONS:
Bulk operations are particularly well suited to Sets. when applied, they perform standard set-algebraic operations.
Suppose s1 and s2 are sets. Here's what bulk operations do:
⦁   s1.containsAll(s2) — returns true if s2 is a subset of s1. Here, s2 is a subset of s1 if set s1 contains all of the elements in s2.
⦁   s1.addAll(s2) — transforms s1 into the union of s1 and s2. Here,The union of two sets is the set containing all of the elements contained in either set.
⦁   s1.retainAll(s2) — transforms s1 into the intersection of s1 and s2. Here,The intersection of two sets is the set containing only the elements common to both sets.
⦁   s1.removeAll(s2) — transforms s1 into the set difference of s1 and s2.

Add a comment
Know the answer?
Add Answer to:
Methods enforced by the set interface: import java.util.Iterator; public interface Set<E> { /** Removes all of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /**...

    Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • Implement the missing methods in java! Thanks! import java.util.Iterator; import java.util.NoSuchElementException; public class HashTableOpenAddressing<K, V> implements...

    Implement the missing methods in java! Thanks! import java.util.Iterator; import java.util.NoSuchElementException; public class HashTableOpenAddressing<K, V> implements DictionaryInterface<K, V> { private int numEntries; private static final int DEFAULT_CAPACITY = 5; private static final int MAX_CAPACITY = 10000; private TableEntry<K, V>[] table; private double loadFactor; private static final double DEFAULT_LOAD_FACTOR = 0.75; public HashTableOpenAddressing() { this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR); } public HashTableOpenAddressing(int initialCapacity, double loadFactorIn) { numEntries = 0; if (loadFactorIn <= 0 || initialCapacity <= 0) { throw new IllegalArgumentException("Initial capacity and load...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic *...

    package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic * collection to store elements where indexes represent priorities and the * priorities can change in several ways. * * This collection class uses an Object[] data structure to store elements. * * @param <E> The type of all elements stored in this collection */ // You will have an error until you have have all methods // specified in interface PriorityList included inside this...

  • create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s)...

    create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s) to return the max element in the queu. min(SinglyLinkedQueue<Integer> s) to return the min element in the queue. sum(SinglyLInkedQueue<Integer> s) to return the sum of elements in the queu. median(SinglyLinkedQueue<Integer> s) to return the median of elements in the queue. split(SinglyLinkedQueue<Integer> s) to separate the SinglyLinkedQueue into two ArrayQueues based on whether the element values are even or odd. package Stack_and_Queue; import java.util.Iterator; import...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • Im having trouble implimenting the addAll method.. import java.util.Collection; public interface Tree<E> extends Collection<E> { /**...

    Im having trouble implimenting the addAll method.. import java.util.Collection; public interface Tree<E> extends Collection<E> { /** Return true if the element is in the tree */ public boolean search(E e); /** Insert element e into the binary tree * Return true if the element is inserted successfully */ public boolean insert(E e); /** Delete the specified element from the tree * Return true if the element is deleted successfully */ public boolean delete(E e);    /** Get the number of...

  • Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT....

    Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT. Our goal is to implement the interface that is provided for this ADT. Notice that there are two interfaces: OrderedListADT builds on ListADT. In this homework, you'll only be responsible for the OrderedListADT. Figure 1: UML Overview 1 Requirements Create a doubly linked implementation of the OrderedListADT interface. Note that the book includes most of the source code for a singly linked implementation of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT