Menu
Topics Index
...
`

Collections1958


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections1958
What will be the output of the following program?
import java.util.LinkedHashMap;

public class SwimmingStyles {
    public static void main(String[] args) {
        CustomMap cm = new CustomMap();
        cm.put("back", "stroke");
        System.out.print(cm);
        cm.put("front", "crawl");
        System.out.println(cm);
        cm.put("breast", "stroke");
        System.out.print(cm);
        cm.put("butterfly", "stroke");
        System.out.println(cm);
    }
}

class CustomMap extends LinkedHashMap<String, String> {
    private static final long serialVersionUID = 8169641341001802000L;
    protected boolean removeEldestEntry(java.util.Map.Entry<String, String> eldest) {
        return size() > 3 || eldest.getValue().contains("stroke");
    }
}

A. {back=stroke}{back=stroke, front=crawl}
{back=stroke, front=crawl, breast=stroke}{front=crawl, breast=stroke, butterfly=stroke}
B. {}{front=crawl}
{front=crawl, breast=stroke}{front=crawl, breast=stroke, butterfly=stroke}
C. {back=stroke}{back=stroke, front=crawl}
{back=stroke, front=crawl, breast=stroke}{back=stroke, front=crawl, breast=stroke, butterfly=stroke}
D. Some other output
E. Compilation Error or Runtime Error
Topic: LinkedHashMap In Java with Code Example

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 is B. first for the key "back" value stroke is added and due to the implementation of removeEldestEntry method, the entry with value as stroke is removed.. so for the first call it prints empty set. Next front key and crawl values are added and not deleted and same with the remaining keys and corresponding values.

Posted by Nikhil Sulibhavi    2014-05-16 12:45:40


Ans is B.. as the 1st element value is stroke so it is removed the list. then front=crawl vl be added when nxt element add even that also contains the stroke but that is not the eldest entry so it vl not be removed..

Posted by Om Prakash Om    2014-05-16 12:45:52


Congratulations Nikhil Sulibhavi. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-19 05:55:48


© meritcampus 2019

All Rights Reserved.

Open In App