下面是Employee課堂。
public static class Employee {
private String name;
private int age;
private int salary;
// constructor, getters, setters, etc.
}
有一份員工名單。
我需要獲取age大于或等于32年數的員工串列,然后將其salary增加50%并收集這些員工到一個新的串列中。
樣本資料:
List<Employee> el = new ArrayList<Employee>();
el.add(new Employee("A",30,3000));
el.add(new Employee("B",32,3000));
el.add(new Employee("C",33,5000));
我的嘗試:
el.stream()
.filter(i -> i.getAge() >= 32)
.map(i -> i.getSalary() *3 / 2)
.collect(Collectors.toList());
但這是回傳整數型別的串列 - List<Integer>。相反,我希望回傳的串列是 Employee 型別的串列 - List<Employee>。
uj5u.com熱心網友回復:
更改流中元素的狀態不是一個好習慣。
相反,您可以使用流過濾具有目標年齡的員工。然后使用方法申請薪金變動Iterable.forEach()。
List<Employee> employeeOlder32 = el.stream()
.filter(i -> i.getAge() >= 32)
.toList(); // for Java 16 or collect(Collectors.toList()) for earlier versions
employeeOlder32.forEach(employee ->
employee.setSalary(employee.getSalary() * 3 / 2)
);
旁注:一種常見的做法是BigDecimal用于價格、工資等(不是int或double)。
uj5u.com熱心網友回復:
您不能修改流中的資料,最終您需要創建新串列并將操作資料存盤在其中。
List<Employee> newList = el.stream()
.map(f -> new Employee(f.getName(),f.getAge(), f.setSalary((f.getSalary()*3)/2)))
.collect(Collectors.toList());
uj5u.com熱心網友回復:
其他兩個答案都說您不應該或不能更改正在流式傳輸的物件。據我所知,該指令是不正確的。我懷疑他們混淆了您不應該修改正在流式傳輸的集合的結構的規則,例如在源串列中添加/洗掉物件。您可以修改集合元素的內容。
在流式傳輸時修改物件
當我們流式傳輸串列時,在每個Employee物件元素上,我們呼叫Employee#setSalary以使用新計算的值進行更新。
使用擴展符號,代碼的關鍵位如下。
當我們流式傳輸串列時,我們使用Stream#forEach在每個元素上運行一些代碼。
將我們的int欄位乘以salary一個float型別會產生一個float值。呼叫Math.round將其轉換回int.
employees
.stream()
.forEach (
( Employee employee ) ->
{
employee.setSalary (
Math.round( employee.getSalary () * 1.5F )
);
}
)
;
這是使用緊湊符號的完整示例。
為方便起見,我們使用List.of文字語法生成不可修改的串列。
List < Employee > employees = List.of(
new Employee( "Alice" , 30 , 3000 ) ,
new Employee( "Bob" , 32 , 3000 ) ,
new Employee( "Carol" , 33 , 5000 )
);
System.out.println( "Before: " employees );
employees.stream().forEach( employee -> employee.setSalary( Math.round( employee.getSalary() * 1.5F ) ) );
System.out.println( "After: " employees );
結果:
Before: [Employee[name=Alice, age=30, salary=3000], Employee[name=Bob, age=32, salary=3000], Employee[name=Carol, age=33, salary=5000]]
After: [Employee[name=Alice, age=30, salary=4500], Employee[name=Bob, age=32, salary=4500], Employee[name=Carol, age=33, salary=7500]]
僅供參考,這是Employee上面使用的類。沒意思。
package work.basil.example.modstream;
import java.util.Objects;
public final class Employee
{
// Member fields.
private String name;
private int age;
private int salary;
// Constructor
public Employee ( String name , int age , int salary )
{
this.name = name;
this.age = age;
this.salary = salary;
}
// Accessors
public String getName ( ) { return name; }
public void setName ( final String name ) { this.name = name; }
public int getAge ( ) { return age; }
public void setAge ( final int age ) { this.age = age; }
public int getSalary ( ) { return salary; }
public void setSalary ( final int salary ) { this.salary = salary; }
// `Object` overrides.
@Override
public boolean equals ( Object obj )
{
if ( obj == this ) { return true; }
if ( obj == null || obj.getClass() != this.getClass() ) { return false; }
var that = ( Employee ) obj;
return Objects.equals( this.name , that.name ) &&
this.age == that.age &&
this.salary == that.salary;
}
@Override
public int hashCode ( )
{
return Objects.hash( name , age , salary );
}
@Override
public String toString ( )
{
return "Employee["
"name=" name ", "
"age=" age ", "
"salary=" salary ']';
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/505425.html
上一篇:從串列python中添加和洗掉值
