元素之间的平均差异

时间:2017-07-11 00:24:33

标签: java java-stream

我需要转换按时间升序排序的列表(时间,频道):

[15, A], [16, B], [17, C], [20, A], [22, C], [24, B], [26, C], [27, B], [28, A]

到这一个:

[6.5, A], // ((20-15)+(28-20))/2 - average difference between elements (channel A) 
[5.5, B], // ((24-16)+(27-24))/2
[4.5, C]   // ((22-17)+(26-22))/2

使用java流。

2 个答案:

答案 0 :(得分:2)

好吧,假设有类似ChannelInfo class:

的内容
    class ChannelInfo {
       private final int time;
       private final String channel; 
       // constructor, getters, setters 

可以这样实现:

 List<ChannelInfo> pairs = Arrays.asList(
            new ChannelInfo(15, "A"), new ChannelInfo(16, "B"), 
            new ChannelInfo(17, "C"), new ChannelInfo(20, "A"), 
            new ChannelInfo(22, "C"), new ChannelInfo(24, "B"), 
            new ChannelInfo(26, "C"), new ChannelInfo(27, "B"), 
            new ChannelInfo(28, "A"));

    Map<String, Double> map = pairs.stream()
            .collect(Collectors.groupingBy(ChannelInfo::getChannel, 
                       Collectors.collectingAndThen(Collectors.toList(),
                         list -> {
                            int size = list.size();
                            return IntStream.range(1, size)
                                .map(x -> (list.get(size - x).getTime() - list.get(size - x - 1).getTime()))
                                .average()
                                .orElse(0d);

                    })));

    System.out.println(map); // {A=6.5, B=5.5, C=4.5}

答案 1 :(得分:0)

正如我从原始问题中所理解的那样,我可以使用自己的数据类提出其中一种方法:

public class MyList {

    private int first;
    private String second;

    public MyList(int it, String str) {

        this.first = it;
        this.second = str;
    }

    public MyList() {
    }

    public int getValue() {
        return this.first;
    }

    public MyList getChannel(String str) {
        if (str.equals(second)) {
            return this;
        }
        return null;
    }
}

主要处理类:

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;

public class MyListProcessing {

    private ArrayList<MyList> ml = new ArrayList<MyList>();
    private String[] channels = new String[]{"A", "B", "C"};
    private double[] channels_avg = new double[channels.length];
    private int[] channels_qnt = new int[channels.length];

    public MyListProcessing() {

        ml.add(new MyList(15, "A"));
        ml.add(new MyList(16, "B"));
        ml.add(new MyList(17, "C"));
        ml.add(new MyList(20, "A"));
        ml.add(new MyList(22, "C"));
        ml.add(new MyList(24, "B"));
        ml.add(new MyList(26, "C"));
        ml.add(new MyList(27, "B"));
        ml.add(new MyList(28, "A"));
        getAverage();
    }

    private void getAverage() {
        for (int i = 0; i < channels.length; i++) {
            MyList mmll = new MyList();
            double sum = 0.0;
            for (int j = 0; j < ml.size(); j++) {
                mmll = ml.get(j).getChannel(channels[i]);
                if (mmll != null) {
                    sum += mmll.getValue();
                    channels_qnt[i] = channels_qnt[i] + 1;
                }
                channels_avg[i] = sum / channels_qnt[i];
            }
        }
        NumberFormat formatter = new DecimalFormat("#0.00");
        for (int i = 0; i < channels_avg.length; i++) {
            System.out.println("[" + formatter.format(channels_avg[i]) + ", " + channels[i] + "]");
        }
    }

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

<强>输出:

[21.00, A]
[22.33, B]
[21.67, C]