我有一个类值:
public class Values {
private int count;
private int values;
}
Run Code Online (Sandbox Code Playgroud)
以及多个类型的地图列表 Map<String, Values>
Map<String, Values> map1 = new HashMap<>();
map1 .put("aaa", new Values(1, 10));
map1 .put("bbb", new Values(5, 50));
map1 .put("ccc", new Values(2, 30));
Map<String, Values> map2= new HashMap<>();
map2.put("aaa", new Values(2, 20));
map2.put("bbb", new Values(3, 50));
map2.put("ccc", new Values(3, 10));
List<Map<String, Values>> list = Arrays.asList(map1, map2);
Run Code Online (Sandbox Code Playgroud)
地图中可以有任意数量的地图和任意数量的条目,但地图的键始终相同,只有值可以不同.为清晰起见,我的示例仅包含2个地图,每个地图仅包含3个条目.
我想获得一个具有相同键的单个映射,并使用Values对象作为对象中每个"count"和每个"value"的总和,如下所示:
{
aaa= {
count = 3,
value = 30
},
bbb= {
count = 8,
value = 100
}, …Run Code Online (Sandbox Code Playgroud) 我正在学习Java中的Lambda,并尝试理解它.例如,我有以下代码:
@FunctionalInterface
interface Square
{
int calculate(int x);
}
class Test
{
public static void main(String args[])
{
int a = 5;
Square s = (int x) -> x*x;
int ans = s.calculate(a);
System.out.println(ans);
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白这个陈述Square s = (int x) -> x*x;我看到的s是类型的引用变量Square.但我不明白到底是什么(int x) -> x*x.这是方法定义吗?这种方法的返回类型是什么?我认为这应该是int因为calculate方法的返回类型是int.任何反馈将不胜感激.
我正在尝试计算在“对象列表”中的字段中看到一个Int的次数。
这是我的代码
TreeMap<Integer, Double> ratings = new TreeMap();
ArrayList<Establishment> establishments = new ArrayList<>();
double one = 0;
double two = 0;
double three = 0;
double five = 0;
for (Establishment e : establishments) {
if (e.getRating() == 1) {
one++;
}
if (e.getRating() == 2) {
two++;
}
if (e.getRating() == 3) {
three++;
}
if (e.getRating() == 5) {
five++;
}
}
ratings.put(1, (one / establishments.size()) * 100);
ratings.put(2, (two / establishments.size()) * 100);
ratings.put(3, (three / establishments.size()) …Run Code Online (Sandbox Code Playgroud) 我正在编写一种简单的方法来打印一系列游戏结果的统计数据。每个游戏都有一个结果列表,其中包含根据游戏结果列出的枚举。我的老师在我的代码中注释了一个TODO:
public static void printStatistics(List<Game> games) {
float win = 0;
float lose = 0;
float draw = 0;
float all = 0;
//TODO: should be implemented /w stream API
for (Game g : games) {
for (Outcome o : g.getOutcomes()) {
if (o.equals(Outcome.WIN)) {
win++;
all++;
} else if (o.equals(Outcome.LOSE)) {
lose++;
all++;
} else {
draw++;
all++;
}
}
}
DecimalFormat statFormat = new DecimalFormat("##.##");
System.out.println("Statistics: The team won: " + statFormat.format(win * 100 / all) + " …Run Code Online (Sandbox Code Playgroud) 我们如何通过使用Java流以最佳方式将List <Foo>转向Map <propertyA,List <propertyB >>。
当心:propertyA不是唯一的
//pseudo-code
class Foo
propertyA //not unique
List<propertyB>
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有以下内容:
fooList.stream()
.collect(Collectors.groupingBy(Foo::propertyA,
Collectors.mapping(Foo::propertyB, Collectors.toList())))
Run Code Online (Sandbox Code Playgroud)
导致Map<propretyA, List<List<propretyB>>>的价值尚未被压平。
我目前正在学习 Java 中的流,但是当我尝试编写一些像这样的简单代码时
Map<String, Integer> map = new TreeMap<>();
map.put("Tom", 5);
map.put("Andrew", 6);
map.put("Kim", 3);
map.put("Milo", 2);
map.stream();
Run Code Online (Sandbox Code Playgroud)
它让我java cannot resolve method stream()
在 Java 11 中使用 Inttelij 和编码,老实说我不知道发生了什么。
我已经阅读了很多有关Java内存区域的信息,但看起来只是一团糟。主要是由于引入了新MetaSpace区域而不是PermGenJava8。现在有问题:
heapjava8 +中包括哪些区域?static的方法和变量存储之前java8和java8 +?MetaSpace除了类元数据信息外,是否存储任何其他内容?JVM?谢谢您的回答。