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);
-
Do you want to merge lists based on index instead of values?Vishal Aggarwal– Vishal Aggarwal2018年09月19日 10:20:53 +00:00Commented Sep 19, 2018 at 10:20
1 Answer 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
-
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.Nikhil Vernekar– Nikhil Vernekar2018年09月19日 07:33:48 +00:00Commented Sep 19, 2018 at 7:33
-
I gave you the answer that solves the issue stated in your example. Rework your example.Alexey R.– Alexey R.2018年09月19日 07:35:52 +00:00Commented Sep 19, 2018 at 7:35
-
I have updated the example. You can take a look now.Nikhil Vernekar– Nikhil Vernekar2018年09月19日 07:43:30 +00:00Commented Sep 19, 2018 at 7:43
-
Check updated answerAlexey R.– Alexey R.2018年09月19日 07:48:11 +00:00Commented 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?Nikhil Vernekar– Nikhil Vernekar2018年09月19日 08:14:34 +00:00Commented Sep 19, 2018 at 8:14