crispycat
crispycat

Reputation: 79

How to make a sorted list in java?

I have an assignment in school where one of the requirments is to have a sorted list of dogs. I have done everything in my code except this requirement since Im not sure how to do it. Down below you can see the description of this part of the assignment:

7 Command: "list dogs" This command was included in the program you submitted in the submission step four, but has somewhat changed requirements. What has changed is that the list must be sorted, the owner must be present at the printout, and that there should be an error message if there are no dogs registered. This command lists the dogs in the registry. The user must get a question about the minimum tail length and the program must print a list of all dogs at the kennel that have the same or longer tail length than this minimum stated. If you enter 0 then all dogs will be printed. If you specify 10 instead, only the dogs whose tail length is greater or equal to 10 are written. When printing, all the dog's attributes and the tail length must be printed. This includes a possible owner in this version of the program. The list should be sorted by tail length. If two dogs have the same tail length, they must be sorted by name.

Example: list dogs   Command> list dogs   Smallest tail length to display> 1   The following dogs have such a large tail:   * Fido (Dachshund, 1 year, 2 kilo, 3.70 cm tail)   * Karo (Bulldog, 8 years, 7 kilo, 5.60 cm tail, owned by Henrik)   * Milou (Terrier, 7 years, 8 kilos, 5.60 cm tail)

If there are no dogs registered, there should be no question of the minimum tail length. Instead, an error message should be printed. Make sure that the error message contains something of the words error or error, otherwise the test program cannot recognize that it is an error message.   Example: list dogs Command> list dogs Error: no dogs in register

This is my code for "list dogs"

    private void listDogs() {
    System.out.print("Smallest tail length to display: ");
    kennelList.sort(Comparator.comparing(dog -> dog.getName()));
    double tailLength = input.nextDouble();
    input.nextLine();
    int y = 0;
    while (y < kennelList.size()) {

        Dog dTail = kennelList.get(y);
        if (dTail.getTailLength() >= tailLength) {
            System.out.println(dTail);
        }
        y++;
    }
}

Upvotes: 2

Views: 950

Answers (2)

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

// define comparators
final Comparator<Dog> SORT_BY_NAME_ASC = Comparator.comparing(Dog::getName);
final Comparator<Dog> SORT_BY_TAIL_LENGTH_ASC = Comparator.comparing(Dog::getTailLength);

// define a list
List<Dog> kennelList = Collections.emptyList();

// sort list items (you can swap comparators)
kennelList.sort(SORT_BY_NAME_ASC.thenComparing(SORT_BY_TAIL_LENGTH_ASC));

// retrieve items with at least required tailLength
public static List<Dog> filterByTailLength(List<Dog> kennelList, double tailLength) {
    return kennelList.stream()
            .filter(dog -> Double.compare(dog.getTailLength(), tailLength) >= 0)
            .collect(Collectors.toList());
}

Upvotes: 1

TongChen
TongChen

Reputation: 1430

The following four steps can help you,the method use Java 8 Stream API

  • check the dog list, if is null or empty then print error message
  • sort dog list
  • filter dog list to find the target dog
  • print dog info

Assume the Dog structure like this:

class Dog {
    private String name;
    private Double tailLength;
    private String owner;
}

The code in Java 8 as follows:

public static void listDogs(List<Dog> dogs, int tailLengh) {
    if (null == dogs || dogs.size() < 1) {
        System.out.println("no register dogs");
    } else dogs.stream()
            .sorted((o1, o2) -> o1.getTailLength().compareTo(o2.getTailLength()) == 0 ? o1.getName().compareTo(o2.getName()) : o1.getTailLength().compareTo(o2.getTailLength()))
            .filter(dog -> dog.getTailLength() >= tailLengh)
            .forEach(dog -> System.out.println(dog));
}

if the sorted dog list always used,your must store the sorted dog list so don't need to sort every time.

Upvotes: 0

Related Questions