whatisnew
V2EX  ›  Java

Java 或者 guava 有没有像 PHP array_column 的实现?

  •  
  •   whatisnew · Apr 30, 2018 · 5600 views
    This topic created in 2961 days ago, the information mentioned may be changed or developed.

    主要是 php 的 array_column 太爽了,哈哈 http://php.net/manual/zh/function.array-column.php

    21 replies    2018-05-02 15:20:32 +08:00
    binbinyouliiii
        1
    binbinyouliiii  
       Apr 30, 2018
    手动实现不就好了。
    这个功能需求应该也很小,又简单,估计第三方库不会有这种方法
    whatisnew
        2
    whatisnew  
    OP
       Apr 30, 2018
    @binbinyouliiii Lambda 实现 easy 但是还是觉得不够优雅。。。
    ghos
        3
    ghos  
       Apr 30, 2018
    港道理 好像没有这种操作的吧。。。lambda 已经相当便捷了
    binbinyouliiii
        4
    binbinyouliiii  
       Apr 30, 2018
    @whatisnew #2 换 kotlin,使用 kotlin 扩展函数
    Cbdy
        5
    Cbdy  
       Apr 30, 2018
    ```java
    List<List<Integer>> a2 = new ArrayList<>();
    a2.add(List.of(1, 2, 3, 4, 5, 6));
    a2.add(List.of(2, 3, 4, 5, 6, 7));
    a2.add(List.of(3, 4, 5, 6, 7, 8));
    var result = a2.stream().map(l -> l.get(3)).collect(Collectors.toList());
    assert Objects.deepEquals(result, List.of(4, 5, 6));
    ```
    Seanfuck
        6
    Seanfuck  
       Apr 30, 2018
    作为 php 老司机居然不知道还有这么个好用的函数,一直是 foreach 构造。。。法克!
    mudone
        7
    mudone  
       Apr 30, 2018
    @Seanfuck 老司机不知道是正常的,没事翻翻手册,总是有新收获.
    aristotll
        8
    aristotll  
       Apr 30, 2018
    @Cbdy #5 php 的 array 其实更对应 Java 的 map, 人家就叫关联数组
    aristotll
        9
    aristotll  
       Apr 30, 2018
    由于 php 关联数组的灵活性(可以当 list 用也可以当 map 用)
    所以 Java 应该是没有这个的 (list 和 map 本身接口都不一样 当然要实现一个应该也不太难吧
    shiji
        10
    shiji  
       Apr 30, 2018 via Android
    @Seanfuck 我以前测过,for 循环是最快的。php 里 array 开头的函数性能都一般。如果数据量大小不可预见,最好还是 for 循环
    sagaxu
        11
    sagaxu  
       Apr 30, 2018 via Android
    list.map { it.column }
    realvalkyrie
        12
    realvalkyrie  
       Apr 30, 2018   ❤️ 1
    Guava 的 Table ? getColumn ?
    lihongjie0209
        13
    lihongjie0209  
       Apr 30, 2018
    对象了解一下?
    msg7086
        14
    msg7086  
       May 1, 2018
    就是 stream().map().collect()。

    其实你要是觉得半函数式编程爽,不如来玩 Ruby,各种 MapReduce 飞起。

    names = users.map(&:first_name)
    cncqw
        15
    cncqw  
       May 1, 2018
    @shiji 又遇到先上来啥都不说就直接谈性能的,那点性能能干嘛
    UnluckyNinja
        16
    UnluckyNinja  
       May 1, 2018
    如果目的单纯就是集合内元素属性提取
    java 用一个集合函数方法加 lambda 应该就行
    groovy 有语法糖可以用,call with spread operator "*."
    php 文档第一个例子翻译成 groovy 差不多应该是
    ```groovy
    def records = [
    [
    id: 2135,
    first_name: 'John',
    last_name: 'Doe'
    ],
    ...
    ]

    def first_names = records*.first_name
    println first_names
    ```
    HangoX
        17
    HangoX  
       May 1, 2018 via Android
    请使用 groovy 实现
    lol173
        18
    lol173  
       May 2, 2018
    @Seanfuck what ??你确定你是 PHP 老司机?
    wingyiu
        19
    wingyiu  
       May 2, 2018
    论内置数据结构的丰富性,我只服 PHP
    picasso2501
        20
    picasso2501  
       May 2, 2018
    @lol173 恰恰因为他是老司机,这是 5.4 的 **新功能** 哈哈

    如果是 php4 的语法,他应该是很熟的。。。。

    其实我不是在讽刺他,这很正常,php7.1 和 7.2 的 new feature 我到现在都没看。。。。
    Navee
        21
    Navee  
       May 2, 2018
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3101 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 144ms · UTC 13:45 · PVG 21:45 · LAX 06:45 · JFK 09:45
    ♥ Do have faith in what you're doing.