Hi guys, got a few questions

1. i have initialised my collection with a few objects, how do i set the make etc when i initialise them (i have the set methods). So in the line below where would i put setMake()? would it work with a . accesor after LargePlane?

planesAvailable.put("502146", new LargePlane());

2. Im also having a problem with when i create and put objects in the collection i use the key (above 502146) which is the registration of the object how can i set that to the registration within the collection? (when i print out the collection the registration is null).

3. Also I have overided toString(), i have a bunch of types (eg. LargePlane, SmallPlane) that could be in this collection, how do i print like LargePlane? I've tried like typeOf() and stuff but it doesn't work, do i need to import something?

Thanks

Dani AI

Generated

Short practical summary: fields (make, registration) must be set on the Plane instance itself — Java will not copy a Map key into an object's field automatically. Common approaches are (1) add a constructor that accepts registration/make, (2) assign to a local variable, call setters, then put that variable into the map, or (3) make setters fluent (return this) so they can be chained at creation. For printing the concrete type, use the runtime class name or include it in toString().

Constructor example:

public class LargePlane {
    private String registration;
    private String make;

    public LargePlane(String registration, String make) {
        this.registration = registration;
        this.make = make;
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + "[reg=" + registration + ", make=" + make + "]";
    }
}

Fluent-setter and helper-put patterns:

public LargePlane setRegistration(String reg) {
    this.registration = reg;
    return this;
}

public static void putPlane(Map<String,Plane> map, String reg, Plane p) {
    p.setRegistration(reg);
    map.put(reg, p);
}

Printing examples and notes: use plane.getClass().getSimpleName() to obtain "LargePlane" at runtime, or override toString() (as shown) to include class name plus fields; iterate entrySet() to print key -> value pairs so the map key is visible alongside the object's state. Avoid relying on void setters for inline chaining (they break expressions); consider fluent setters if inline initialization is desired. Double-brace initialization would work syntactically but creates an anonymous subclass and is not recommended for production code.

Context tied to posts: observed the registration field staying null — that happens because the map key is separate from the object's internal field and must be assigned explicitly. ’s pointer about checking keys is useful for validation, but keep the object’s state and the map key consistent by setting the registration inside the object (constructor, fluent setter, or helper method) before or when placing it into the map.

Recommended Answers

All 3 Replies

Anyone?

Oh ps ignore the first one, i was just curious if i could do it implicitly in that line using the key.

Im also having a problem with when i create and put objects in the collection i use the key (above 502146) which is the registration of the object how can i set that to the registration within the collection? (when i print out the collection the registration is null).

You could use .keySet().toArray() and print out everything in the array it returns. You could also use .containsKey("registrationNumber") to see if the registration number is in the "Collection" (actually since you're using put to map key->value it is probably more specifically some kind of class that extends Map).

Also I have overided toString(), i have a bunch of types (eg. LargePlane, SmallPlane) that could be in this collection, how do i print like LargePlane? I've tried like typeOf() and stuff but it doesn't work, do i need to import something?

System.out.println(yourObject.toString()); I think you already know that, but I don't understand your question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.