如何在一个步骤中将一个值与所有列表条目进行比较?

时间:2017-03-04 07:44:11

标签: java algorithm

对于线性距离的特殊斜率,我需要检查一个新点,给定的纬度/经度相对于存储在地图中的所有其他点的距离大于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;      
}

3 个答案:

答案 0 :(得分:2)

为避免计算和比较所有已存储点与新存储点之间的距离,您可以使用(多)Map或数组实现的某种网格(或矩阵)。网格的每个单元可以具有sqrt(10km)的大小并且包含具有相关坐标的所有点。通过将它的坐标除以sqrt(10),可以很容易地计算该点的合适单元格的坐标。

因此,当添加每个下一个点时,可以仅检查9个单元格中的点的距离(自己的+周围)。我想它会远远低于地图上的所有点。

答案 1 :(得分:1)

查看Java 8 Stream API中的anyMatch

boolean exists = allDsegs.stream().anyMatch(x -> x.equals(firstDseg /* or whetever you compare */));

答案 2 :(得分:0)

好的伙计们。

现在我通过使用Stream-Api和allMatch() - Method解决了它。问题是,我创建了一个Map来保存新元素,而且测试check = allDsegs.stream().anyMatch(x->dist>10);是错误的。 解决方案是使用新元素的列表(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;
}