Example usage for java.util Comparator nullsFirst

List of usage examples for java.util Comparator nullsFirst

Introduction

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

Prototype

public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) 

Source Link

Document

Returns a null-friendly comparator that considers null to be less than non-null.

Usage

From source file:Main.java

public static void main(String... args) {
    List<String> names2 = Arrays.asList("XML", null, "Java", "HTML", "CSS");
    names2.sort(Comparator.nullsFirst(String::compareTo));
    System.out.println(names2);//from w  w w .j a v a2s .  c  o m

}

From source file:Main.java

public static void main(String[] args) {
    // Sort the names based on their length, placing null first
    SortedSet<String> names = new TreeSet<>(Comparator.nullsFirst(Comparator.comparing(String::length)));
    names.add("XML");
    names.add("CSS");
    names.add("HTML");
    names.add(null); // Adds a null

    // Print the names
    names.forEach(System.out::println);

}

From source file:cn.edu.zjnu.acm.judge.contest.ContestController.java

@ResponseBody
@GetMapping(value = "standing", produces = APPLICATION_JSON_VALUE)
public UserStanding[] standing(@PathVariable("contestId") long contestId) {
    Map<String, UserStanding> hashMap = new HashMap<>(80);
    contestMapper.standing(contestId)/*from w  w w  . jav a  2s. c o m*/
            .forEach(standing -> hashMap.computeIfAbsent(standing.getUser(), UserStanding::new)
                    .add(standing.getProblem(), standing.getTime(), standing.getPenalty()));
    contestMapper.attenders(contestId).forEach(attender -> Optional.ofNullable(hashMap.get(attender.getId()))
            .ifPresent(us -> us.setNick(attender.getNick())));
    UserStanding[] standings = hashMap.values().stream().sorted(UserStanding.COMPARATOR)
            .toArray(UserStanding[]::new);
    setIndexes(standings, Comparator.nullsFirst(UserStanding.COMPARATOR), UserStanding::setIndex);
    return standings;
}

From source file:org.kitodo.production.services.data.ProcessService.java

/**
 * Filter and sort after creation date list of process properties for
 * correction and solution messages./*from w ww  .  j a  v  a  2s  .c o m*/
 *
 * @return list of ProcessProperty objects
 */
public List<PropertyDTO> getSortedCorrectionSolutionMessages(ProcessDTO process) {
    List<PropertyDTO> filteredList = filterForCorrectionSolutionMessages(process.getProperties());

    if (filteredList.size() > 1) {
        filteredList.sort(Comparator.comparing(PropertyDTO::getCreationDate,
                Comparator.nullsFirst(Comparator.naturalOrder())));
    }

    return filteredList;
}