我正在尝试自学如何在Java中使用lambda表达式而不是常规的外部迭代类型解决方案.我做了一个简短的程序,我使用整数流并为它们分配等级字母.我没有使用典型的"switch"语句,而是使用了这些lambda表达式.有什么办法可以简化吗?似乎有更好的方法来做到这一点.它有效,但看起来并不整洁.
grades.stream()
.mapToInt(Integer::intValue)
.filter(x -> x >= 90)
.forEach(x -> System.out.println(x + " -> A"));
grades.stream()
.mapToInt(Integer::intValue)
.filter(x -> x >= 80 && x <= 89)
.forEach(x -> System.out.println(x + " -> B"));
grades.stream()
.mapToInt(Integer::intValue)
.filter(x -> x >= 70 && x <= 79)
.forEach(x -> System.out.println(x + " -> C"));
grades.stream()
.mapToInt(Integer::intValue)
.filter(x -> x >= 60 && x <= 69)
.forEach(x -> System.out.println(x + " -> D"));
grades.stream()
.mapToInt(Integer::intValue)
.filter(x -> x >= 50 && x <= 59) …Run Code Online (Sandbox Code Playgroud) 有没有一种很好的方法将字符串列表(使用Collectos API?)转换/转换为HashMap?
StringList和Map:
List<String> entries = new ArrayList<>();
HashMap<String, String> map = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
...
我的StringList包含如下字符串:
entries.add("id1");
entries.add("name1, district");
entries.add("id2");
entries.add("name2, city");
entries.add("id3");
entries.add("name3");
Run Code Online (Sandbox Code Playgroud)
输出应该是:
{id1=name1, district, id2=name2, city, id3=name3}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有一个数据结构 - ArrayList>这就是我需要做的 -
ArrayList<HashMap<String, String>> optMapList;
//populated optMapList with some code. Not to worry abt this
List<String> values = new ArrayList<String>();
for(HashMap<String,String> entry: optMapList){
values.add(entry.get("optValue"));
}
Run Code Online (Sandbox Code Playgroud)
我们如何使用Java Streams实现相同的目标?
在通过以下答案讨论在流中使用并发数据结构以及使用并发映射和转换为映射之间的不同之后,有人可以解释如果我使用collectie 的其他语法将会发生什么
Stream<Integer> integers = Stream.iterate(1, n -> n + 1).parallel();
Map<Integer, Boolean> resultMap = integers
.limit(1000)
.collect(HashMap::new,
(map, value) -> map.put(value, false),
HashMap::putAll);
Run Code Online (Sandbox Code Playgroud)
根据文档,将根据生成的线程数调用供应商.如果我使用ConcurrentHashMap而不是HashMap?
当并行执行时,可以实例化,填充和合并多个中间结果,以便保持可变数据结构的隔离.因此,即使与非线程安全数据结构(例如ArrayList)并行执行,也不需要额外的同步来进行并行缩减.
我在我的控制器中有以下代码片段,从第一个列表中我得到了所有用户,然后我迭代向他们发送电子邮件.不知何故,在函数中sendEmail()我不得不返回一个对象,但我不需要返回任何东西.
List<User> users = new ArrayList<User>();
menuPriceSummaryService.findAll()
.stream()
.map (mps -> checkPreferences(mps))
.iterator()
.forEachRemaining(users::addAll);
users
.stream()
.map (o -> sendEmail(o))
.iterator();
Run Code Online (Sandbox Code Playgroud) 我坚持用一些优雅的方法BigDecimal在地图中得到s 的总和.我知道如何计算地图中的总和BigDecimal而不是带有的对象列表BigDecimal.
我的对象的结构如下:
Class Obj {
private BigDecimal b;
// Getter for b, say getB()
}
Map<String, List<Obj>> myMap;
Run Code Online (Sandbox Code Playgroud)
我需要得到所有b的总和myMap.在Java中寻找一些优雅的方法,可能会使用流?
我希望以前没有问过这个问题.在java 8中,我myArray在输入中有一个String数组和一个整数maxLength.我想计算我的数组中的字符串数小于maxLength.我想使用流来解决此问题.
为此,我想这样做:
int solution = Arrays.stream(myArray).filter(s -> s.length() <= maxLength).count();
Run Code Online (Sandbox Code Playgroud)
但是我不确定这是否是正确的方法.它需要经过第一个数组,然后通过过滤后的数组进行计数.
但是如果我不使用流,我可以轻松地制作一个算法,我在myArray上循环一次.
我的问题很简单:有没有办法用相同的时间性能来解决这个问题而不是循环?它总是一个使用流的"好"解决方案吗?
我只是在重构一些旧项目以使用Java 8的功能.
int counter = 1;
for (Checker checker : checkers) {
if (counter < checkers.size()) {
checker.setNextChecker(checkers.get(counter++));
}
}
Run Code Online (Sandbox Code Playgroud)
这是一种响应链模式.我需要为列表中的每个检查器设置下一个检查器,不包括最后一个检查器.
还是找不到在这里使用Stream API的方法:)
我有以下代码,我想使用Java 8流API进行翻译.
count = 0;
for (A a : someList) {
if (a.get() == 1) {
count++;
newList.add(new X(count));
}
}
Run Code Online (Sandbox Code Playgroud)
映射时如何计算?
newList = someList.stream
.filter(a -> a.get().equals("red"))
.map(a -> new X(count))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 我曾经使用以下行来在Java中出现流之前对元素进行排序:
Collections.sort(collection, new CustomComparator());
Run Code Online (Sandbox Code Playgroud)
现在我发现我可以通过以下方式获得相同的结果:
collection.stream().sorted(new CustomComparator());
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我都使用一个列表和一个实现的自定义比较器Comparator(我需要这个并且不能在类本身上实现它).
我没有找到任何表明它们在任何方面都不同的东西,除了流解决方案看起来更好的事实.
使用流版本有什么好处吗?我的意思是......性能更高吗?有什么区别吗?
java-stream ×10
java ×8
java-8 ×4
lambda ×4
collections ×3
hashmap ×2
bigdecimal ×1
foreach ×1
list ×1
sorting ×1