Menu
Topics Index
...
`

Creating Map And Set


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating Map And Set
What will be the output of the following program?
import java.util.*;

public class PrintStatment11 {
    public void findDuplicateChars(String str) {
        Map<Character, Integer> dupMap = new HashMap<Character, Integer>();
        char[] chrs = str.toCharArray();
        for (Character ch : chrs) {
            if (dupMap.containsKey(ch)) {
                dupMap.put(ch, dupMap.get(ch) + 1);
            } else {
                dupMap.put(ch, 1);
            }
        }
        Set<Character> keys = dupMap.keySet();
        for (Character ch : keys) {
            if (dupMap.get(ch) > 1)
                System.out.println(ch + "--->" + dupMap.get(ch));
        }
    }
    public static void main(String a[]) {
        PrintStatment11 statmt = new PrintStatment11();
        statmt.findDuplicateChars("java programming");
    }
}

A. g--->2
r--->2
a--->3
m--->2
B. g--->3
r--->2
a--->3
m--->3
C. g--->1
r--->1
a--->1
m--->1
D. g--->2
r--->2
a--->3
b--->2
E. Compilation Error or Runtime Error
Topic: Java HashMap Implementation

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans =A

as map data type will compare the string one by one and if it becomes true then appropriate no will be printed after checking the condition of if statement..

Posted by Shubham Bansal    2014-10-22 13:00:54


ans a
findduplicatChar() function called and java programming string passed.
in this char frequency checked using Map and Set class.

sop prints ly char frequency which is greater than 1 ..so g r a m chars and their frequencies get printd

Posted by Maheshwari Natarajan    2014-10-22 13:09:29


answer A.    The HashMap class extends AbstractMap and implements the Map interface. It uses a hash
table to store the map. This allows the execution time of get( ) and put( ) to remain constant
even for large sets. HashMap is a generic class that has this declaration:
class HashMap<K, V>
Here, K specifies the type of keys, and V specifies the type of values.            in main method  findduplicatChar() function called and java programming string passed.
frequencies of char find using for loop.    
2nd for loop
sop prints ly char frequency which is greater than 1 ..so g r a m chars and their frequencies get printd in the order og givn string

Posted by Ananda Kumar    2014-10-22 13:37:13


Ans is A...

HashMap is Implemented Based on the HashTable..HashTable is Synchronized where as HashMap is UnSynchronized..HashpMap does not provide Insertion order...and HashMap allows nul also..here we create and hashMap with default size and default loadfactor..
default load factor is 75%,loadfactor means when hashmap filled with 75% of elements than size of the map is get Increased..
here we convert String into char array.

by using the for each loop we are travelling throug the array..

put(key,value)--> insert an key value pair into Map

containsKey()--> wil return true if the Map already contains the key.

get(key)--> wil return the value which is assigned to the key

example

a--2
f--3
s---1
get(a)--wil return 2

at the end of the for loop map is assigned with keys and their count...

Ex: aabbccc

a-2
b-23
c--3
keyset()---wil return the all keys of the map as a Set...
here we are getting every value of the key from the keySet..if the value is >1 than we are printing..
finally we are printg the chars which repeats more than once..

Posted by Uday Kumar    2014-10-22 14:41:15


This dose is now closed and the winners are Maheshwari Natarajan,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Shubham Bansal for the 'Popular Comment'. The 'lucky liker' is Urvashi Arya. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-10-23 01:43:06


© meritcampus 2019

All Rights Reserved.

Open In App