Home > database >  Find Longest mark
Find Longest mark

Time:12-29

I tried to return the longest "mark" (example Ferarri and Mercede in a list) but aparently I get stuck.After I run this code I also get the(Audi) in my output and i cannot figure it out. Dose anyone here can please explain me what am I doing wrong ?

This is the --Car class--

    package victor;

import java.security.PrivateKey;
import java.util.PrimitiveIterator;

public class Car {

    private String mark;
    private String model;
    private int year;
    private String color;
    public Type type;

    public Car(String mark, String model, int year, String color, Type type) {
        this.mark = mark;
        this.model = model;
        this.year = year;
        this.color = color;
        this.type = type;
    }

    public String getMark() {
        return mark;
    }

    public void setMark(String mark) {
        this.mark = mark;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "Car{"  
                "mark='"   mark   '\''  
                ", model='"   model   '\''  
                ", year="   year  
                ", color='"   color   '\''  
                ", type="   type  
                '}';
    }
}

and here is the code i figure it out till now

    package victor;

import java.util.ArrayList;
import java.util.List;

public class FindLongest {
    public static void main(String[] args) {
        Car car1 = new Car("BMW", "M5", 2020, "Black", Type.SEDAN);
        Car car2 = new Car("Audi", "SQ8", 2021, "Red", Type.COUPE);
        Car car3 = new Car("Fiat", "Abarth", 2019, "Blue", Type.SEDAN);
        Car carTest = new Car("Fiat", "Abarth", 2019, "Blue", Type.COUPE);
        Car car4 = new Car("Ferarri", "Pista", 2020, "Pink", Type.CABRIO);
        Car carTest1 = new Car("Mercede", "Abarth", 2019, "Blue", Type.COUPE);
        List<Car> carList = List.of(car1, car2, car3, carTest, car4, carTest1);
        List<Car> longestMark = findLongestMark(carList);
        System.out.println("This is longest mark : "   longestMark);
    }

    public static List<Car> findLongestMark(List<Car> carList) {
        List<Car> nfindLongCarMark = new ArrayList<>();
        Car longMark = carList.get(0);
        for (int c = 0; c < carList.size(); c  ) {
            if (carList.get(c).getMark().length() > longMark.getMark().length()) {
                longMark = carList.get(c);
                nfindLongCarMark.add(carList.get(c));
            }
        }
        return nfindLongCarMark;
    }
    }

CodePudding user response:

You should be clearing the results' list (nfindLongCarMark) each time you find a mark with longer than the current maximum length.

So (pseudocode):

add the first car to the results' list.
for each car in the list do:
    if the length of the car's mark is equal to the current maximum then:
        add the car to the results' list.
    otherwise if the length of the car's mark is greater than the current maximum then:
        clear the list first!
        then add the car to the results' list.

For example, imagine you would like to search the maximum numbers in a data set of integers like the following:

1, 2, 3, 4, 5, 5, 5, 4

... you only care about the numbers which are equal to the global maximum (ie 5 in this case). But according to your current algorithm in your question you add to the results' list the element which is greater than the current maximum (the current maximum in your algorithm is the maximum of all the elements that the loop has checked up to that point).

So if you run your algorithm with the above input, you will get something like: 2, 3, 4, 5, 5, 5. And that's because when the loop tests for the second element (2) it will be greater than the current value of maximum (which was initialized with a 1), so 2 will be added to the list. After that, 3 will be greater than 2 so it will too be added to the results' list, and so on.

CodePudding user response:

The easiest way is, you can introduce a method in class to return length of string and use below stream function.

// Method to return length of string
public int lengthOfMark() {
            return this.mark != null ? this.mark.length() : 0;
        }

// find longest length object.
final Car car = carList.stream()
                .max(Comparator.comparingInt(Car::lengthOfMark))
                .orElseThrow(NoSuchElementException::new);
        System.out.println("This is longest mark : "   car);

Hoping this will help you.

  •  Tags:  
  • java
  • Related