달성하고자하는 것이나 데이터 구조가 어떻게 보이는지 분명하지 않습니다. 당신이 작성한 코드와 보여준 코드가 일치하지 않습니다. 그들은 매우 다른 것들을 성취합니다.
그러나 Collectors.maxBy
을 사용하고 Collectors.toMap
을 사용하지 않고 해결책을 원했습니다. 다른 대답은 그런 해결책을 제공하지 못했습니다. 그러므로 나는 하나를 주려고 노력할 것이다.
귀하의 데이터 : 데이터 구조로
난 당신이 각각 다수의 직원을 고용 고용주가 가정합니다. 모든 직원은 몇 가지 개인 정보, 그가 근무하는 위치 및 그가 알 수있을 때까지이 직위에서 근무한 기간을가집니다.
예 (지도 employerEmployeeMap)의 경우 :
CompanyA: [{Max, Developer, 5.4}, {Mari, Designer, 6.8}, {Paul, Manager, 1.2}]
CompanyB: [{Lea, CEO, 7.1}, {Tom, Admin, 4.5}, {Ria, Marketing, 0.4}]
CompanyC: [{Alex, Secretary, 1.5}, {Lisa, Developer, 3.3}, {Mia, Developer, 2.7}]
당신이 무엇을 달성하고자하는
:
당신은 시간이 가장 긴 PersonPositionDuration
을 갖고 싶어. 모든 고용주 또는 각 고용주에 대해 가장 긴 기간을 가진 사람 만 원하면 확실하지 않았습니다. 따라서 여러 솔루션을 제공 할 것입니다. maxBy와
솔루션
당신은 당신이 바로 직원 반복 가장 높은 기간과 하나 얻을 수있는 모든 고용주를 통해 시간이 가장 긴 사람하려면 다음
Optional<PersonPositionDuration> maxDurationPerson = employerEmployeeMap.values().stream()
.flatMap(List::stream)
.collect(maxBy(comparing(PersonPositionDuration::getDuration)));
을
이것은 다음과 같습니다.
Optional<PersonPositionDuration> maxDurationPerson = employerEmployeeMap.values().stream()
.flatMap(List::stream)
.max(comparing(PersonPositionDuration::getDuration));
01 23,414,결과 :
{Lea, CEO, 7.1}
당신이 가장 높은 기간 만 사람을 원하지만 고용주 정보를 잃고 싶지 않는 경우 :
Optional<Pair<String, PersonPositionDuration>> maxDurationPersonCompany = employerEmployeeMap.entrySet().stream()
.flatMap(entry -> entry.getValue().stream().map(p -> new Pair<>(entry.getKey(), p)))
.max(comparing(entry -> entry.getValue().getDuration()));
결과 : CompanyB: {Lea, CEO, 7.1}
당신은 가장 높은 가진 사람을 원하는 경우
Map<String, Optional<Pair<String, PersonPositionDuration>>> maxDurationPerCompany = employerEmployeeMap.entrySet().stream()
.flatMap(entry -> entry.getValue().stream().map(p -> new Pair<>(entry.getKey(), p)))
.collect(groupingBy(Pair::getKey, maxBy(comparing(entry -> entry.getValue().getDuration()))));
결과 :다음 작업을 수행 할 수있는 모든 회사의 지속 시간
CompanyA: {Mari, Designer, 6.8}
CompanyB: {Lea, CEO, 7.1}
Companyc: {Lisa, Developer, 3.3}
귀하의 질문과 필요 사항이 모두 포함되기를 바랍니다. 그렇지 않은 경우, 누락 된 것이 있거나 불분명 한 것이있는 경우 언제든지 의견을 말하십시오.
전체 작업 예 :
import javafx.util.*;
import java.util.*;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.*;
public class Main {
public static void main(String[] args) {
class PersonPositionDuration {
private final String person;
private final String position;
private final Double duration;
public PersonPositionDuration(String person, String position, Double duration) {
this.person = person;
this.position = position;
this.duration = duration;
}
public Double getDuration() {
return duration;
}
}
Map<String, List<PersonPositionDuration>> employerEmployeeMap = new HashMap<>();
employerEmployeeMap.put("CompanyA", Arrays.asList(new PersonPositionDuration("Max", "Developer", 5.4), new PersonPositionDuration("Mari", "Designer", 6.8), new PersonPositionDuration("Paul", "Manager", 1.2)));
employerEmployeeMap.put("CompanyB", Arrays.asList(new PersonPositionDuration("Lea", "CEO", 7.1), new PersonPositionDuration("Tom", "Admin", 4.5), new PersonPositionDuration("Ria", "Marketing", 0.4)));
employerEmployeeMap.put("CompanyC", Arrays.asList(new PersonPositionDuration("Alex", "Secretary", 1.5), new PersonPositionDuration("Lisa", "Developer", 3.3), new PersonPositionDuration("Mia", "Developer", 2.7)));
Optional<PersonPositionDuration> maxDurationPerson = employerEmployeeMap.values()
.stream()
.flatMap(List::stream)
.max(comparing(PersonPositionDuration::getDuration));
Optional<Pair<String, PersonPositionDuration>> maxDurationPersonCompany = employerEmployeeMap.entrySet()
.stream()
.flatMap(entry -> entry.getValue()
.stream()
.map(p -> new Pair<>(entry.getKey(), p)))
.max(comparing(entry -> entry.getValue()
.getDuration()));
Map<String, Optional<Pair<String, PersonPositionDuration>>> maxDurationPerCompany = employerEmployeeMap.entrySet()
.stream()
.flatMap(entry -> entry.getValue()
.stream()
.map(p -> new Pair<>(entry.getKey(), p)))
.collect(groupingBy(Pair::getKey, maxBy(comparing(entry -> entry.getValue()
.getDuration()))));
}
}
이미 시도한 작업을 보여주십시오. – mangotang