Example usage for java.util Comparator comparing

List of usage examples for java.util Comparator comparing

Introduction

In this page you can find the example usage for java.util Comparator comparing.

Prototype

public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
        Function<? super T, ? extends U> keyExtractor) 

Source Link

Document

Accepts a function that extracts a java.lang.Comparable Comparable sort key from a type T , and returns a Comparator that compares by that sort key.

Usage

From source file:org.apache.nifi.toolkit.cli.impl.result.ControllerServicesResult.java

@Override
protected void writeSimpleResult(final PrintStream output) throws IOException {
    final Set<ControllerServiceEntity> serviceEntities = controllerServicesEntity.getControllerServices();
    if (serviceEntities == null) {
        return;//from w  w  w .  j  av  a 2  s .c o  m
    }

    final List<ControllerServiceDTO> serviceDTOS = serviceEntities.stream().map(s -> s.getComponent())
            .collect(Collectors.toList());

    Collections.sort(serviceDTOS, Comparator.comparing(ControllerServiceDTO::getName));

    final Table table = new Table.Builder().column("#", 3, 3, false).column("Name", 5, 40, false)
            .column("State", 5, 40, false).build();

    for (int i = 0; i < serviceDTOS.size(); i++) {
        final ControllerServiceDTO serviceDTO = serviceDTOS.get(i);
        table.addRow(String.valueOf(i + 1), serviceDTO.getName(), serviceDTO.getState());
    }

    final TableWriter tableWriter = new DynamicTableWriter();
    tableWriter.write(table, output);
}

From source file:org.cgiar.ccafs.marlo.action.json.global.MilestonesbyYearAction.java

@Override
public String execute() throws Exception {
    crpMilestones = new ArrayList<Map<String, Object>>();
    CrpProgramOutcome crpProgramOutcome = crpProgramManager.getCrpProgramOutcomeById(crpProgamID);
    List<CrpMilestone> milestones = crpProgramOutcome.getCrpMilestones().stream()
            .filter(c -> c.isActive() & c.getYear() >= year).collect(Collectors.toList());
    milestones.sort(Comparator.comparing(CrpMilestone::getYear));

    for (CrpMilestone crpMilestoneInfo : milestones) {
        Map<String, Object> crpMilestone = new HashMap<>();
        crpMilestone.put("id", crpMilestoneInfo.getId());
        crpMilestone.put("description", crpMilestoneInfo.getComposedName());
        crpMilestones.add(crpMilestone);
    }//from  ww  w . jav a 2  s . co  m
    return SUCCESS;
}

From source file:org.apache.nifi.toolkit.cli.impl.result.VariableRegistryResult.java

@Override
protected void writeSimpleResult(final PrintStream output) {
    final VariableRegistryDTO variableRegistryDTO = variableRegistryEntity.getVariableRegistry();
    if (variableRegistryDTO == null || variableRegistryDTO.getVariables() == null) {
        return;/*from ww  w  .  ja  va  2s.  c  om*/
    }

    final List<VariableDTO> variables = variableRegistryDTO.getVariables().stream().map(v -> v.getVariable())
            .collect(Collectors.toList());
    Collections.sort(variables, Comparator.comparing(VariableDTO::getName));

    final Table table = new Table.Builder().column("#", 3, 3, false).column("Name", 5, 40, false)
            .column("Value", 5, 40, false).build();

    for (int i = 0; i < variables.size(); i++) {
        final VariableDTO var = variables.get(i);
        table.addRow(String.valueOf(i + 1), var.getName(), var.getValue());
    }

    final TableWriter tableWriter = new DynamicTableWriter();
    tableWriter.write(table, output);
}

From source file:Utils.Distribution.ComparableDistribution.java

/**
 * Get a map from object to count for all objects
 * @return Map of counts/*from  w  w  w .j a  va2  s  .  com*/
 */
public Stream<ImmutablePair<V, Integer>> allCounts() {
    return super.allCounts().sorted(Comparator.comparing(ImmutablePair::getLeft));
}

From source file:org.cgiar.ccafs.marlo.action.json.global.CrpByUserEmailAction.java

@Override
public String execute() throws Exception {
    crps = new ArrayList<Map<String, Object>>();
    Map<String, Object> crpMap;
    List<GlobalUnit> crps = crpManager.crpUsers(userEmail);
    crps.sort(Comparator.comparing(GlobalUnit::getAcronym));

    for (GlobalUnit crp : crps) {
        try {/*from   ww  w .  ja v  a2 s.co  m*/
            crpMap = new HashMap<String, Object>();
            crpMap.put("id", crp.getId());
            crpMap.put("name", crp.getName());
            crpMap.put("acronym", crp.getAcronym());
            crpMap.put("type", crp.getGlobalUnitType().getName());
            crpMap.put("idType", crp.getGlobalUnitType().getId());

            this.crps.add(crpMap);
        } catch (Exception e) {
            logger.error("unable to add flagship to crps list", e);
            /**
             * Original code swallows the exception and didn't even log it. Now we at least log it,
             * but we need to revisit to see if we should continue processing or re-throw the exception.
             */
        }
        user = new HashMap<>();
        User usrDB = userManager.getUserByEmail(userEmail);
        if (usrDB != null) {
            user.put("name", usrDB.getComposedCompleteName());
            user.put("agree", usrDB.getAgreeTerms());
        } else {
            usrDB = userManager.getUserByUsername(userEmail);
            if (usrDB != null) {
                user.put("name", usrDB.getComposedCompleteName());
                user.put("agree", usrDB.getAgreeTerms());
            }
        }

    }
    return SUCCESS;

}

From source file:com.webtide.jetty.load.generator.jenkins.cometd.CometdProjectAction.java

public void doTrend(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
    ObjectMapper objectMapper = new ObjectMapper();

    List<BuildLoadResults> datas = new ArrayList<>();

    for (Run run : builds) {
        CometdResultBuildAction buildAction = run.getAction(CometdResultBuildAction.class);
        if (buildAction != null) {
            if (buildAction.getLoadResults() != null) {
                BuildLoadResults buildLoadResults = new BuildLoadResults(run.getId(),
                        buildAction.getLoadResults());
                datas.add(buildLoadResults);
            }/* w ww .j a v  a2 s.c o m*/
        }
    }

    // order by buildId

    Collections.sort(datas, Comparator.comparing(BuildLoadResults::getBuildId));

    //StringWriter stringWriter = new StringWriter();

    rsp.setHeader("Content-Type", "application/json");

    objectMapper.writeValue(rsp.getOutputStream(), datas);

    //rsp.getWriter().write( data );

}

From source file:Utils.Distribution.ComparableDistribution.java

/**
 * Got a map from object to proportion (of total count) for each object
 * @return Map of proportions// w  w w. j  a  v  a2 s  .c  om
 */
public Stream<ImmutablePair<V, Double>> allProportions() {
    return super.allProportions().sorted(Comparator.comparing(ImmutablePair::getLeft));
}

From source file:org.niord.web.DictionaryRestService.java

/** Returns the dictionary entries with the given name */
@GET//from   w w w .j a v a 2  s .c o m
@Path("/dictionary/{name}/entries")
@Produces("application/json;charset=UTF-8")
@GZIP
@NoCache
public List<DictionaryEntryVo> getDictionaryEntries(@PathParam("name") String name) {
    return dictionaryService.getCachedDictionary(name).getEntries().values().stream()
            .sorted(Comparator.comparing(e -> e.getKey().toLowerCase())).collect(Collectors.toList());
}

From source file:org.mortbay.jetty.load.generator.jenkins.LoadGeneratorProjectAction.java

public String getAllResponseTimeInformations() throws IOException {

    ObjectMapper objectMapper = new ObjectMapper();

    List<RunInformations> datas = new ArrayList<>();

    for (Run run : this.builds) {
        LoadGeneratorBuildAction buildAction = run.getAction(LoadGeneratorBuildAction.class);
        if (buildAction != null) {
            if (buildAction.getGlobalResponseTimeInformations() != null) {
                RunInformations runInformations = new RunInformations(run.getId(),
                        buildAction.getGlobalResponseTimeInformations(), buildAction.getTransport());
                datas.add(runInformations);
            }//from w ww. jav  a 2 s  . c  o  m
        }
    }

    // order by buildId

    Collections.sort(datas, Comparator.comparing(RunInformations::getBuildId));

    StringWriter stringWriter = new StringWriter();

    objectMapper.writeValue(stringWriter, datas);

    return stringWriter.toString();

}

From source file:org.codice.ddf.admin.application.service.migratable.JsonProfile.java

/**
 * Retrieves all exported bundles/*  www  .j a va  2s. c  o  m*/
 *
 * @return a stream of all exported bundles
 */
public Stream<JsonBundle> bundles() {
    // sort bundles based on bundle id to be sure we process them in that order
    return bundles.stream().sorted(Comparator.comparing(JsonBundle::getId));
}