如何使用流API将类型对象的实例映射到Java中的特定类

发布于 2025-01-23 18:17:27 字数 871 浏览 0 评论 0原文

我遇到了以下问题:

如果我将流中的对象映射到Java中的特定类,则Java流API在映射后无法识别特定对象,并且仍然假定它是对象。我在做什么错,是否有一种方法可以解决这个问题,而没有潜在的班级铸造例外?

这是代码示例:

public class MyMapper {

        MyMapper() {
            Object someObject = new Person();
            final var listOfObjects = List.of(someObject);
            final var listOfPerson = toListOfPerson(listOfObjects);
        }

        List<Optional<Person>> toListOfPerson(Object object) {
            return ((List) object).stream()
                           .map(this::toPerson)
                           .collect(Collectors.toList());
        }

        Optional<Person> toPerson(Object object) {

            if (object instanceof Person) {
                return Optional.of((Person) object);
            }
            return Optional.empty();
        }

        public class Person {}
}

I've encountered the following problem:

If I map an object in a stream to a specific class in Java, the java stream API does not recognize a specific object after the mapping and still assumes it is an object. What am I doing wrong, and is there a way to solve this without potential class cast exceptions?

Here is the code example:

public class MyMapper {

        MyMapper() {
            Object someObject = new Person();
            final var listOfObjects = List.of(someObject);
            final var listOfPerson = toListOfPerson(listOfObjects);
        }

        List<Optional<Person>> toListOfPerson(Object object) {
            return ((List) object).stream()
                           .map(this::toPerson)
                           .collect(Collectors.toList());
        }

        Optional<Person> toPerson(Object object) {

            if (object instanceof Person) {
                return Optional.of((Person) object);
            }
            return Optional.empty();
        }

        public class Person {}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪 2025-01-30 18:17:27

将其投入到键入列表中,然后将您的 .map(this :: toperson)将其“接受”此列表的元素

List<Optional<Person>> toListOfPerson(Object object) {
            return ((List<Object>) object).stream()
                    .map(this::toPerson)
                    .collect(Collectors.toList());
    }

cast it to a typed List and then your .map(this::toPerson) will "accept" the element of this List

List<Optional<Person>> toListOfPerson(Object object) {
            return ((List<Object>) object).stream()
                    .map(this::toPerson)
                    .collect(Collectors.toList());
    }
你是暖光i 2025-01-30 18:17:27

保持可选对象的集合可能是空的,这是没有意义的,它可能是空的,几乎与存储null值相同。

如果您出于某种原因认为list&lt; lt; person&gt;&gt;是个好主意,我建议您查看这个问题。 Stuart Marks(JDK开发人员)的答案引用:

我敢肯定有人会提出一些人为的情况
真的想存储一个可选的在一个领域或集合
一般,最好避免这样做

在JDK中引入了可选的作为有限的机制,以代表A Nullable返回值。这是它的唯一目的,其他使用可选对象(例如可选方法参数,字段,选项集合)的情况被认为是反对的。

您必须在流中解开可选 s:

public List<Person> toListOfPerson(Object object) {
    if (!(object instanceof List<?>)) {
        return Collections.emptyList();
    }
    
    return ((List<?>) object).stream()
        .map(this::toPerson)
        .filter(Optional::isPresent)
        .map(Optional::get)
        .collect(Collectors.toList());
}

It doesn't make sense to keep the collection of optional objects which could be potentially empty, it almost the same as storing null values.

If you think for some reason that List<Optional<Person>> is good idea, I recommend you to have a look at this question. A quote from the answer by Stuart Marks (JDK developer):

I'm sure somebody could come up with some contrived cases where they
really want to store an Optional in a field or a collection, but in
general, it is best to avoid doing this.

Optional was introduced in the JDK as a limited mechanism to represent a nullable return value. That is its only purpose, other cases of usage of optional objects like optional method arguments, fields, collections of optionals are considered to be antipattern.

You have to unpack your Optionals in the stream:

public List<Person> toListOfPerson(Object object) {
    if (!(object instanceof List<?>)) {
        return Collections.emptyList();
    }
    
    return ((List<?>) object).stream()
        .map(this::toPerson)
        .filter(Optional::isPresent)
        .map(Optional::get)
        .collect(Collectors.toList());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文