Java 迭代器创建新对象或修改旧对象

Java 迭代器创建新对象或修改旧对象,java,Java,这只是java大师的一个问题。如果我有如下代码 public void setSeenAttribute(String notificationId , String userId){ UserNotification userNotification = notificationRepository.getUserNotification(userId); if (userNotification != null) { for (Noti

这只是java大师的一个问题。如果我有如下代码

public void setSeenAttribute(String notificationId , String userId){
        UserNotification userNotification = notificationRepository.getUserNotification(userId);
        if (userNotification != null) {
            for (Notification notification : userNotification.getNotifications()) {
                if (StringUtils.equals(notification.getNotificationId(), notificationId)) {
                    notification.setSeen(true);
                }
            }
            notificationRepository.createUpdateNotification(userNotification);
        }
    }

我想知道天气
notification.setSeen(true)
将对原始集合进行更改,还是这样做毫无价值?或者什么是最佳实践?

在Java中,“对对象的引用是通过值传递的”。因此,除非显式重置引用以指向另一个对象,否则当前对象将被修改。

首先,这不是迭代器,而是用于每个循环对集合进行迭代。 在为每个循环使用时更新值是非常好的。这在Java中的“迭代器”中是完全不允许的,因为它们是快速调用失败的

所以


正在将集合中的对象更新为新引用,即通知指向驻留在集合本身中的对象。

是的,您可以这样做,因为句柄作为值传递,但其引用是按对象传递的。为了证明这一点,这里有一个小例子:

public class ModifyElementsOfCollection {

    public static void main(String[] args) {
        Collection<Wrapper<Integer>> collection = new ArrayList<Wrapper<Integer>>();

        for(int i=0; i<10; i++) {
            collection.add(new Wrapper<Integer>(i));
        }

        collection.stream().map(w -> w.element).forEach(System.out::println);

        for(Wrapper<Integer> wrapper : collection) {
            wrapper.element += 1;
        }

        collection.stream().map(w -> w.element).forEach(System.out::println);

    }

    private static class Wrapper<T> {
        private T element;

        private Wrapper(T element) {
            this.element = element;
        }
    }

}
公共类ModifyElementsOfCollection{
公共静态void main(字符串[]args){
集合集合=新的ArrayList();
for(int i=0;i w.element).forEach(System.out::println);
for(包装器:集合){
1.element+=1;
}
collection.stream().map(w->w.element).forEach(System.out::println);
}
私有静态类包装器{
私有T元素;
专用包装器(T元素){
this.element=元素;
}
}
}
在第二个for循环之前,输出是数字0到9,之后是数字1到10。这也适用于更复杂的东西


顺便说一下,本例使用Java 8的一些功能来打印结果,当然,您也可以使用for循环。

是的,当您更新Notificaton的引用时,原始集合将被修改。如果理解正确,我可以这样说吗Java确实通过引用操作对象,所有对象变量都是引用。但是,Java不通过引用传递方法参数;它通过值传递它们。“@SaurabhKumar-是的..你可以说它在内部使用了一个
迭代器
。这被称为增强的for循环。
public class ModifyElementsOfCollection {

    public static void main(String[] args) {
        Collection<Wrapper<Integer>> collection = new ArrayList<Wrapper<Integer>>();

        for(int i=0; i<10; i++) {
            collection.add(new Wrapper<Integer>(i));
        }

        collection.stream().map(w -> w.element).forEach(System.out::println);

        for(Wrapper<Integer> wrapper : collection) {
            wrapper.element += 1;
        }

        collection.stream().map(w -> w.element).forEach(System.out::println);

    }

    private static class Wrapper<T> {
        private T element;

        private Wrapper(T element) {
            this.element = element;
        }
    }

}