0

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);
asked Sep 19, 2018 at 7:22
1
  • Do you want to merge lists based on index instead of values? Commented Sep 19, 2018 at 10:20

1 Answer 1

1

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
answered Sep 19, 2018 at 7:27
12
  • Hi Alexey, Thanks for your quick help. But i cannot use compare. The actual lists with which i am working contain longitude and latitude values. So list1 has longitude values and list2 has latitude values. I need list3 with merged values as [ longitude,latitude,longitude,latitude,...etc]. Appreciate your help. Commented Sep 19, 2018 at 7:33
  • I gave you the answer that solves the issue stated in your example. Rework your example. Commented Sep 19, 2018 at 7:35
  • I have updated the example. You can take a look now. Commented Sep 19, 2018 at 7:43
  • Check updated answer Commented Sep 19, 2018 at 7:48
  • Hi Alexey. I have updated the expected a little. Can you please suggest how can i get such output? Commented Sep 19, 2018 at 8:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.