whatisnew
V2EX  ›  Java

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

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

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

    names = users.map(&:first_name)
    cncqw
    cncqw
    May 1, 2018
    15
    @shiji 又遇到先上来啥都不说就直接谈性能的,那点性能能干嘛
    UnluckyNinja
    UnluckyNinja
    May 1, 2018
    16
    如果目的单纯就是集合内元素属性提取
    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
    HangoX
    May 1, 2018 via Android
    17
    请使用 groovy 实现
    lol173
    lol173
    May 2, 2018
    18
    @Seanfuck what ??你确定你是 PHP 老司机?
    wingyiu
    wingyiu
    May 2, 2018
    19
    论内置数据结构的丰富性,我只服 PHP
    picasso2501
    picasso2501
    May 2, 2018
    20
    @lol173 恰恰因为他是老司机,这是 5.4 的 **新功能** 哈哈

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

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