Menu
Question Index
...


Write a program to map the elements from one list to the nearest element in the other list. Note that the values in the map should be unique, which means no two keys should have the same value. Only map to the nearest value, if the nearest value is taken, then do not map it. Try to get as many mappings as possible. See requirements below for more details.

Input (List, List) Map
[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] [515.0=514.0, 510.0=511.0, 520.0=521.0, 525.0=523.0]
[180.00, 160.00, 125.00, 115.00, 130.00, 150.00, 110.00], [100.00, 170.00, 201.00, 105.00, 140.00, 120.00, 210.00] [110.0=105.0, 160.0=170.0, 130.0=140.0, 115.0=120.0]
[180.00, 160.00, 125.00, 115.00, 130.00, 200.0, 150.00, 110.00], [100.00, 170.00, 201.00, 105.00, 190.0, 140.00, 120.00, 210.00] [110.0=105.0, 160.0=170.0, 130.0=140.0, 115.0=120.0, 200.0=201.0, 180.0=190.0]
[110.00, 120.00, 130.00, 140.00, 150.00, 160.00], [105.00, 115.00, 125.00, 127.00, 135.00, 138.00, 145.00, 155.00, 165.00] [110.0=105.0, 160.0=155.0, 130.0=127.0, 120.0=115.0, 140.0=138.0, 150.0=145.0]
[500.00, 530.00, 510.00, 540.00, 550.00, 520.00], [509.00, 511.00, 519.00, 501.00, 521.00, 560.00, 510.00] [510.0=510.0, 550.0=560.0, 520.0=519.0, 530.0=521.0, 500.0=501.0]

class MapToNearestUniqueNumber

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

    }


private static HashMap mapToNearestNumber(List<Double> firstList, List<Double> secondList) {
//Write code here to map the elements as explained in the requirements above
}
}

Doubts

Problems

Topic: Java HashMap Implementation

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App