简体   繁体   English

如何从地图中给出的总键中获取所需的键?

[英]How can I get the required keys from total keys given in a map?

I require approx. 我需要大约。 20 keys from a data provided that contains 100+ keys. 提供的包含20个以上键的数据中有20个键。

Set<Entry<Double,PosTvector>> set = realPosDataMap.entrySet();

Iterator<Entry<Double, PosTvector>> i1 = set.iterator();

Map.Entry<Double,PosTvector> map;

while(i1.hasNext()) {
    map = i1.next();

    Double xvalue = map.getKey();
    PosTvector yvalue = map.getValue();
    PosTvector yvalue1 = map.getValue();
    PosTvector yvalue2 = map.getValue();                       
    Data<String, Number> data = new Data<String, Number>(xvalue.toString(), yvalue.state[0]);
    Data<String, Number> data1 = new Data<String, Number>(xvalue.toString(), yvalue1.state[1]);
    Data<String, Number> data2 = new Data<String, Number>(xvalue.toString(), yvalue2.state[2]);
  //Data<String, Number> verticalMarker = new Data<String, Number>(xvalue.toString(), 0);

    series.getData().add(data);   
    series1.getData().add(data1); 
    series2.getData().add(data2);


}   
series.setName("X-Position");
series1.setName("Y-Position");
series2.setName("Z-Position");


lineChart.getData().addAll(series, series1, series2);

I want to display it on line chart only 20 key and values not all the 100 values on the graph. 我想在折线图上仅显示20个键,而不是在图形上显示所有100个值。 can u help me out, how can we achieve this? 您能帮我吗,我们如何实现呢? Thanks in advance. 提前致谢。

I don't know that I understand it well so I will talk about the case when you want to decimate the data. 我不知道我是否理解得很好,所以我将在您要抽取数据的情况下谈论这种情况。

The best solution would be to resample your input data and time vector. 最好的解决方案是对输入数据和时间向量进行重新采样。

But if you want to be simple, you can try something like this: 但是,如果您想简单一点,可以尝试如下操作:

  • Always add the starting end ending data point 始终添加开始结束点数据点
  • For inner data points calculate the decimation 对于内部数据点,计算抽取

Example

This example will always produce a LineChart with 20 nodes independently from how many data points you input array has. 本示例将始终产生一个带有20个节点的LineChart与输入数组具有的数据点无关。

Note: this solution is far not the best you can have, and heavily relies on that the input data vectors has the same time vector. 注意:此解决方案并不是您所能拥有的最好的解决方案,并且在很大程度上依赖于输入数据向量具有相同的时间向量。 It is also influences the result that the step size of the time vector is fixed. 固定时间向量的步长也会影响结果。

public class ChartingApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {

        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);

        // Size of the input data
        int dataCount = 777;

        // One X axis two Y axis
        double[] xValues = new double[dataCount];
        double[] y1Values = new double[dataCount];
        double[] y2Values = new double[dataCount];

        // Generate some initial data
        Random randomGenerator = new Random();

        for (int i = 0; i < dataCount; i++) {
            xValues[i] = i;
            y1Values[i] = randomGenerator.nextInt(200);
            y2Values[i] = randomGenerator.nextInt(200);
        }

        // Amount of nodes to display
        int valueCountToDisplay = 20;

        Series<Number, Number> series1 = new Series<Number, Number>();
        Series<Number, Number> series2 = new Series<Number, Number>();

        // The first element is always added
        series1.getData().add(new Data<Number, Number>(xValues[0], y1Values[0]));
        series2.getData().add(new Data<Number, Number>(xValues[0], y2Values[0]));

        // Generate the inner nodes
        double elementToPick = Math.floor((dataCount - 2) / (valueCountToDisplay - 2));

        for (int i = 1; i < dataCount - 1; i++) {
            if (i % elementToPick == 0) {
                series1.getData().add(new Data<Number, Number>(xValues[i], y1Values[i]));
                series2.getData().add(new Data<Number, Number>(xValues[i], y2Values[i]));
            }
        }

        // The last element is always added
        series1.getData().add(new Data<Number, Number>(xValues[dataCount - 1], y1Values[dataCount - 1]));
        series2.getData().add(new Data<Number, Number>(xValues[dataCount - 1], y2Values[dataCount - 1]));

        Scene scene = new Scene(lineChart, 800, 600);
        lineChart.getData().addAll(series1, series2);

        stage.setScene(scene);
        stage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

}

The result is: 结果是:

在此处输入图片说明

The logic behind can be summarized like this: 背后的逻辑可以总结如下:

double[] createSparseArray(double[] inputData, int arraySize) {
    List<Double> sparseList = new ArrayList<Double>();

    sparseList.add(inputData[0]);

    double elementToPick = Math.floor((inputData.length - 2) / (arraySize - 2));

    for (int i = 1; i < inputData.length - 1; i++) {
        if (i % elementToPick == 0)
            sparseList.add(inputData[i]);
    }
    sparseList.add(inputData[arraySize - 1]);
    return sparseList.stream().mapToDouble(d -> d).toArray();
}

The example uses arrays to store the data, but it is really similar with you data structure (using size of Set and looping with the good-old for loop). 该示例使用数组来存储数据,但实际上与您的数据结构相似(使用Set 大小并使用良好的for循环进行循环)。

Well, just create a new variable which you increase on each iteration. 好吧,只需创建一个新变量,您可以在每次迭代中增加它。 When the value of this variable reaches 20, break out of the loop. 当该变量的值达到20时,跳出循环。 Like this: 像这样:

int amount = 0;
while(i1.hasNext()) {
    amount += 1;
    if(amount == 20) {
        break;
    }
    ...
}

If you don't know break : Its a keyword that leaves the containing loop. 如果您不知道break :它是一个离开包含循环的关键字。

EDIT 编辑

I misunderstood you first, here's the (hopefully) correct answer. 我首先误解了您,这是(希望)正确的答案。 Instead of the solution above, do this: 代替上面的解决方案,执行以下操作:

while(i1.hasNext()) {
    map = i1.next();
    if(map.getKey() > 20 || map.getKey() < 1) {
        continue;
    }
    ...
}

This code checks if the key of the entry is between 1 and 20. If not, the next entry will be checked. 此代码检查条目的密钥是否在1到20之间。如果不是,则将检查下一个条目。 To do so, it uses continue - a keyword similar to break , but instead of ending the loop, it will just jump to the next iteration. 为此,它使用continuebreak相似的关键字,但是它不会结束循环,而是跳转到下一个迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM