gpt4 book ai didi

java - 如何通过java中的值获取对象列表的交集?

转载 作者:行者123 更新时间:2023-12-01 14:02:53 25 4
gpt4 key购买 nike

类的结构:

class MyObject{
private String key;
private int value;
private int num;
}

创建对象列表:

List<MyObject> a = new ArrayList<MyObject>();

对象列表的内容:

"Einstein",12,1
"Princeton",12,4
"Einstein",16,3
"Princeton",16,7
"Einstein",19,6
"Princeton",22,6
"Quantum",12,3
"Quantum",16,6

输入:“Einstein”、“Princeton”、“Quantum”

检查所有值字段中是否存在某个键,如果存在,则将 num 字段相加。在本例中,Einstein、Princeton、Quantum 仅以值 12 出现。因此将 num 字段相加将得到 8。因此,

预期输出列表:

12,8   

基本上,我试图获取对象的值字段与相应的数字字段的总和的交集。如何实现这一目标?

编辑:列表 xy = Arrays.asList(terms);//术语是输入

    Map<Integer, Integer> check = new HashMap<Integer, Integer>();
for (int i = 0; i < a.size(); i++) {

if (xy.contains(a.get(i).getKey())) {

Integer sum = check.get(a.get(i).getNum());
if (sum == null)
sum = 0;
sum += a.get(i).getNum();
check.put(a.get(i).getValue(), sum);
}

}

列出内容:

Key: british Value: 899816511 Occ: 8
Key: naren Value: 899816511 Occ: 1
Key: einstein Value: 899816511 Occ: 1
Key: british Value: 562115287 Occ: 1
Key: einstein Value: 2056958632 Occ: 1
Key: british Value: 2056958632 Occ: 1
Key: einstein Value: 1426519040 Occ: 1
Key: british Value: 1426519040 Occ: 5

输入:“英国”、“纳伦”、“爱因斯坦”

输出:

{1426519040=5, 562115287=1, 2056958632=1, 899816511=1}

最佳答案

我已经根据我认为您想要的内容编写了一个“解决方案”代码。根据您的需要尝试和修改。请注意,有两个结果:
16,16
12,8

private static class MyObject {
private String key;
private int value;
private int num;

public MyObject(String key, int value, int num) {
this.key = key;
this.value = value;
this.num = num;
}
public String getKey() {
return key;
}
public int getValue() {
return value;
}
public int getNum() {
return num;
}
}

private static class KeysAndSum {
private Set<String> keys = new HashSet<String>();
private int sum;

public Set<String> getKeys() {
return keys;
}
public void addKey(String key) {
keys.add(key);
}
public int getSum() {
return sum;
}
public void addNum(int num) {
sum += num;
}
}

public static void main(String[] args) {
List<MyObject> a = new ArrayList<MyObject>();
a.add(new MyObject("Einstein", 12, 1));
a.add(new MyObject("Princeton", 12, 4));
a.add(new MyObject("Einstein", 16, 3));
a.add(new MyObject("Princeton", 16, 7));
a.add(new MyObject("Einstein", 19, 6));
a.add(new MyObject("Princeton", 22, 6));
a.add(new MyObject("Quantum", 12, 3));
a.add(new MyObject("Quantum", 16, 6));

List<String> requiredKeys = new ArrayList<String>();
requiredKeys.add("Einstein");
requiredKeys.add("Princeton");
requiredKeys.add("Quantum");

Map<Integer, KeysAndSum> map = new HashMap<>();
for (MyObject obj : a) {
KeysAndSum keysAndSum;
if (map.containsKey(obj.getValue())) {
keysAndSum = map.get(obj.getValue());
} else {
keysAndSum = new KeysAndSum();
map.put(obj.getValue(), keysAndSum);
}
keysAndSum.addKey(obj.getKey());
keysAndSum.addNum(obj.getNum());
}
for (Entry<Integer, KeysAndSum> entry : map.entrySet()) {
boolean allFound = true;
for (String reqKey : requiredKeys) {
if (!entry.getValue().getKeys().contains(reqKey)) {
allFound = false;
break;
}
}
if (allFound) {
System.out.println(entry.getKey() + ","
+ entry.getValue().getSum());
}
}
}

关于java - 如何通过java中的值获取对象列表的交集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19203692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com