Java 流使用函数编程

Java 流使用函数编程,java,lambda,java-8,java-stream,Java,Lambda,Java 8,Java Stream,然而,正如我试图通过函数式编程来解决的那样,我一直在努力提高使用效率。请求是否可以检查在这种情况下可以做什么 对于给定的事务数组(每个事务都有一个项目名称),按项目名称对所有事务进行分组。返回字符串数组,其中每个字符串包含项目名称,后跟空格,然后是事务数。对于具有匹配事务数的项目,按事务数降序排列数组,然后按项目名称字母升序排列 例如: 列表=[“粗体”、“非”、“粗体”、“粗体”] O/p: 粗体3 不是1 尝试了下降的方法,但对函数编程用法感到困惑: import java.util.Arr

然而,正如我试图通过函数式编程来解决的那样,我一直在努力提高使用效率。请求是否可以检查在这种情况下可以做什么

对于给定的事务数组(每个事务都有一个项目名称),按项目名称对所有事务进行分组。返回字符串数组,其中每个字符串包含项目名称,后跟空格,然后是事务数。对于具有匹配事务数的项目,按事务数降序排列数组,然后按项目名称字母升序排列

例如: 列表=[“粗体”、“非”、“粗体”、“粗体”]

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);
    }
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.Map;
导入java.util.function.function;
导入java.util.stream.collector;
公共类PYT{
公共静态void main(字符串[]args){
列表=新的ArrayList();
列表。添加(“粗体”);
列表。添加(“非”);
列表。添加(“粗体”);
列表。添加(“粗体”);
系统输出打印LN(grT(列表));
}
私有静态列表grT(列表列表){
//这里这是错误的,因为抛出错误
映射频率=((字符序列)列表).codePoints()
.parallel()
.boxed()
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting());
//按降频排序并将代码点收集到数组中
int[]输出=频率。入口集()
.parallelStream()
.sorted(Map.Entry.comparingByValue().reversed())
.mapToInt(Map.Entry::getKey)
.toArray();
//从字符串列表创建输出字符串。
//无法找到返回字符串-->错误列表的方法
返回新字符串(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;
}
私有静态列表递增顺序(列表){
Map stringCount=new HashMap();
for(字符串t:list){
合并(t,1L,Long::sum);
}
List-toSort=new-ArrayList();
对于(Map.Entry e:stringCount.entrySet()){
加入(e);
}
toSort.sort(Map.Entry.comparingByValue().reversed().then比较(Map.Entry.comparingByKey());
List refinedList=新建ArrayList();
用于(地图条目e:toSort){
字符串s=e.getKey()+“”+e.getValue();
精简列表。添加(s);
}
返回细化列表;
}

首先使用tx name作为键创建一个映射,并将其计数为值。然后使用此映射构建所需的结构。还要注意的是,当您使用
Map.Entry.comparingByValue()
以及另一个链式操作符(如
reversed
thenComparing
)时,类型推断的功能不足以为自己找出类型,因此我们不得不帮助它显式地提供它们。这是一个报告的bug,您可能会发现它

private static final String SPACE=”“;
映射频率Map=tx.stream()
.collect(collector.groupingBy(t->t,collector.counting());
列表formattedTx=frequencyMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue().reversed())
.thenComparing(Map.Entry.comparingByKey())
.map(e->e.getKey()+空格+e.getValue())
.collect(Collectors.toList());

StringBuilder
对于
.map(e->e.getKey()+“”+e.getValue())
中的连接,这里可能有点过火了。谢谢,如果我想在下一行中打印[bold 3不是1],我使用的是System.lineSeparator(),它解析为下一行,但逗号没有通过,无法使用“替换”。@RezD'Souza排序后如何打印最终的
列表或条目?您只需选择调用
grT(list).forEach(System.out::println)我正在通过System.out.println(grT(列表))打印;我在想,我们是否可以使用一些正则表达式来修剪正在打印的“,”并将第二个字符串列表打印到新行。
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());