V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
mmdsun
V2EX  ›  问与答

Java stream 如何优雅处理这种数据

  •  
  •   mmdsun · Dec 23, 2020 via Android · 1956 views
    This topic created in 1952 days ago, the information mentioned may be changed or developed.

    [{

    	"bookName": "1",
    
    	"bookTypeId": "",
    
    	"attr": {
    
    		"typeId": "type-a"
    
    	}
    
    },
    
    
    
    {
    
    	"bookName": "2",
    
    	"bookTypeId": "",
    
    	"attr": {
    
    		"typeId": "type-b"
    
    	}
    
    }
    

    ]

    需要把 attr 对象中的 typeid 设置到外面 bookid 中,attr 对象可能为空,返回还是 List<Book>。类似于这种伪代码。请问这种怎么优雅实现。

    List<Book> collect = json.getBookList()
    
                .stream()
    
                .map(m -> m.setBookTypeId(m.getAttr().getTypeId())).collect(
    
    Supplement 1  ·  Dec 23, 2020
    append:

    json.getBookList()
    .stream()
    .peek(book -> m.getArrt())
    .map(Arrt::getTypeId)
    .ifPresent(book::setBookTypeId))
    .collect()

    感谢大家,已解决。网上找到了类似写法。 明天试试看。
    (要改 set get 方法,setBookTypeId 需要返回 Book 的 this )

    回 5 楼 c# linq 确实给编程语言贡献了很多。
    14 replies    2020-12-23 18:49:41 +08:00
    zm8m93Q1e5otOC69
        1
    zm8m93Q1e5otOC69  
       Dec 23, 2020 via Android
    你这样写挺好的
    canbingzt
        2
    canbingzt  
       Dec 23, 2020   ❤️ 1
    json.getBookList()
    .stream()
    .filter(m -> m.getAttr() != null)
    .forEach(m -> m.setBookTypeId(m.getAttr().getTypeId()))
    mmdsun
        3
    mmdsun  
    OP
       Dec 23, 2020 via Android
    @beichenhpy 不行。。我这样写要把 setBookTypeId 方法给改了返回 Book 才行,现在 set 方法返回的是 void,而且 getAttr().getId 会报空指针。😅
    6IbA2bj5ip3tK49j
        4
    6IbA2bj5ip3tK49j  
       Dec 23, 2020   ❤️ 1
    json.getBookList()
    .stream()
    .filter(m -> m.getAttr() != null &&m.getAttr().getTypeId()!=null )
    .peek(m -> m.setBookTypeId(m.getAttr().getTypeId()))
    Rwing
        5
    Rwing  
       Dec 23, 2020
    C#欢迎您

    var result = json.BookList
    .Where(b=>b.attr != null)
    .Select(b=>{ bookTypeId=b.attr.typeId; return b; })
    .ToList();
    taogen
        6
    taogen  
       Dec 23, 2020   ❤️ 1
    @canbingzt #2
    @xgfan #4

    Don't do that. If you use a functional pattern, do not bring any imperative/procedural stuff in it. The purpose of map is to transform an object into another object. It's meant to be purely functional (side-effect free) by design.
    taogen
        7
    taogen  
       Dec 23, 2020
    If we want to process this data in parallel, we are going to distribute it among all the cores of our processors and we do not want to have any kind of visibility or synchronization issues that could lead to bad performances or errors. Avoiding this kind of interference means that we shouldn't modify the source of the data while we're processing it.
    luban
        8
    luban  
       Dec 23, 2020
    @mmdsun 可以让 set 方法返回 this
    yuhuan66666
        9
    yuhuan66666  
       Dec 23, 2020
    这个赋值位置不能用 map map 要求返回一个新对象,但你的需求实际上是在原对象上添加元素 应该用 peek
    yuhuan66666
        10
    yuhuan66666  
       Dec 23, 2020
    看一下 《 java8 实战》对 stream 讲用法还不错
    auin
        11
    auin  
       Dec 23, 2020
    松散结构直接 stream 操作想想就可怕,NPE 教做人
    6IbA2bj5ip3tK49j
        12
    6IbA2bj5ip3tK49j  
       Dec 23, 2020
    @taogen 是的,你是对的。


    -----------
    最好是不要修改原有集合 /集合里的数据。
    mmdsun
        13
    mmdsun  
    OP
       Dec 23, 2020 via Android
    json.getBookList()
    .stream()
    .peek(book -> m.getArrt())
    .map(Arrt::getTypeId)
    .ifPresent(book::setBookTypeId))
    .collect()

    感谢大家,已解决。网上找到了类似写法。 明天试试看。
    ( setBookTypeId 需要返回 Book 的 this )

    回 5 楼 c# linq 确实给编程语言贡献了很多。

    @liuxey @yuhuan66666 @luban @canbingzt @beichenhpy @taogen @xgfan
    wysnylc
        14
    wysnylc  
       Dec 23, 2020
    @liuxey #11 需要 optional 救你
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3553 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 70ms · UTC 11:48 · PVG 19:48 · LAX 04:48 · JFK 07:48
    ♥ Do have faith in what you're doing.