开发者社区> 问答> 正文

流使用功能编程

但是,由于试图通过函数式编程来解决,我一直坚持要更有效地使用它。询问是否可以检查出在这种情况下可以做什么。

对于给定的交易数组,每个交易都有一个项目名称,将所有交易按项目名称分组。返回一个字符串数组,其中每个字符串都包含项目名称,后跟一个空格,然后是交易数量。对具有匹配交易计数的项目,将数组按交易计数降序排列,然后按项目名称按字母顺序升序排列。

示例:list = [“ bold”,“ not”,“ bold”,“ bold”]

O / P:

粗体3 不是1

尝试了直到下降的方法,但对函数式编程用法感到困惑:

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class PYT {

    public static void main(String[] args) {            
        List<String> list = new ArrayList<String>();
        list.add("bold");
        list.add("not");
        list.add("bold");
        list.add("bold");
        System.out.println(grT(list));
    }

    private static List<String> grT(List<String> list) {  
        //here this is wrong, as throwing error
         Map<Integer, Long> frequencies = ((CharSequence) list).codePoints()  
                       .parallel()
                       .boxed()
                       .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        // sort by descending frequency and collect code points into array
        int[] output = frequencies.entrySet()
                               .parallelStream()
                               .sorted(Map.Entry.<Integer, Long>comparingByValue().reversed())
                               .mapToInt(Map.Entry::getKey)
                               .toArray();

        // create output string from List of Strings.  
        // Could not think of a way to return a list of string --> Error
        return new String(output, 0, output.length);
    }
}

如果可以通过使用分解/功能编来解释如何检查这一点将很有帮助。方法。

更新1:坚决清除代码后,如果函数式编程未使用它,则看起来更加干净

private static List<String> ascendingOrder(List<String> list) {
    Map<String, Long> stringCount = new HashMap<>();
            for (String t : list) {
                stringCount.merge(t, 1L, Long::sum);
            }

            List<Map.Entry<String, Long>> toSort = new ArrayList<>();
            for (Map.Entry<String, Long> e : stringCount.entrySet()) {
                toSort.add(e);
            }
            toSort.sort(Map.Entry.<String, Long>comparingByValue().reversed().thenComparing(Map.Entry.comparingByKey()));
            List<String> refinedList = new ArrayList<>();
            for (Map.Entry<String, Long> e : toSort) {
                String s = e.getKey() + " " + e.getValue();
                refinedList.add(s);
            }

            return refinedList;
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-25 13:44:53 441 0
1 条回答
写回答
取消 提交回答
  • 首先使用TX名称作为键创建一个地图,并将其计为值。然后使用此地图构建所需的结构。还要注意,当您将Map.Entry.comparingByValue()with与另一个链接的运算符(如reversed或)一起使用时thenComparing,类型推断的功能不足以自行确定类型,因此我们被迫帮助它明确提供它们。这是一个已报告的错误,您可以在这里找到。

    private static final String SPACE = " ";
    
    Map<String, Long> frequencyMap = tx.stream()
        .collect(Collectors.groupingBy(t -> t, Collectors.counting()));
    
    List<String> formattedTx = frequencyMap.entrySet().stream()
        .sorted(Map.Entry.<String, Long>comparingByValue().reversed()
            .thenComparing(Map.Entry.comparingByKey()))
        .map(e -> e.getKey() + SPACE + e.getValue())
        .collect(Collectors.toList());
    

    回答来源:Stack Overflow

    2020-03-25 13:58:29
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
为并行图数据处理提供高层抽象/语言 立即下载
复杂升学环境下的语言交互:技术与实践 立即下载
如何使用Tair增强数据结构构建丰富在线实时场景 立即下载