0

班级结构:

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 字段相加。在这种情况下,爱因斯坦、普林斯顿、量子只存在于值 12。因此,将 num 字段相加将得到 8。因此,

预期输出列表:

12,8   

基本上我正在尝试获取对象的 value 字段与相应 num 字段的总和的交集。如何做到这一点?

编辑:列表 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

输入:"british","naren","einstein"

输出:

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

1 回答 1

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());
        }
    }
}
于 2013-10-06T00:08:58.017 回答