Menu
Topics Index
...
`

Match Numbers


If you need explanation Read this topic

If you need Answer Take test on this topic

Match Numbers
What will be the output of the following program?
import java.util.*;

public class MatchNumbers {

    public static void main(String[] args) {
        List<Double> one = getAsList(25.56, 36.21, 16.00, 29.98, 31.7, 54.1, 32.2, 90.0);
        List<Double> two = getAsList(24.9, 76.0, 45.0, 55.9);
        System.out.println(matchNumbers(one, two));
    }

    public static List getAsList(Double... doubles) {
        List result = new ArrayList();
        for (Double d : doubles) { result.add(d); }
        return result;
    }

    private static Map<Double, Double> matchNumbers(List<Double> one, List<Double> two) {
        Map<Double, Double> result = new HashMap<Double, Double>();
        Collections.sort(one);  Collections.sort(two); Collections.reverse(two);
        for (Double d2 : two) {
            for (Double d1 : one) {
                if (d1 < d2) {
                    result.put(d2, d1);
                }
            }
        }
        return result;
    }
}

A. {36.21=76.0, 32.2=76.0, 31.7=76.0, 25.56=76.0, 
16.0=76.0, 54.1=76.0, 29.98=76.0}
B. {55.9=16.0, 76.0=16.0, 24.9=16.0, 45.0=16.0}
C. {55.9=54.1, 76.0=54.1, 24.9=16.0, 45.0=36.21}
D. Some other output
E. Compilation or Runtime Error
Topic: Java Collection Algorithms

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.

This dose is now closed and the winners are Sai Ram for the 'Popular Comment'. The 'lucky liker' is Mânïshå Mùlchåndânï. 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    2015-03-10 07:08:46


© meritcampus 2019

All Rights Reserved.

Open In App