是否可以根据所需的条件匹配过滤和映射两个集合,如下所示:
fun main(args: Array<String>) {
val selectedDates = listOf("2018-08-12", "2018-08-13", "2018-08-14")
val expenses = listOf(Expense("Food", "2018-08-12"),
Expense("Transportation", "2018-08-15"),
Expense("Misc.", "2018-08-13"),
Expense("Uber", "2018-08-12"),
Expense("Clothing", "2018-08-16"))
val listOfExpensesInSelectedDate = mutableListOf<Expense>()
for (date in selectedDates){
listOfExpensesInSelectedDate.addAll(expenses.filter { it.date==date })
}
println(listOfExpensesInSelectedDate)
}
data class Expense(
val expense:String,
val date: String
)
Run Code Online (Sandbox Code Playgroud)
提供上面给定的代码,我试图返回与另一个字符串列表中的日期匹配的费用列表。在上面的例子中,我同时使用了循环和过滤函数来获得我想要的结果。但是是否可以在一行代码中避免 for 循环和过滤器并映射两个集合?
我正在尝试使用 Angular ngClass 指令在 div 上制作悬停效果。在模板文件中,我有一个类为“list-item-container”的 div 元素,其中包含一个类为“list-item”的 div,并使用 *ngFor 指令进行迭代。在“list-item”div 元素内,我有三个具有“list-item-column”类的 div 水平放置,就像具有内联显示的表格行一样。在具有“list-item”类的 div 中,我放置了一个 mouseenter 和 mouseleave 事件侦听器,它们在我的 .ts 中调用hoverListItem()文件。hoverListItem() 函数将 listItemHovered 变量的值更改为 true 或 false。在“list-item”类中,我有一个 ngClass 指令,它根据 listItemHovered 布尔值触发 css 类“list-item-highlight”然后将背景更改为不同颜色的值。
我面临的问题是,将鼠标指针悬停在“列表项”div 上时,我的所有“列表项”div 都会受到影响,而不是我悬停的那个。如何解决这个问题呢?
.html 文件
<div class="list-item-container">
<ng-container *ngFor="let opportunity of dealService.opportunities">
<div [ngClass]="{'list-item-highlight': listItemHovered}" class="list-item" (mouseenter)="hoverListItem()"
(mouseleave)="hoverListItem()"
(click)="selectOpportunity(opportunity)">
<div
class="list-item-column">{{opportunity.account?.name === null ? "N/A" : opportunity.account.name}}</div>
<div class="list-item-column">{{opportunity.requirementTitle}}</div>
<div class="list-item-column">{{opportunity.salesValue | number: '1.0-2'}}</div>
</div>
</ng-container>
</div>
Run Code Online (Sandbox Code Playgroud)
.css 文件
.list-item-container{
overflow: auto;
width: 100%;
max-height: 500px; …Run Code Online (Sandbox Code Playgroud) 我有一个Flutter应用程序,正在使用SQFLITE插件从Sqlite数据库中获取数据。在这里,我面临一个奇怪的问题。根据我的理解,我们使用async / await或then()函数进行异步编程。在这里,我有一个db.query()方法,该方法正在执行一些SQL查询以从db获取数据。在此函数获取数据之后,我们将在.then()函数中进行一些进一步的处理。但是,用这种方法我遇到了一些问题。从我调用此getExpensesByFundId(int fundId)函数的位置来看,它似乎无法正确获取数据。它应该返回Future>对象,然后在数据可用时将其转换为List。但是当我打电话时它不起作用。
但是,我只是对其进行了一些试验,并在db.query()函数之前添加了“ await”关键字,并且以某种方式使其开始可以正常工作。您能否解释为什么添加await关键字可以解决此问题?我以为使用.then()函数时,我们不需要使用await关键字。
这是我的代码:
Future<List<Expense>> getExpensesByFundId(int fundId) async {
Database db = await database;
List<Expense> expenseList = List();
// The await in the below line is what I'm talking about
await db.query(expTable,where: '$expTable.$expFundId = $fundId')
.then((List<Map<String,dynamic>> expList){
expList.forEach((Map<String, dynamic> expMap){
expenseList.add(Expense.fromMap(expMap));
});
});
return expenseList;
}
Run Code Online (Sandbox Code Playgroud) 在 Angular html 模板中,我有一个带有 mat-select 指令(Angular Material)的选择元素,它从存储在服务 .ts 文件中名为“selectedCriteria”的属性中的对象加载数据。根据应用程序逻辑,三种类型的对象可以存储在“selectedCriteria”属性中,并且它们都具有字符串类型的“name”变量。所以我想在这个选择元素中加载对象名称。问题是当应用程序加载时,选择元素不会加载/显示“selectedCriteria”,因为它是一个对象而不是字符串。我可以使用 selectedCriteria.name 将其与字符串值绑定,但这将添加额外的代码以从对象转换为字符串,反之亦然,以进行双向数据绑定。在应用程序启动期间在此选择元素中加载对象的解决方案是什么?
<mat-form-field style="width: 45%; display: inline-block">
<mat-select placeholder="Criteria" [(value)]="reportService.selectedCriteria" (valueChange)="reportService.filterSalesData()">
<mat-option *ngFor="let criteria of reportService.criteriaList" [value]="criteria">{{criteria.name}}</mat-option>
</mat-select>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud) 鉴于这个带有 ArrayList 的 StudentList 类采用具有三个字段的 Student 对象:Roll number、Name 和 Marks,如何编写增强的 For 循环而不是下面代码中编写的常规 For 循环方法?
学生.java
public class Student {
private int rollNumber;
private String name;
private int marks;
public Student(int roll, String name, int marks){
this.rollNumber=roll;
this.name=name;
this.marks=marks;
}
public int getRollNumber(){
return this.rollNumber;
}
public String getName() {
return name;
}
public int getMarks() {
return marks;
}
}
Run Code Online (Sandbox Code Playgroud)
这是带有 Main 方法的 SudentList 类
学生名单.java
import java.util.ArrayList;
import java.util.List;
public class StudentList {
public static void main(String[] args) { …Run Code Online (Sandbox Code Playgroud)