[英]How to build a new list containing all entries from existing list AND a copy of each entry with one field modified?
[英]How to Compare one value with all list entries in one step?
對於線性距離的特殊斜率,我需要檢查給定緯度/經度相對於存儲在地圖中的所有其他點的距離是否大於10 km的新點。 我想避免依次比較地圖中的所有點。
我嘗試了以下代碼:
private static Map<Long,String> getTop10DSEGs() throws IOException {
Map<Long,String> top10Dsegs = new HashMap<>();
List<String> allDsegs = loadDsegRanking(rFiles);
//the list allDsegs is a csv with the following structure
//dsegID, Latitide, Longitude
//1167317891449551556,51.435550689697266,6.695819854736328
double LatFromList, LonFromList;
double LatFromMap, LonFromMap;
String[] listArray;
String[] mapArray;
Long firstDseg = Long.parseLong(allDsegs.get(0).substring(0, 19));
top10Dsegs.put(firstDseg, allDsegs.get(0).substring(20));
//Iterating through all dsegs
for(String line : allDsegs){
Long nextID = null;
String nextCoordinates = "0.0000,0.0000";
listArray = line.split(",");
LatFromList = Double.valueOf(listArray[1]);
LonFromList = Double.valueOf(listArray[2]);
//checking all coordinates of maped dsegs
for(Map.Entry<Long, String> entry : top10Dsegs.entrySet()){
List<Double> distanceList = new ArrayList<>();
mapArray = entry.getValue().split(",");
LatFromMap = Double.valueOf(mapArray[0]);
LonFromMap = Double.valueOf(mapArray[1]);
//calculating the distance between the next city from the list and the other dsegs from the map
//result of dist is a double and it's metric is Km.
Double dist = Implements.DistanceCalculator.distance(LatFromMap, LonFromMap, LatFromList, LonFromList, "K");
distanceList.add(dist);
for(Double value : distanceList){
if (dist>10){
nextID = Long.parseLong(listArray[0]);
nextCoordinates = listArray[1] + "," + listArray[2];
}
}
}
if(nextID != null && top10Dsegs.size() < 10){
top10Dsegs.put(nextID, nextStartCoordinates);
}
return top10Dsegs;
}
為了避免計算和比較所有已經存儲的點與新點之間的距離,您可以使用由(Multi)Map或數組實現的某種網格(或矩陣)。 網格的每個像元可能具有sqrt(10km)的大小,並包含具有相關坐標的所有點。 通過將其坐標除以sqrt(10),可以輕松計算出該點的合適像元的坐標。
因此,在添加每個下一個點時,可以僅檢查9個像元中點的距離(自身+周圍)。 我想它將比地圖上的所有點都少。
看看Java 8 Stream API中的anyMatch
boolean exists = allDsegs.stream().anyMatch(x -> x.equals(firstDseg /* or whetever you compare */));
好的,伙計們。
現在,我通過使用Stream-Api以及allMatch()-Method解決了這一問題。 問題是,我已經創建了一個Map以保存新元素,並且還進行了測試check = allDsegs.stream().anyMatch(x->dist>10);
是錯的。 解決方案是對新元素使用list(top10List),測試需要檢查top10List中是否存在小於10 km的元素。
這里是代碼,(不幸的是)它並不漂亮。 我從7個月以來只是一個初學者:
private static List<Top10Dsegs> getTop10DSEGs() throws IOException {
List<File>rFiles = getRFiles();
List<String> allDsegs = loadDsegRanking(rFiles);
List<Top10Dsegs> top10List = new ArrayList<>();
double startLat_allDsegs, startLon_allDsegs;
double startLat_top10List, startLon_top10List;
String[] listArray;
//declaring the starting dseg and its coordinates
String startEntry = allDsegs.get(0);
Top10Dsegs firstEntry = new Top10Dsegs(startEntry);
top10List.add(firstEntry);
System.out.println("Iteration starts here!\n---------------------");
//begin to iterate over all entries (just ranked)
for(String line : allDsegs){
boolean check=true;
Top10Dsegs nextEntry=null;
//structure from allDsegs:
//DSEG,startLat,startLon
//1231231231231231231, 54.123, 8.456
listArray = line.split(",");
startLat_allDsegs = Double.valueOf(listArray[1]);
startLon_allDsegs = Double.valueOf(listArray[2]);
//start to check if line-entry of allDsegs has a distance > 10 km compared to given Dsegs
for(Top10Dsegs entry : top10List){
startLat_top10List = entry.getStartLat();
startLon_top10List = entry.getStartLon();
//the DistanceCalculator calculates the distance between the dseg from allDsegs and the other dseg from top10Dsegs
DistanceCalculator distanceCalculator = new DistanceCalculator();
Double dist = distanceCalculator.distance(startLat_top10List, startLon_top10List, startLat_allDsegs, startLon_allDsegs, "K");
System.out.println("Checked Dseg: " + listArray[0]);
System.out.println("Distance between checked Dseg and " + entry.getSegmentID() + " = " + dist);
//check if there is a dseg from allDsegs distance > 10 km compared to ALL OTHER dsegs
if(top10List.stream().allMatch(x->dist<10)==true){
check = false;
}
System.out.println("Are all distances > 10 ? \t" + check);
}
if(check==true && top10List.size()<10){
nextEntry = new Top10Dsegs(listArray[0]+","+listArray[1]+","+listArray[2]);
top10List.add(nextEntry);
}
System.out.println("Progress: \n"+top10List.size()+" entries inside top 10 list. \n<---------------->\nNext Iteration:");
}
return top10List;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.