我想转换我看起来像这样的地图:
{
key="someKey1", value=Apple(id="1", color="green"),
key="someKey2", value=Apple(id="2", color="red"),
key="someKey3", value=Apple(id="3", color="green"),
key="someKey4", value=Apple(id="4", color="red"),
}
Run Code Online (Sandbox Code Playgroud)
到另一个地图,将所有相同颜色的苹果放入同一个列表中:
{
key="red", value=list={apple1, apple3},
key="green", value=list={apple2, apple4},
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
Map<String, Set<Apple>> sortedApples = appleMap.entrySet()
.stream()
.collect(Collectors.toMap(l -> l.getColour, ???));
Run Code Online (Sandbox Code Playgroud)
我是在正确的轨道上吗?我应该使用过滤器来执行此任务吗?有没有更简单的方法?
我有一个list1包含不同的字符串,从另一个列表(fooBarList)的字符串开始.
List<String> list1 = Arrays.asList("FOO1234", "FOO1111", "BAR1", "BARRRRR");
List<String> fooBarList = Array.asList("FOO", "BAR");
Run Code Online (Sandbox Code Playgroud)
我想创建一个Hashmap<String, List<String>> hm从list1中分离出来的字符串,具体取决于它们的开头.
结果应如下所示:
{FOO=["FOO1234",FOO1111"], BAR=["BAR1", "BARRRRR"]}
Run Code Online (Sandbox Code Playgroud)
fooBarList定义了不同的键.
如何在溪流的帮助下实现这一目标?我只是不知道如何做我基本上说的步骤:FOO1234从FOO开始,所以检查密钥FOO是否存在并将其添加到列表中,否则创建一个新列表.(我想我必须遍历流中的fooBarList,这对我来说似乎是错的)
/ edit:确保fooBarList中只有2个值我可以做一个简单的检查,如果字符串以"foo"开头,那么就这样做.但是如果我的列表包含100个字符串呢?
我正在使用 mat-chips 过滤表格,一切似乎都很好:
主页.html
<mat-chip-list>
<mat-chip (click)="onSectorChipSelect(sector)" [color]="chipColor(sector)" *ngFor="let sector of sectors" selected>{{sector}}</mat-chip>
<mat-chip (click)="clearSectors()">
Clear
</mat-chip>
</mat-chip-list>
Run Code Online (Sandbox Code Playgroud)
home.ts将扇区添加到名为filtersSectors的列表中,该列表一次保存所有选定的扇区。
onSectorChipSelect(sector: string) {
if (this.filteredSectors.includes(sector)) {
this.filteredSectors.splice(this.filteredSectors.indexOf(sector), 1);
} else {
this.filteredSectors.push(sector);
}
}
Run Code Online (Sandbox Code Playgroud)
home.ts如果选中,此方法会将芯片着色为蓝色(主要),否则保持白色(基本)。
chipColor(sector: string) {
if (this.filteredSectors.includes(sector)) {
return 'primary';
}
return 'basic';
}
Run Code Online (Sandbox Code Playgroud)
“问题”是chipColor()方法绑定到芯片的颜色属性,因此在主页组件中滚动或单击时会多次调用。我的猜测是每次浏览器呈现时,都会再次调用该方法?(我真的不知道,也许有人可以解释一下?)
大约有 20 个扇区,所以我猜这不是一个真正的性能问题?知道这很烦人,我认为这不是一个好习惯。但我怎么能改变呢?
到目前为止我尝试的是将每个芯片的颜色设置为白色(基本):
主页.html
<mat-chip (click)="onSectorChipSelect(sector)" color="basic" *ngFor="let sector of sectors" selected>{{sector}}</mat-chip>
Run Code Online (Sandbox Code Playgroud)
家.ts
@ViewChild(MatChipList) chipList: MatChipList;
Run Code Online (Sandbox Code Playgroud)
使用 ViewChild 注释获取芯片列表,然后迭代芯片并设置它们的颜色,但这不起作用。选择第二个芯片后,第一个芯片再次变为白色。
onSectorChipSelect(sector: string) {
if (this.filteredSectors.includes(sector)) …Run Code Online (Sandbox Code Playgroud)