Jdk8 stream流使用求助

z6883123 2021-03-13 03:14:25
题目:使用stream流先根据字母出现次数排序再根据字母降序排序,求大佬帮忙。
如:String s = "BBAACCC",则排序后结果为一个Map,Map里内容为C3B2A2
...全文
497 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰思雨 2021-04-07
  • 打赏
  • 举报
回复
引用 楼主 z6883123 的回复:
题目:使用stream流先根据字母出现次数排序再根据字母降序排序,求大佬帮忙。 如:String s = "BBAACCC",则排序后结果为一个Map,Map里内容为C3B2A2
        final String value = "BBAACCC";
        String result = value.chars().mapToObj(v->(char)v).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                .entrySet().stream().sorted((e1, e2)->{
                    int compare = Long.compare(e2.getValue(), e1.getValue());
                    return compare == 0 ? Character.compare(e2.getKey(), e1.getKey()) : compare;
        }).map(e->String.valueOf(e.getKey()) + e.getValue()).collect(Collectors.joining());

        System.out.println(result);
老王就是我 2021-04-02
  • 打赏
  • 举报
回复
先map再遍历map
冰思雨 2021-04-01
  • 打赏
  • 举报
回复
直接上代码:
        final String value = "BBAACCC";
        String result = value.chars().mapToObj(v->(char)v).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                .entrySet().stream().sorted((e1, e2)->{
                    int compare = Long.compare(e2.getValue(), e1.getValue());
                    return compare == 0 ? Character.compare(e2.getKey(), e1.getKey()) : compare;
        }).map(e->String.valueOf(e.getKey()) + e.getValue()).collect(Collectors.joining());

        System.out.println(result);
先将字符串转换成字符流, 然后分组计数, 然后排序, 然后将排序结果转成字符串, 然后拼接,得到结果。
KeepSayingNo 2021-03-15
  • 打赏
  • 举报
回复
z6883123 2021-03-15
  • 打赏
  • 举报
回复
引用 3 楼 hbhbhbhbhb1021 的回复:
public static void main(String[] args) {
        String s="BBAACCC";
        List<String> strings = Arrays.asList(s.split(""));
        Map<String, Long> collect = strings.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        Map<String, Long> result = new TreeMap<String, Long>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj2.compareTo(obj1);
                    }
                });
        for (String item:
                collect.keySet()) {
            result.put(item,collect.get(item));
        }
        System.out.println(result);
    }
您好,您第二段result并没有达到预期效果,可能我问题描述不太清楚,是要先根据字母出现次数排序,再根据字母自然降序排序,比如AABBCCCD,则要排序成C3B2A2D1这样子。 还有就是请问下您第二段处理result时能不能用stream流重写下啊,我第一个collect知道如何处理,第二个result那里我用stream流处理不了,附上我的错误代码,谢谢! Map<String, Long> result = collect.entrySet().stream() .sorted(Map.Entry.<String, Long>comparingByValue().thenComparing(Map.Entry.<String, Long>comparingByKey().reversed())) .collect( Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (oldVal, newVal) -> oldVal, TreeMap::new ) ); result.forEach((k, v) -> System.out.print(k + "" + v));
hbhbhbhbhb1021 2021-03-15
  • 打赏
  • 举报
回复
public static void main(String[] args) {
        String s="BBAACCC";
        List<String> strings = Arrays.asList(s.split(""));
        Map<String, Long> collect = strings.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        Map<String, Long> result = new TreeMap<String, Long>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj2.compareTo(obj1);
                    }
                });
        for (String item:
                collect.keySet()) {
            result.put(item,collect.get(item));
        }
        System.out.println(result);
    }
mymtom 2021-03-15
  • 打赏
  • 举报
回复
3楼的方法是对的,只是次数没有参与排序,稍微修改即可!

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;

public class StreamDemo {
public static void main(String[] args) {
String str = "AABBCCCD";
List<String> list = Arrays.asList(str.split(""));
Map<String, Long> map1 = list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Map<String, Long> map2 = new TreeMap<String, Long>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (map1.get(o2).equals(map1.get(o1))) {
return o2.compareTo(o1);
}
return map1.get(o2).compareTo(map1.get(o1));
}
});
map2.putAll(map1);
for (Entry<String, Long> entry : map2.entrySet()) {
System.out.printf("%s%d", entry.getKey(), entry.getValue());
}
System.out.printf("%n");
}
}
sunny boy . 2021-03-14
  • 打赏
  • 举报
回复
sort再groupby再map
sotondolphin 2021-03-13
  • 打赏
  • 举报
回复
用Collectors.groupingBy

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧