2013-10-05 26 views
0

结构:如何通过java中的值获取交集的对象列表?类的

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"

检查如果一个密钥存在于所有的值域中,如果s加起来的数字字段。在这种情况下,爱因斯坦,普林斯顿,量子只存在于价值12.因此增加了NUM场会给8.因此,

预计输出列表:

12,8 

基本上我试图让交集对象的值字段和对应的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} 
+0

你是什么意思与 “交集” 吗? – Manu343726

+0

“交集”对于你想要做的事来说并不合适。为什么输出中不是'19,6'和'22,6'?因为它必须有2个“价值”元素才能显示出来?如果有2个以上的话? – Dukeling

+0

num字段未使用。只有价值领域,因此19,6和22,6不考虑。 – NEO

回答

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()); 
     } 
    } 
} 
+0

预计只有12.8,因为所有这三个字都只在12中出现。 – NEO

+1

@Naren不,他们不是。检查你自己的例子 - 你在12和16中都有三个单词。 – alterfox

相关问题