In: Computer Science
Using Java, Complete LinkedListSet:
package Homework3;
public class LinkedListSet <T> extends LinkedListCollection
<T> {
LinkedListSet() {
}
public boolean add(T element) {
// Code here
return true;
}
}
Homework3 class:
public class Homework3 {
public static void main(String[] args) {
ArrayCollection ac1 = new ArrayCollection(); // Calling
Default
Constructor
ArrayCollection ac2 = new ArrayCollection(2); // Calling
overloaded
constructor
ArraySet as1 = new ArraySet();
ac2.add("Apple");
ac2.add("Orange");
ac2.add("Lemon"); // This can't be added into ac2 as collection is
full
System.out.println(ac2.remove("Apple")); // This should return
true
System.out.println(ac2);
ac2.enlarge(10);
ac2.add("Watermelon");
System.out.println("Equals: " + ac1.equals(ac2));
as1.add("Avocado");
as1.add("Avocado"); // This will not be added, since the
collection is "set"
}
}
import java.util.LinkedHashSet;
public class Demo
{
public static void main(String[] args)
{
LinkedHashSet<String>
linkedset =
new
LinkedHashSet<String>();
// Adding element to
LinkedHashSet
linkedset.add("A");
linkedset.add("B");
linkedset.add("C");
linkedset.add("D");
// This will not add new element
as A already exists
linkedset.add("A");
linkedset.add("E");
System.out.println("Size of
LinkedHashSet = " +
linkedset.size());
System.out.println("Original
LinkedHashSet:" + linkedset);
System.out.println("Removing D from
LinkedHashSet: " +
linkedset.remove("D"));
System.out.println("Trying to
Remove Z which is not "+
"present: " + linkedset.remove("Z"));
System.out.println("Checking if A
is present=" +
linkedset.contains("A"));
System.out.println("Updated
LinkedHashSet: " + linkedset);
}
}