public class IterateMap {
public static void main(String[] args) {
System.out.println("========List of Objects=========");
Employee emp;
Map<String,Employee> anotherHashMap = new HashMap<String,Employee>();
ArrayList<Employee> elist = new ArrayList<Employee>();
elist.add(new Employee(1, "John","Pillay", 26));
elist.add(new Employee(2, "Richard","Santos", 25));
elist.add(new Employee(3, "Deep","Singh", 19));
elist.add(new Employee(4, "Sandeep","Narwal", 20));
elist.add(new Employee(5, "Benny","Gaucho", 30));
for(int i = 0; i < elist.size(); i++){
emp = elist.get(i);
anotherHashMap.put(UUID.randomUUID().toString(), emp);
}
System.out.println("================================");
System.out.println(anotherHashMap);
System.out.println("================================");
Map<String, Employee> resultMap = anotherHashMap.entrySet().stream().collect(
Collectors.toMap(Map.Entry::getKey, e -> new
Employee(e.getValue().getFirstName(),e.getValue().getAge())));
System.out.println(resultMap);
}
}
Сотрудник. java
public class Employee {
int id;
String firstName;
String lastName;
int age;
public Employee(int id, String firstName, String lastName, int age) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public Employee()
{
}
public Employee(String name, int age)
{
this.age= age;
this.firstName = name;
}
//Getter and setter
}
Вывод:
================ ================
{cf135244-6d17-471f-8b3a-fd9e0a4e89c2 = Сотрудник [id = 0, firstName = Benny, lastName = null, age = 30], 980a7fea-0616-46d3-8a57-88d66864c67f = Сотрудник [id = 0, firstName = Richard, lastName = null, age = 25], ae754c99-b998-4c54-9594-59f7268d9a32 = Сотрудник [id = 0, firstName = John, lastName = null, age = 26], f4ccc96f-06d7-4b72-98ca-94ae60eba703 = Сотрудник [id = 0, firstName = Sandeep, lastName = null, age = 20], ce4a50d2-34cf-404 c -87e4-435c51764a6f = Employee [id = 0, firstName = Deep, lastName = null, age = 19]}
Итак, в следующем выводе я хочу, чтобы атрибуты, представленные в список resultMap не должен содержать атрибутов «lastName = null» и «id = 0», список значений должен быть настроен на «firstName», «age». Я не беру другой объект для размещения значений. Можно ли сделать это в java -8?