java - 创建新的自定义对象与在 Java 中将同一对象的多个值设置为 Null

标签 java spring performance

所以,我遇到过这种情况

我有一个 Restful Controller 方法,它返回对象的 list Match

@RequestMapping(value = "/fetchFootballMatchesToday", method = RequestMethod.GET)
public @ResponseBody List<Match> fetchMatchesScheduledToday() throws ParseException {
    return matchesToday;
   }

对象 Match 有 15 个属性,而我在 UI 上只需要 3 个属性。我不希望用户看到该对象的其他属性。

有两种方法可以做到这一点:

  1. 对于每个不打算发送到 UI 的 Match 对象,运行 for 循环并显式将属性设置为 null。就像这样

    List<Match> matchesToday = matchInvService.fetchMatchByDate(today);
    
    for (Match stats : matchesToday) {
     if (stats != null) {
    /* user should not know these details, so they are being set to null */
        stats.setMatchSuccessIndex(null);
        stats.setTeamRanking(null);
        stats.setStadiumInfrastructureLevel(null); 
        ..... & so on
      }
    }
    

这种方法的问题在于, future 属性的数量会不断增加。这段代码也需要更新。此外,如果对象的属性数量很大。我需要多次在这里设置 null。

  • 第二种方法是在循环中创建一个新的 Match 对象,并为其设置 3 个必需的值。

    List<Match> matchesToday = matchInvService.fetchMatchByDate(today);
    List<Match> responseList = new ArrayList<Match>();  
    
    for (Match stats : matchesToday) {
     if (stats != null) {
       Match tmpMatch = new Match();
       match.setProperty1(stats.getProperty1());
      }
     responseList.add(tmpMatch); 
    }
    
     return responseList;
    
  • 这种方法在每次循环运行时都会创建额外的 Match 对象。此外,如果过于频繁地调用该特定方法,对象的创建就会激增。 尽管对象将被垃圾收集,但我不确定这是否是最佳方式。

    需要你们的建议。这是编写更多代码与节省内存之间的权衡吗?解决这个问题的最佳方法是什么?

    最佳答案

    The object Match has say 15 properties & I need only 3 properties on the UI. I do not want the user to see the other properties of the object.

    将所有字段设置为null确实很麻烦并且容易出错。
    所以我会避免第一种方式。

    第二种方法似乎朝着更好的方向发展。
    但是您不应该害怕将对象映射到其他对象,因为客户端需要查看它们的特定 View 。
    “合理”列表大小的简单映射操作很便宜。我想您不会在 UI 中显示数百万行,因此大小应该合理。
    否则,您应该通过考虑分页概念来重新思考整个 UI 设计。

    我会使用第三个选项:创建一个类,声明客户端需要知道的 3 个属性,并将 Match 映射到该类。它让事情变得更加清晰。

    List<Match> matchesToday = matchInvService.fetchMatchByDate(today);
    List<MatchDTO> responseList = new ArrayList<Match>();  
    
    for (Match stats : matchesToday) {
     if (stats != null) {
        MatchDTO match = new MatchDTO(stats);
        responseList.add(match); 
      } 
    }
    
    return responseList;
    

    其中 MatchDTO(Match) 是一个构造函数,它从 Match 实例复制 3 个所需字段:

    public MatchDTO(Match match){
      this.foo = match.getFoo();
      this.bar = match.getBar();
      this.fooBar = match.getFooBar();
    }
    

    或者在 Java 8 中:

    List<MatchDTO> responseList =   
        matchInvService.fetchMatchByDate(today)
                       .stream()
                       .filter(Objects::nonNull)
                       .map(MatchDTO::new)
                       .collect(Collectors.toList);
    

    关于java - 创建新的自定义对象与在 Java 中将同一对象的多个值设置为 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51667528/

    相关文章:

    java - 在 eclipse pmd 插件中添加新的基于 java 的 PMD 规则

    java - 在 Java 中最大化 JInternalFrame

    java - 从 Weblogic 9 调用部署在 Weblogic 12 中的远程 EJB 时出错

    spring - 关闭程序的 DefaultMessageListenerContainer 问题

    java - 从类文件中读取一行代码

    java - tomcat 中的 <session-timeout> 过期非常快(1 天 > 7 天)

    java - Camel FTP 定时轮询

    database - ArangoDB 通过 _id 获取文档,每个查询缓存

    c++ - 堆数组性能慢

    R 数据表优化。 overdraw 财务数据