Question

Answer the following Java Collection/Map Question: The Map interface has a method putAll() that adds all...

Answer the following Java Collection/Map Question:

The Map interface has a method putAll() that adds all entries in map2 into map1. Write your own putAll2 method ( a generic method), given the two maps as parameters the method will add all entries in map2 into map1 and print map1. Show that the method is working in an application class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program : Created as PutAll2.java in Eclipse

import java.util.*;
public class PutAll2
{   
void putAll2(Map m2, Map m1)
   {
       Set<Map.Entry<String, Double>> mentry = m2.entrySet();//Creating set
Iterator<Map.Entry<String, Double>> mapIterator = mentry.iterator();//Creating Iterator
while (mapIterator.hasNext())
{
Map.Entry<String, Double> entry = mapIterator.next();
m1.put(entry.getKey(),entry.getValue());//adding map2 element into map1
}
   }
public static void main(String[] args)
{   
PutAll2 mp = new PutAll2();//Creating object of class
Map<String, String> map2 = new HashMap<>(); //creating map2
map2.put("1", "A"); //Adding element to map2
map2.put("2", "B");
map2.put("3", "C");
map2.put("4", "D");
map2.put("5", "E");
System.out.println("Elements in Map2:");
System.out.println(map2); //Printing content of map 2
Map<String, String> map1 = new HashMap<>();//Creating map1
mp.putAll2(map2,map1);//Passing map2 and map1 to method
System.out.println("Elements in Map1:");
System.out.println(map1); //Printing content of map 1   
}
}

Explanation : map2 is created using HashMap. Elements are added into map2. map1 is also created and pass both map1 and map2 to the method putAll2. Which put all elements in map2 into map1. Content of both map is displayed.

Screenshot :

Add a comment
Know the answer?
Add Answer to:
Answer the following Java Collection/Map Question: The Map interface has a method putAll() that adds all...
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
  • Answer the following Java Collection/Map Question: The antonym map is a map that associating a word...

    Answer the following Java Collection/Map Question: The antonym map is a map that associating a word (the key) to its antonym. Suppose the antonym map wasn't necessarily symmetric: it might have an entry that maps the key "short" to "tall", but not have an entry that maps the key "tall" to "short": there is no key "tall" in the map. Yet, the antonym relationship certainly is symmetic: if the opposite of "short" is "tall", the opposite of "tall" is "short"....

  • These are all different questions, please answer them separately and label each question Write a static...

    These are all different questions, please answer them separately and label each question Write a static method named conflicts that takes two Maps using Strings as keys and Integers as values. The method should return a Set of Strings. The Set returned by conflicts should be a list of all of the keys that are in conflict between the two Maps. A conflict occurs when two keys point to a different value in the two different maps. (For example, map1...

  • in java • Create an interface called Person. In this interface create a method called printData()...

    in java • Create an interface called Person. In this interface create a method called printData() • Implement interface Person by classes Student, Teacher, Admin. You need to think the relevant data attributes for each class. Define atleast 4 attributes for each class. • In each class override a method toString() and create a string which holds all data • In the method printData in each class print the data using toString() method. Design the UML and write a code...

  • JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a...

    JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a Utils class that takes as parameter a collection of Pair<K,V> objects and returns a new collection object (use ArrayList<...>) with the pair elements from collection col sorted in ascending order. For comparing pairs, the K type must implement Comparable<....> Use the proper type constraints. b) Write a main() function in Utils.java that tests the sortPairCollection() with K=String and V=Integer.

  • Can some please help me to answer these two questions. Write a program in Java create...

    Can some please help me to answer these two questions. Write a program in Java create a class diagram fragment with two classes: User and Patient. Patient is a sub-class of User. User has a method getName(), which is both over-loaded and over-ridden in the subclass In Java write an abstract class called User. User has one abstract method called authenticate, which takes two string parameters. Write a concrete sub-class of User called Administrator with an implementation of the authenticate...

  • java 4. Java allows for the use of generic typing to do away with all type...

    java 4. Java allows for the use of generic typing to do away with all type checking of parameters to methods.​ TRUE OR FALSE ? 5 How many interfaces can a class implement? 6.When a programmer writes a class definition to implement an interface, he/she must write the method body for at least one of the method headings specified in the interface TRUE OR FALSE? 7. Declaring a formal parameter's type as T is the same thing as declaring the...

  • please help answer this whole question. it is all supoosed to be coded in java. i...

    please help answer this whole question. it is all supoosed to be coded in java. i will rate :) linked below is the wikipedia entry that is linked in the image below. https://en.m.wikipedia.org/wiki/Histogram QUESTION 3 10 points Save Answer Read the Wikipedia entry on what a histogram is. Write a class or collection of classes that will print a vertically-stacked histogram, using the character to build the bars. The constructor should have the range (min, max) and number of bins...

  • Using Java programming, The Set interface in the API has the methods addAll(Collection<? exte...

    Using Java programming, The Set interface in the API has the methods addAll(Collection<? extends E> c) and retainAll(Collections<? > c), which acts like union and intersection respectively. However, these modify the set it is called on. This means after set1.addAll(set2) or set1.retainsAll(set2) is executed, set1 will be the union or intersection of the two sets, respectively. Choose ONE of the implementations below Ordered lists Binary search trees Hash tables Add a method in that implementation that returns the union of...

  • Problemi: Create a new Java Application that has the following methods: 1. A method that generate...

    Problemi: Create a new Java Application that has the following methods: 1. A method that generate a binary search tree (BST) where the number of nodes and the range of the values are from a file 2. A recursive method to print the BST in preorder traversal 3. A recursive method to print the BST in post-order traversal 4. A recursive method to print the BST in in-order traversal 5. A method to find the largest value in a BST...

  • JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of...

    JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of this document) that specifies the protocols for a priority queue with generic values and priorities, where the priorities are discretely ordered rather than comparable. Implement the class ListofQueuesPQ implements PriorityQueue to fulfill the requirements of the interface (see the Javadoc in the interface’s source code). Use an ListofQueues in order to fulfill the needs of the interface. You will need to make sub-classes within...

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