我的问题如下:我有对象列表。我需要将列x2与相同的列X3相加并合并X1列。后来删除了旧行。请提出建议。
Input:-
List<A> list = new ArrayList<>()
A A1 = new A(X1=111, X2=10, X3= '333')
A A2 = new A(X1=222, X2=20, X3= '333')
A A3 = new A(X1=333, X2=2, X3= '444')
A A4 = new A(X1=444, X2=5, X3= '444')
A A5 = new A(X1=666, X2=6, X3= '777')
list.add(A1).addd(A2).add(A3).add(A4).add(A4)
OutPut should be like below:-
{
Object A1 (list = {111,222}, x2= 30 , x3= '333')
Object A3 (list = {333,444}, x2= 7 , x3= '444')
Object A5 (list = {666}, x2= 6 , x3= '777')
}
public class A {
private long X1;
private long X2;
private String X3;
private List<Long> list;
}
答案 0 :(得分:0)
尝试此操作(java 8+),将A作为可变对象(我不喜欢):
ABCDEFG:random-folder User$ docker run storyassist
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM default
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM default
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM dmix
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "./transcribe.py", line 83, in read_audio
RATE = int(p.get_default_input_device_info() ['defaultSampleRate'])
File "/usr/local/lib/python3.7/site-packages/pyaudio.py", line 949, in get_default_input_device_info
device_index = pa.get_default_input_device()
OSError: No Default Input Device Available
,输出显示:
List<A> objects = new ArrayList<>();
A A1 = new A(111, 10, "333");
A A2 = new A(222, 20, "333");
A A22 = new A(222, 20, "333");
A A3 = new A(333, 2, "444");
A A4 = new A(444, 5, "444");
A A5 = new A(666, 6, "777");
objects.add(A1);
objects.add(A2);
objects.add(A22);
objects.add(A3);
objects.add(A4);
objects.add(A5);
List<A> output = objects
.stream()
.collect(Collectors.collectingAndThen(
Collectors.toMap(A::getX3, a -> {
a.getList().add(a.getX1());
return a;
}, (a, b) -> {
a.getList().addAll(b.getList());
a.setX2(a.getX2() + b.getX2());
return a;
}),
map -> new ArrayList(map.values())
));
System.out.println(output);
答案 1 :(得分:0)
也许您正在寻找类似的东西?这绝对可以简化,但是我认为它具有您想要的输出。请务必对其进行分解以了解其工作原理,以便将来自己进行操作。
public static void main(String[] args) {
List<A> vals = new ArrayList<>();
vals.add(new A(111, 10, "333"));
vals.add(new A(222, 20, "333"));
vals.add(new A(333, 2, "444"));
vals.add(new A(444, 5, "444"));
vals.add(new A(666, 6, "777"));
List<Output> outputs = getOutput(vals);
for(Output output : outputs) {
System.out.println(output);
}
}
public static class Output {
public List<Long> X1 = new ArrayList<Long>();
public long X2;
public String X3;
//Set initial values
public Output(A value) {
X1.add(value.X1);
X2 = value.X2;
X3 = value.X3;
}
//Add additional values
public void addValue(A value) {
X1.add(value.X1);
X2 += value.X2;
}
@Override
public String toString() {
return "X1: " + X1.toString() + ", X2: " + X2 +", X3: " + X3;
}
}
public static class A {
public long X1;
public long X2;
public String X3;
public A(int x1, int x2, String x3) {
X1 = x1;
X2 = x2;
X3 = x3;
}
}
public static List<Output> getOutput(List<A> vals) {
//Create return list
List<Output> outputList = new ArrayList<>();
//Copy given array
List<A> values = new ArrayList<>(vals);
while(!values.isEmpty()) {
//Grab first value and remove it from list copy
A value = values.get(0);
values.remove(0);
//Create output object
Output output = new Output(value);
//List for deleting objects processed
List<A> deletionList = new ArrayList<>();
//Process the rest of list to look for matching X3 values
for(A otherValue : values) {
if (value.X3.equals(otherValue.X3)) {
deletionList.add(otherValue);
output.addValue(otherValue);
}
}
//Add output object to return list and remove processed values
outputList.add(output);
values.removeAll(deletionList);
}
return outputList;
}