Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在不同列表之间移动属性_Java_Algorithm_List_Collections - Fatal编程技术网

Java 在不同列表之间移动属性

Java 在不同列表之间移动属性,java,algorithm,list,collections,Java,Algorithm,List,Collections,我只想在两个不同的列表之间移动一个属性: 我有一张狗的名单list 在第一个列表中,我没有newValue属性的值 第二个列表是list 在第二个列表中,我有newValue属性的值 我想要的是将newValue属性从列表移动到列表。 在这种情况下,列表可以有不同数量的元素和不同的元素,这意味着列表的大小可以不同,元素也可以不同 我想要的操作类似于: 如果列表中的id属性与列表中的id属性匹配,我想将newValue属性从列表移动到列表 我知道我可以创建某种算法来实现这一点,但我想知道在某些

我只想在两个不同的列表之间移动一个属性:

我有一张狗的名单
list

在第一个列表中,我没有newValue属性的值

第二个列表是
list

在第二个列表中,我有newValue属性的值

我想要的是将newValue属性从
列表
移动到
列表。
在这种情况下,列表可以有不同数量的元素和不同的元素,这意味着列表的大小可以不同,元素也可以不同

我想要的操作类似于:

  • 如果
    列表中的id属性与
    列表中的id属性匹配,我想将newValue属性从
    列表
    移动到
    列表
我知道我可以创建某种算法来实现这一点,但我想知道在某些google项目或apache commons项目中是否有一种现有的解决方案只是为了避免这种情况 重新发明轮子

更新: 因为这似乎是离题的,因为我要求一个库来做这件事,现在我想知道使用工具或算法来实现这一点的最佳方法


谢谢。

我和@Alan一样,也不确定是否有内置的解决方案,但我也同意Alan的观点,即编写自己的算法来完成这项任务应该相当简单。-循环浏览两个列表,创建两个字典,将ID映射到Dog/DogProp对象。然后通过dogProps循环,如果有与任何Dog对象匹配的ID,则执行替换

代码如下:

static void transferDogProps(List<Dog> dogList, List<DogProp> dogPropList){
    Dictionary<String,Dog> dogDictionary = new Dictionary<String,Dog>();
    Dictionary<String,DogProp> dogPropDictionary = new Dictionary<String,DogProp>();
    foreach(Dog dog in dogList) dogDictionary.Add(dog.getId(),dog);
    foreach(Dog dogProp in dogPropList) dogPropDictionary.Add(dogProp.getId(),dogProp);
    foreach(String id in dogPropList.Keys){
        if(dogDictionary.ContainsKey[id]){
            Dog matchingDog = dogDictionary[id];
            DogProp matchingDogProp = dogPropDictionary[id];
            matchingDog.setNewValue(matchingDogProp.getNewValue());
        }
    }
}
静态无效传输道具(列表道具列表,列表道具列表){
Dictionary dogDictionary=新字典();
Dictionary dogPropDictionary=新字典();
foreach(dogList中的Dog-Dog)dogdirectionary.Add(Dog.getId(),Dog);
foreach(dogPropList中的DogDogProp)dogPropDictionary.Add(dogProp.getId(),dogProp);
foreach(dogPropList.Keys中的字符串id){
if(dogdirectionary.ContainsKey[id]){
狗匹配狗=狗字典[id];
DogProp matchingDogProp=dogPropDictionary[id];
matchingDog.setNewValue(matchingDogProp.getNewValue());
}
}
}

我不知道有任何现有的项目可以为您做到这一点,但我觉得解决方案似乎很简单。您不能使用一个嵌套的循环结构来比较每个列表中的ID吗。然后,当您有匹配项时,只需在将值复制到列表后从列表中删除newValue即可。您应该知道,要求我们推荐或查找工具、库或最喜爱的非现场资源是堆栈溢出的主题(根据)。请更新此问题以避免询问库这是避免堆栈溢出的一个好方法,它可以是一种工具或算法,以避免离题问题。请注意,您使用的
foreach
构造在Java中是无效的语法。这更像是
for(Dog-Dog:dogList){}
噢,我用的是c#语法,但我想这个问题确实有一个Java标记。
public class DogProp{
    private int id;
    private String newValue;
}
static void transferDogProps(List<Dog> dogList, List<DogProp> dogPropList){
    Dictionary<String,Dog> dogDictionary = new Dictionary<String,Dog>();
    Dictionary<String,DogProp> dogPropDictionary = new Dictionary<String,DogProp>();
    foreach(Dog dog in dogList) dogDictionary.Add(dog.getId(),dog);
    foreach(Dog dogProp in dogPropList) dogPropDictionary.Add(dogProp.getId(),dogProp);
    foreach(String id in dogPropList.Keys){
        if(dogDictionary.ContainsKey[id]){
            Dog matchingDog = dogDictionary[id];
            DogProp matchingDogProp = dogPropDictionary[id];
            matchingDog.setNewValue(matchingDogProp.getNewValue());
        }
    }
}