Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Map and Set in Java Eclipse... I\'m kind of lost. I do have to use the code I\'m

ID: 3763904 • Letter: M

Question

Map and Set in Java Eclipse...

I'm kind of lost. I do have to use the code I'm attaching. Thanks in advance!

2. In the Set class, use each method that I provided to do something with the set object.

a) You must use all of the methods.

b) You can use them in whatever way you want.

c) Your usage of those methods should be able to show that you know how they work. Note: You can use the documentation provided by Eclipse to see how each method works. To do this simply hover your pointer over the method and a popup appears with information on the method.

3. In the Map class, use each method that I provided to do something with the map object.

a) Do the same thing as you did in set class.

4. Create a comment in both classes containing your ideas on how this class could be useful when writing a program.

Set Class:

import java.util.HashMap;
import java.util.HashSet;


public class Set {

   public static void main(String[] args) {
       HashSet<String> set=new HashSet<String>();
       set.add(e);
       set.clear();
       set.clone();
       set.contains(o);
       set.isEmpty();
       set.iterator();
       set.remove(o);

      
          
   }

}

Map Class:

import java.util.HashMap;

public class Map {

   public static void main(String[] args) {

       // you can use this HashMap to Store the score of an exam of several

       // students

       HashMap<String, Integer> map = new HashMap<String, Integer>();

       map.put(key, value);

       map.clear();

       map.clone();

       map.containsKey(key);

       map.containsValue(value);

       map.remove(key);

       map.replace(key, value);

       map.size();

       map.keySet();

       map.get(key);

   }

}

Explanation / Answer

import java.util.HashSet;
import java.util.Iterator;

/**
*
*/

/**
* @author srinu
*
*/
public class SetTest {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub

       HashSet<String> set = new HashSet<String>();
       set.add("first");
       set.add("second");
       set.add("third");
       System.out.println("Hashset: " + set);

      
       System.out.println("Does HashSet contains first element? "
               + set.contains("first"));
      
       Iterator<String> setIt = set.iterator();
       System.out.println("Iterating Set Using Iterator");
       while (setIt.hasNext()) {
           String string = setIt.next();
           System.out.println(string);

       }
       System.out.println("Is HashSet empty? "+set.isEmpty());

       System.out.println("Before Remove Hashset: " + set);
       set.remove("third");
       System.out.println("Removes third from Hashset: " + set);
      
      
       HashSet clonedSet=(HashSet) set.clone();
       System.out.println("Cloned Set :"+clonedSet);
       set.clear();
       System.out.println("After Clearing Set:"+set);
      
   }

}

OUTPUT:

Hashset: [second, third, first]
Does HashSet contains first element? true
Iterating Set Using Iterator
second
third
first
Is HashSet empty? false
Before Remove Hashset: [second, third, first]
Removes third from Hashset: [second, first]
Cloned Set :[second, first]
After Clearing Set:[]

import java.util.HashMap;

/**
*
*/

/**
* @author srinu
*
*/
public class MapTest {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       HashMap<String, Integer> map = new HashMap<String, Integer>();
       map.put("first", 1);
       map.put("second", 2);
       map.put("third", 3);
       System.out.println("initial map :" + map);

       HashMap<String, Integer> cloneMap = (HashMap<String, Integer>) map
               .clone();

       System.out.println("Cloned Map :" + cloneMap);
       System.out.println("The hashmap contains key first ?"
               + map.containsKey("first"));
       System.out.println("The hashmap contains Value 2 ?"
               + map.containsValue(2));
       System.out.println("before removing third :" + map);
       map.remove("third");
       System.out.println("After removing third :" + map);
       System.out.println("before replacing key first with value 5 :" + map);
       map.put("first", 5);
       System.out.println("after replacing key first with value 5 :" + map);
       System.out.println("Map size :" + map.size());
       System.out.println("Map key set :" + map.keySet());
       System.out.println("Value of second: " + map.get("second"));
       map.clear();
       System.out.println("After Clear the map :"+map);
   }

}

OUTPUT:

initial map :{second=2, third=3, first=1}
Cloned Map :{second=2, first=1, third=3}
The hashmap contains key first ?true
The hashmap contains Value 2 ?true
before removing third :{second=2, third=3, first=1}
After removing third :{second=2, first=1}
before replacing key first with value 5 :{second=2, first=1}
after replacing key first with value 5 :{second=2, first=5}
Map size :2
Map key set :[second, first]
Value of second: 2
After Clear the map :{}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote