Java 元素间的平均差值
我需要转换按时间升序排序的列表(时间、通道):Java 元素间的平均差值,java,java-stream,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
[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流 正如我从原始问题中了解到的,我可以使用自己的数据类提出以下方法之一:
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();
}
}
正如我从原始问题中了解到的,我可以使用自己的数据类提出以下方法之一:
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();
}
}
好吧,假设有类似于
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}
List pairs=Arrays.asList(
新渠道信息(15,“A”)、新渠道信息(16,“B”),
新渠道信息(17,“C”)、新渠道信息(20,“A”),
新渠道信息(22,“C”)、新渠道信息(24,“B”),
新渠道信息(26,“C”)、新渠道信息(27,“B”),
新ChannelInfo(28,“A”);
Map=pairs.stream()
.collect(收集器).groupingBy(ChannelInfo::getChannel、,
Collectors.collectionAndThen(Collectors.toList(),
列表->{
int size=list.size();
返回IntStream.range(1,大小)
.map(x->(list.get(size-x).getTime()-list.get(size-x-1.getTime()))
.average()
.orElse(0d);
})));
System.out.println(映射);//{A=6.5,B=5.5,C=4.5}
好吧,假设有类似于ChannelInfo
类的东西:
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}
List pairs=Arrays.asList(
新渠道信息(15,“A”)、新渠道信息(16,“B”),
新渠道信息(17,“C”)、新渠道信息(20,“A”),
新渠道信息(22,“C”)、新渠道信息(24,“B”),
新渠道信息(26,“C”)、新渠道信息(27,“B”),
新ChannelInfo(28,“A”);
Map=pairs.stream()
.collect(收集器).groupingBy(ChannelInfo::getChannel、,
Collectors.collectionAndThen(Collectors.toList(),
列表->{
int size=list.size();
返回IntStream.range(1,大小)
.map(x->(list.get(size-x).getTime()-list.get(size-x-1.getTime()))
.average()
.orElse(0d);
})));
System.out.println(映射);//{A=6.5,B=5.5,C=4.5}
很酷的故事,兄弟。你试过什么?因此,这不是一个免费的编码服务。请先尝试不使用流。请先展示一些尝试解决问题的努力。然后,如果您遇到困难,请寻求帮助。您希望如何组织您的流?此外,您可以通过阅读本教程和示例来定义它:Cool story,bro.OK。你试过什么?因此,这不是一个免费的编码服务。请先尝试不使用流。请先展示一些尝试解决问题的努力。然后,如果您遇到困难,请寻求帮助。您希望如何组织您的流?此外,您可以通过阅读本教程和示例来定义它:OP要求基于流的解决方案(尽管他/她没有显示任何研究成果)是的,您是对的,但他/她提供了一个关于使用streamsOP的目标的小信息。OP要求基于流的解决方案(尽管他/她没有显示任何研究成果)是的,您是对的,但是他/她给出了一个关于使用streams的目标的小信息