在java中,如何通过一个值获得对象列表的交集?
班级结构:在java中,如何通过一个值获得对象列表的交集?,java,list,intersection,Java,List,Intersection,班级结构: 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 "Qu
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
输入:“爱因斯坦”、“普林斯顿”、“量子”
检查所有值字段中是否存在键,如果存在,则添加num字段。在这个例子中,爱因斯坦,普林斯顿,量子只存在于值12中。因此,将num字段相加将得到8。因此,
预期输出列表:
12,8
基本上,我试图得到对象的值字段和相应num字段的总和的交点。如何做到这一点
编辑:
List xy=数组。asList(术语);//术语是输入
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);
}
}
输入:“英国人”、“纳伦人”、“爱因斯坦”
输出:
{1426519040=5, 562115287=1, 2056958632=1, 899816511=1}
我已经写了一个“解决方案”代码,基于我认为你所追求的。尝试并根据您的需要进行修改。
请注意有两个结果:16,16
12,8
私有静态类MyObject{
私钥;
私有int值;
私有整数;
公共MyObject(字符串键、int值、int num){
this.key=key;
这个值=值;
this.num=num;
}
公共字符串getKey(){
返回键;
}
public int getValue(){
返回值;
}
公共int getNum(){
返回num;
}
}
私有静态类KeysAndSum{
私有集密钥=新的HashSet();
私人整数和;
公共设置getKeys(){
返回键;
}
公共void addKey(字符串键){
key.add(key);
}
公共整数getSum(){
回报金额;
}
公共void addNum(int num){
sum+=num;
}
}
公共静态void main(字符串[]args){
列表a=新的ArrayList();
a、 添加(新的MyObject(“爱因斯坦”,12,1));
a、 添加(新的MyObject(“普林斯顿”,12,4));
a、 添加(新的MyObject(“爱因斯坦”,16,3));
a、 增加(新的MyObject(“普林斯顿”,16,7));
a、 添加(新的MyObject(“爱因斯坦”,19,6));
a、 增加(新的MyObject(“普林斯顿”,22,6));
a、 添加(新的MyObject(“Quantum”,12,3));
a、 添加(新的MyObject(“Quantum”,16,6));
List requiredKeys=new ArrayList();
所需密钥。添加(“爱因斯坦”);
必填键。添加(“普林斯顿”);
所需密钥。添加(“量程”);
Map Map=newhashmap();
对于(MyObject对象:a){
KeysAndSum KeysAndSum;
if(map.containsKey(obj.getValue())){
keysAndSum=map.get(obj.getValue());
}否则{
keysAndSum=新的keysAndSum();
put(obj.getValue(),keysAndSum);
}
keysAndSum.addKey(obj.getKey());
keysAndSum.addNum(obj.getNum());
}
for(条目:map.entrySet()){
布尔allFound=true;
for(字符串reqKey:RequiredKey){
如果(!entry.getValue().getKeys()包含(reqKey)){
allFound=false;
打破
}
}
如果(全部找到){
System.out.println(entry.getKey()+“,”
+entry.getValue().getSum());
}
}
}
你说的“交叉点”是什么意思?“交叉点”这个词不适合你要做的事情。为什么输出中没有19,6
和22,6
?因为它必须有两个值的元素才能显示?如果有2个以上呢?不使用num字段。只考虑值字段,因此不考虑19,6和22,6。您不是从12,1
和12,4
获得12,5
(即12,1+4
)吗?请使用HashMap。循环遍历ArrayList并在每次迭代时更新映射。使用value作为键,num作为映射中的值。所以基本上整数和=map.get(value);如果(sum==null)sum=0;sum+=num;map.put(值、和);预期值只有12,8,因为这三个词都只出现在12中。@Naren不,它们不是。检查你自己的例子——你在12和16中都有这三个词。
{1426519040=5, 562115287=1, 2056958632=1, 899816511=1}
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());
}
}
}