Map the numbers from one list with the nearest number in the other list - Java Quiz Question



Write a program to map the numbers from one list with the nearest in the other list. If the two numbers have same distance, the one which comes first in the list should be used.

Input (List, List) Map
[500.00, 510.00, 515.00, 520.00, 525.00], [501.00, 511.00, 522.00, 518.00, 525.00] [511.0=510.0, 518.0=520.0, 522.0=520.0, 525.0=525.0, 501.0=500.0]
[500.00, 510.00, 515.00, 520.00, 525.00], [511.00, 512.00, 514.00, 521.00, 527.00, 530.00, 518.00, 523.00] [512.0=510.0, 511.0=510.0, 514.0=515.0, 518.0=520.0, 521.0=520.0, 523.0=525.0, 527.0=525.0, 530.0=525.0]
[100.00, 105.00, 110.00, 119.00, 121.00, 136.00, 144.00], [95.00, 103.00, 150.00, 130.00, 111.00, 129.00, 78.00, 147.00] [129.0=136.0, 111.0=110.0, 103.0=105.0, 147.0=144.0, 130.0=136.0, 78.0=100.0, 150.0=144.0, 95.0=100.0]
[23.00, 50.00, 35.00, 40.00, 39.00, 70.00, 85.00], [100.00, 90.00, 60.00, 83.00, 50.00, 33.00, 23.00, 45.00] [90.0=85.0, 45.0=50.0, 23.0=23.0, 60.0=50.0, 33.0=35.0, 50.0=50.0, 83.0=85.0, 100.0=85.0]
[133.00, 150.00, 500.00, 510.00, 515.00, 199.00], [300.00, 318.00, 200.00, 210.00, 218.00, 250.00, 290.00] [290.0=199.0, 250.0=199.0, 200.0=199.0, 218.0=199.0, 318.0=199.0, 210.0=199.0, 300.0=199.0]

class MapToNearestNumber

{    public static void main(String s[])
    {
        ArrayList first = new ArrayList();
        first.add(500.00);
        first.add(510.00);
        first.add(515.00);
        first.add(520.00);
        first.add(525.00);
        ArrayList second = new ArrayList();
        second.add(501.00);
        second.add(511.00);
        second.add(522.00);
        second.add(518.00);
        second.add(525.00);
        HashMap output = mapToNearestNumber(first, second);
        System.out.println("The resultant map is : ");
        System.out.println(output);

    }


private static HashMap mapToNearestNumber(List<Double> first, List<Double> second) {
//Write code here to map elements from second list to the nearest number in the first list
}
}