In: Computer Science
The following is a set of tree-set test programs that show the following outputs: Switch to ArrayList, LinkedList, Vector, TreeMap, and HashMap to display similar output results.
Results:
Tree set example!
Treeset data: 12 34 45 63
Treeset Size: 4
First data: 12
Last Data: 63
Removing data from a tree set
Current tree set elements: 12 34 63
Current tree set size :3
Tree set empty.
Example code
import java.util.Iterator;
import java.util.TreeSet;
public class TreeDemo2 {
public static void main(String[] args) {
System.out.println("Tree set example!\n");
TreeSet<Integer> Tree = new TreeSet<Integer>();
Tree.add(12); Tree.add(63); Tree.add(34); Tree.add(45);
// Review Alignment
Iterator<Integer> iterator = Tree.iterator();
System.out.print("Treeset data: ");
// Display Tree Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
// Review if empty
if (Tree.isEmpty()) {
System.out.print("Tree set is empty.");
} else {
System.out.println("tree set size: " + Tree.size());
}
// Retrieve the first data in a tree set
System.out.println("first data: " + Tree.first());
// Retrieve the last data in a tree set
System.out.println("last data: " + Tree.last());
if (Tree.remove(45)) { // Elimination of elements by value
System.out.println("Removing data from a tree set");
} else {
System.out.println("Data does not exist!");
}
System.out.print("Current tree set elements: ");
iterator = Tree.iterator();
// Display Tree Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Current tree set size: " + Tree.size());
// Remove All
Tree.clear();
if (Tree.isEmpty()) {
System.out.print("Tree set is empty.");
} else {
System.out.println("tree set size: " + Tree.size());
}
}
}
Please write it in Java code. And please write an annotation.
Thank you:)
Required Code in JAVA:
I have written 5 different codes for different collections and used the same values as used in the example given.
ArrayList:
import java.util.Iterator;
import java.util.ArrayList;
public class Main
{
public static void main(String[] args) {
System.out.println("ArrayList example!\n");
ArrayList<Integer> ArrList = new
ArrayList<Integer>();
ArrList.add(12); ArrList.add(63); ArrList.add(34);
ArrList.add(45);
// Review Alignment
Iterator<Integer> iterator = ArrList.iterator();
System.out.print("ArrayList data: ");
// Display ArrList Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
// Review if empty
if (ArrList.isEmpty()) {
System.out.print("ArrList set is empty.");
} else {
System.out.println("ArrList set size: " + ArrList.size());
}
// Retrieve the first data in a ArrList set
System.out.println("first data: " + ArrList.get(0));
// Retrieve the last data in a ArrList set
System.out.println("last data: " +
ArrList.get(ArrList.size()-1));
int index = ArrList.indexOf(45);
if (index!=-1) { // Elimination of elements by value
ArrList.remove(index);
System.out.println("Removing data from a ArrList set");
} else {
System.out.println("Data does not exist!");
}
System.out.print("Current ArrList set elements: ");
iterator = ArrList.iterator();
// Display ArrList Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Current ArrList set size: " +
ArrList.size());
// Remove All
ArrList.clear();
if (ArrList.isEmpty()) {
System.out.print("ArrList set is empty.");
} else {
System.out.println("ArrList set size: " + ArrList.size());
}
}
}
LinkedList:
import java.util.Iterator;
import java.util.LinkedList;
public class Main
{
public static void main(String[] args) {
System.out.println("LinkedList example!\n");
LinkedList<Integer> Link = new
LinkedList<Integer>();
Link.add(12); Link.add(63); Link.add(34); Link.add(45);
// Review Alignment
Iterator<Integer> iterator = Link.iterator();
System.out.print("LinkedList data: ");
// Display Link Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
// Review if empty
if (Link.isEmpty()) {
System.out.print("Link set is empty.");
} else {
System.out.println("Link set size: " + Link.size());
}
// Retrieve the first data in a Link set
System.out.println("first data: " + Link.get(0));
// Retrieve the last data in a Link set
System.out.println("last data: " + Link.get(Link.size()-1));
int index = Link.indexOf(45);
if (index!=-1) { // Elimination of elements by value
Link.remove(index);
System.out.println("Removing data from a Link set");
} else {
System.out.println("Data does not exist!");
}
System.out.print("Current Link set elements: ");
iterator = Link.iterator();
// Display Link Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Current Link set size: " + Link.size());
// Remove All
Link.clear();
if (Link.isEmpty()) {
System.out.print("Link set is empty.");
} else {
System.out.println("Link set size: " + Link.size());
}
}
}
Vector:
import java.util.Iterator;
import java.util.Vector;
public class Main
{
public static void main(String[] args) {
System.out.println("Vector example!\n");
Vector<Integer> Vec = new Vector<Integer>();
Vec.add(12); Vec.add(63); Vec.add(34); Vec.add(45);
// Review Alignment
Iterator<Integer> iterator = Vec.iterator();
System.out.print("Vector data: ");
// Display Vec Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
// Review if empty
if (Vec.isEmpty()) {
System.out.print("Vec set is empty.");
} else {
System.out.println("Vec set size: " + Vec.size());
}
// Retrieve the first data in a Vec set
System.out.println("first data: " + Vec.get(0));
// Retrieve the last data in a Vec set
System.out.println("last data: " + Vec.get(Vec.size()-1));
int index = Vec.indexOf(45);
if (index!=-1) { // Elimination of elements by value
Vec.remove(index);
System.out.println("Removing data from a Vec set");
} else {
System.out.println("Data does not exist!");
}
System.out.print("Current Vec set elements: ");
iterator = Vec.iterator();
// Display Vec Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Current Vec set size: " + Vec.size());
// Remove All
Vec.clear();
if (Vec.isEmpty()) {
System.out.print("Vec set is empty.");
} else {
System.out.println("Vec set size: " + Vec.size());
}
}
}
TreeMap:
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Map;
public class Main
{
public static void main(String[] args) {
System.out.println("TreeMap example!\n");
TreeMap<Integer, Integer> TMap = new TreeMap<Integer,
Integer>();
TMap.put(12, 0); TMap.put(63, 0); TMap.put(34, 0); TMap.put(45,
0);
// Review Alignment
System.out.print("TreeMap data: ");
// Display TMap Set Data
int i=0;
Object first=0, last=0;
for(Map.Entry m:TMap.entrySet()){
System.out.print(m.getKey()+" ");
if(i==0)
first = m.getKey();
else
last = m.getKey();
i++;
}
System.out.println();
// Review if empty
if (TMap.isEmpty()) {
System.out.print("TMap set is empty.");
} else {
System.out.println("TMap set size: " + TMap.size());
}
// Retrieve the first data in a TMap set
System.out.println("first data: " + first);
// Retrieve the last data in a TMap set
System.out.println("last data: " + last);
// int index = TMap.indexOf(45);
if(TMap.containsKey(45)){
TMap.remove(45);
System.out.println("Removing data from a TMap set");
} else {
System.out.println("Data does not exist!");
}
System.out.print("Current TMap set elements: ");
for(Map.Entry m:TMap.entrySet()){
System.out.print(m.getKey()+" ");
if(i==0)
first = m.getKey();
else
last = m.getKey();
}
System.out.println();
System.out.println("Current TMap set size: " + TMap.size());
// Remove All
TMap.clear();
if (TMap.isEmpty()) {
System.out.print("TMap set is empty.");
} else {
System.out.println("TMap set size: " + TMap.size());
}
}
}
HashMap:
import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;
public class Main
{
public static void main(String[] args) {
System.out.println("HashMap example!\n");
HashMap<Integer, Integer> HMap = new HashMap<Integer,
Integer>();
HMap.put(12, 0); HMap.put(63, 0); HMap.put(34, 0); HMap.put(45,
0);
// Review Alignment
System.out.print("HashMap data: ");
// Display HMap Set Data
int i=0;
Object first=0, last=0;
for(Map.Entry m:HMap.entrySet()){
System.out.print(m.getKey()+" ");
if(i==0)
first = m.getKey();
else
last = m.getKey();
i++;
}
System.out.println();
// Review if empty
if (HMap.isEmpty()) {
System.out.print("HMap set is empty.");
} else {
System.out.println("HMap set size: " + HMap.size());
}
// Retrieve the first data in a HMap set
System.out.println("first data: " + first);
// Retrieve the last data in a HMap set
System.out.println("last data: " + last);
// int index = HMap.indexOf(45);
if(HMap.containsKey(45)){
HMap.remove(45);
System.out.println("Removing data from a HMap set");
} else {
System.out.println("Data does not exist!");
}
System.out.print("Current HMap set elements: ");
for(Map.Entry m:HMap.entrySet()){
System.out.print(m.getKey()+" ");
if(i==0)
first = m.getKey();
else
last = m.getKey();
}
System.out.println();
System.out.println("Current HMap set size: " + HMap.size());
// Remove All
HMap.clear();
if (HMap.isEmpty()) {
System.out.print("HMap set is empty.");
} else {
System.out.println("HMap set size: " + HMap.size());
}
}
}