Hi,
Could somebody help me with my small program and tell me how to return key and value from my map. Namely I have so far the following classes that will print for me car number, but how can I print car number and car type from my map of cars?
import java.util.*; public class Car { private static Map<Integer, Car> cars = new TreeMap<Integer, Car>(); private int carNumber; private String carType; private Car(int carNumber, String carType) { this.carNumber = carNumber; this.carType = carType; } public static Car add(int carNumber, String carType) { Car retValue = cars.get(carNumber); if(retValue == null) { cars.put(carNumber, new Car(carNumber, carType)); } return retValue; } public static Collection<Car> getCollection(){ return cars.values(); } public static Car get(int carNumber) { return cars.get(carNumber); } public static Car get(String carType) { return cars.get(carType); } public int getCarNumber() { return carNumber; } public String getCarType() { return carType; } }
public class Main { public static void main (String[] args) { for(int i=0; i<5; i++) { Car.add(i, "Sport"); } System.out.println(car.getCollection()); } //output: [1,2,3,4] }