Simple Collection Manipulation in Java - Sets, Trees, and Vectors

  • Java
  • Thread starter muna580
  • Start date
If not, then it shouldn't be in brackets. It's up to the person giving the assignment how they want the output to be formatted.
  • #1
muna580
My professor told me to do everything in this program as asked. But I am a little bit confused.

In the code below, take a look at the part I made in BOLD. Well, first of all, he said to display the elements in vector1. But there is only 1 element in vector1. Well, I undestand its printable, but look at the next step. He said to create vector2 an add all elements in vector1 to vector 2. Basically, I created teh same thing all over again with a diffrent name. What ever I printed out in vector1 will be printed out in vector2.

import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.Vector;
import java.util.List;

class CollectionExtraCredit
{

public static void main(String[] arg)
{
Iterator i;

Set s1 = new HashSet();
s1.add("Mary");
// Add the following names - Bernadene, Elizabeth, Gene, Elizabeth, Clara
i = s1.iterator();

System.out.println("HashSet s1 \n" + display(i));

// Create another HashSet object set2
// Add the following members - John, Jerry, Elizabeth
// Add set2 to set1
// Display set2

Set tree1 = new TreeSet();
tree1.add("Mary");
i = tree1.iterator();

// Next, use the same data to create TreeSet tree1 - I did it for you.
// Display the elements of tree1
// Create tree2 and add the elements in tree1 to tree2
// Display tree2
System.out.println("TreeSet s1 \n" + display(i));


List vector1 = new Vector(5);
vector1.add("Mary");
// Thirdly,use the same data again to create Vector vector1 - I did it for you.
// Display the elements of vector1

// Create vector2 and add the elements in vector1 to vector2
// Display vector2
i = vector1.iterator();
System.out.println("Vector vector1 \n" + display(i));

}

static String display(Iterator i)
{
if (!i.hasNext())
return "";
else
return i.next() + "\n" + display(i);
}
}
 
Technology news on Phys.org
  • #2
You haven't declared vector2. Do so, and then you need to use a copy function. e.g. Vector vector2 = vector1.clone();

Or, and this might be better practice for you, is to make your own clone function that creates a new vector and iterates through vector1, adding all the objects in vector1 to the new vector, and returns the new vector. Then you can say Vector vector2 = clone(vector2);
 
  • #3
This is what I did. But I am not sure if it is correct becaue for TreeSet, and Vector, only get Mary as the output. Am I suppoe to add something else?

Code:
import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.Vector;
import java.util.List;

class CollectionExtraCredit
{

	public static void main(String[] arg)
	{
		Iterator i;
		
		Set s1 = new HashSet();
		s1.add("Mary");

		// Add the following names - Bernadene, Elizabeth, Gene, Elizabeth, Clara
		
		s1.add("Bernadene");
		s1.add("Elizabeth");
		s1.add("Gene");
		s1.add("Elizabeth");
		s1.add("Clara");

		i = s1.iterator();
		
		System.out.println("HashSet s1 \n" + display(i));
		
		// Create another HashSet object set2 
		// Add the following members - John, Jerry, Elizabeth
		// Add set2 to set1 
		// Display set2
		
		Set s2 = new HashSet();
		s2.add("John");
		s2.add("Jerry");
		s2.add("Elizabeth");
		
		s2.addAll(s1); 
		
		i = s2.iterator();
		
		System.out.println("HashSet s2 \n" + display(i));		Set tree1 = new TreeSet();
		tree1.add("Mary");
		i = tree1.iterator();
		System.out.println("TreeSet tree1 \n" + display(i));
		
		// Next, use the same data to create TreeSet tree1 - I did it for you.
		// Display the elements of tree1
		// Create tree2 and add the elements in tree1 to tree2
		// Display tree2
		
		Set tree2 = new TreeSet();
		tree2.addAll(tree1);
		i = tree2.iterator();
		System.out.println("TreeSet tree2 \n" + display(i));

		
		List vector1 = new Vector(5);
		vector1.add("Mary");
		// Thirdly,use the same data again to create Vector vector1 - I did it for you.
		// Display the elements of vector1
		i = vector1.iterator(); 
		System.out.println("Vector vector1 \n" + display(i));

		// Create vector2 and add the elements in vector1 to vector2
		// Display vector2
		
		List vector2 = new Vector(5);
		vector2.addAll(vector1);
		i = vector2.iterator();
		System.out.println("Vector vector2 \n" + display(i));
	}
	
	static String display(Iterator i)
	{
		if (!i.hasNext())
			return "";
		else
			return i.next() + "\n" + display(i);
	}
}

This is my output.

HashSet s1
Gene
Bernadene
Clara
Mary
Elizabeth

HashSet s2
Jerry
Bernadene
Gene
Mary
Clara
Elizabeth
John

TreeSet tree1
Mary

TreeSet tree2
Mary

Vector vector1
Mary

Vector vector2
Mary

Press any key to continue...
 
Last edited by a moderator:
  • #4
Well, it looks correct to me, but you said you expected to get something other than "Mary"? What output should you get?
 
  • #5
0rthodontist said:
Well, it looks correct to me, but you said you expected to get something other than "Mary"? What output should you get?

One more thing, is there suppose to be any output in bracket? Like for example

[Mary]

is that suppsoe to happen?
 
  • #6
Did the assignment ask for your output to be in brackets?
 

FAQ: Simple Collection Manipulation in Java - Sets, Trees, and Vectors

What are the differences between Sets, Trees, and Vectors in Java?

Sets, Trees, and Vectors are all data structures in Java that allow for efficient manipulation and storage of collections of objects. However, they differ in terms of their underlying implementation and the operations that can be performed on them. Sets are unordered collections that do not allow for duplicate elements. Trees are hierarchical structures that maintain a specific ordering of elements. Vectors are dynamic arrays that can grow or shrink in size.

How do I add and remove elements from a Set, Tree, or Vector in Java?

To add an element to a Set, use the add() method. To remove an element, use the remove() method. For Trees, elements can be added using the put() method and removed using the remove() method. Vectors have methods such as addElement() and removeElement() for adding and removing elements.

Can I iterate over elements in a Set, Tree, or Vector in Java?

Yes, all three data structures support iteration over their elements. Sets and Trees can be iterated using the iterator() method, while Vectors have methods such as elements() and listIterator() for iteration.

How do I check if a particular element is present in a Set, Tree, or Vector in Java?

To check if a Set contains a particular element, use the contains() method. For Trees, the containsKey() and containsValue() methods can be used. Vectors have the contains() method to check for the presence of an element.

Are there any differences in terms of performance between Sets, Trees, and Vectors in Java?

Yes, the performance of these data structures can vary depending on the specific operation being performed. Sets and Trees have faster lookup times, while Vectors have faster insertion and deletion times. It is important to consider the specific needs of your program when choosing which data structure to use.

Similar threads

Replies
2
Views
3K
Replies
3
Views
2K
Replies
5
Views
2K
Replies
7
Views
2K
Replies
4
Views
2K
Replies
11
Views
5K
Replies
2
Views
2K
Back
Top