Getting the unique values from an ArrayList in Java

Viewed 37

So I have a small java console program, which inputs a CSV file with vehicle details into an ArrayList. One of the columns is the type of vehicle (Sedan, coupe, etc). I want to output the unique types of vehicles entered, but I'm not sure how to do this without over complicating it. Is there any easy way to get the unique values out of my list? What is the easiest way for me to do this?

static List<Vehicle> vehicle = new ArrayList<Vehicle>();

public static void main(String[] args) {
    
    textFileReader();
}

public static void textFileReader() {
    try {
                    
        File myObj = new File("~/Fleet.csv");
        Scanner myReader = new Scanner(myObj);
        String header = myReader.nextLine();

        while (myReader.hasNextLine()) {
            String data = myReader.nextLine();
            String[] stringArrayVehicles = data.split(",");
            
            String vehicleID = stringArrayVehicles[0];
            String brand = stringArrayVehicles[1];
            String model = stringArrayVehicles[2];
            String type = stringArrayVehicles[3];
            String color = stringArrayVehicles[6];
            int yearOfManufacture = Integer.parseInt(stringArrayVehicles[4]);
            int seats = Integer.parseInt(stringArrayVehicles[5]);
            int rentPerDay = Integer.parseInt(stringArrayVehicles[7]);
            int insurancePerDay = Integer.parseInt(stringArrayVehicles[8]);
            int serviceFee = Integer.parseInt(stringArrayVehicles[9]);
            String discount = stringArrayVehicles[10];
            
            vehicle.add(new Vehicle(vehicleID, brand, model, type, color, yearOfManufacture, seats, rentPerDay, insurancePerDay, serviceFee, discount));                
                
      }
      myReader.close();
    } catch (FileNotFoundException e) {
      System.out.println("An error occurred.");
      e.printStackTrace();
    } 
}
0 Answers
Related