How do I merge 2 separate lists in java based on index?



  • Say I have 2 lists.

    I want to merge both list into 1 list(mergeList in code) and expected output is: [11.65148,48.2490,0 11.6515,48.2490,0 11.6516,48.2491,0 ].

    Please suggest.

        List<Double> latitudeList = new ArrayList<Double>();
        List<Double> longitudeList = new ArrayList<Double>();
        List<Double> mergeList= new ArrayList<Double>();
        longitudeList.add(11.65148);
        longitudeList.add(11.6515);
        longitudeList.add(11.6516);
    
    
        latitudeList.add(48.2490);
        latitudeList.add(48.2490);
        latitudeList.add(48.2491);
    


  • Add the code below to have your lists merged and sorted.

        mergeList.addAll(latitudeList);
        mergeList.addAll(longitudeList);
        mergeList.sort(new Comparator<Double>() {
            @Override
            public int compare(Double o1, Double o2) {
                if(o1.doubleValue() < o2.doubleValue()){
                    return -1;
                }else if(o1.doubleValue() > o2.doubleValue()){
                    return 1;
                }
                return 0;
            }
        });
    

    Now:

    System.out.println(mergeList);
    

    Prints out:

    [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
    

    UPD:

        if(longitudeList.size() != latitudeList.size()){
            throw new IllegalStateException("longitudeList and latitudeList have to be of the same size");
        }
        for(int i = 0; i < longitudeList.size(); i++){
            mergeList.add(longitudeList.get(i));
            mergeList.add(latitudeList.get(i));
        }
    
        System.out.println(mergeList);
    

    Will print out:

    [11.65148, 48.249, 11.6515, 48.249, 11.6516, 48.2491]
    

    UPD 2:

    Better to override toString() method for your mergeList like it is shown below:

        List<Double> mergeList= new ArrayList<Double>(){
            @Override
            public String toString() {
                StringBuilder stringBuilder = new StringBuilder();
                for(int i = 1; i <= size(); i++){
                    stringBuilder.append(get(i - 1));
                    if(i % 3 == 0){
                        stringBuilder.append(" ");
                    }else{
                        stringBuilder.append(",");
                    }
                }
                stringBuilder.deleteCharAt(stringBuilder.length() - 1);
                return stringBuilder.toString();
            }
        };
    

    Then just add hardcoded altitude when you build up merged list:

        for(int i = 0; i < longitudeList.size(); i++){
            mergeList.add(longitudeList.get(i));
            mergeList.add(latitudeList.get(i));
            mergeList.add(0d);
        }
    
        System.out.println(mergeList);
    

    So the output would be:

    11.65148,48.249,0.0 11.6515,48.249,0.0 11.6516,48.2491,0.0
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2