Justin Dittburner
Justin Dittburner

Reputation: 17

How to find an object without an arrayList

I'm currently working on a program where i have a large amount of objects (all the elements from the periodic table) and it would be a huge pain to have to create a List for over 100 objects. Is there any way i can declare and instantiate all the objects one time and have a method read the class they exist in? For example if a user inputs "He" i can find the corresponding object that holds the elementSymbol "He"? I would have to be able to check each object using the getSymbol method which returns its symbol and then compare it to the input. After that i plan on using other fields assigned to the object like it's molar mass for calculations but that isn't going to be an issue. If there is another way to effectively do this it would be greatly appreciated.

Upvotes: 0

Views: 72

Answers (4)

ManoDestra
ManoDestra

Reputation: 6473

Not sure why you're averse to using an ArrayList, it's a perfectly valid solution to your requirement. I think use of an Enum is the better solution given the constant nature of the periodic table, but I'm adding this List answer, utilizing the streams API for completeness.

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

public class Test {
    public static void main(String[] args) {
        Test test = new Test();
        test.run();
    }

    private void run() {
        List<Element> elements = new ArrayList<>();
        Element oxygen = new Element("O", "Oxygen");
        Element carbon = new Element("C", "Carbon");
        Element hydrogen = new Element("H", "Hydrogen");
        Element helium = new Element("He", "Helium");
        elements.add(oxygen);
        elements.add(carbon);
        elements.add(hydrogen);
        elements.add(helium);

        System.out.println("All Elements:");
        for (Element element : elements) {
            System.out.println("\t" + element);
        }

        System.out.println();

        Element oxy = elements.stream().filter(e -> e.getId().equals("He")).findFirst().get();
        System.out.println("Specific Element: " + oxy);
    }

    private class Element {
        private final String id;
        private final String name;

        public Element(String id, String name) {
            this.id = id;
            this.name = name;
        }

        public String getId() {
            return this.id;
        }

        public String getName() {
            return this.name;
        }

        @Override
        public String toString() {
            return this.getName() + " [" + this.getId() + "]";
        }
    }
}

Upvotes: 0

Vovka
Vovka

Reputation: 599

As you have constant number of objects, it is better to manage them with enum:

public enum Elements {
    ag(10), //10 here is the molarMass constructor parameter
    he(20);

    //constructor if needed
    Elements(int molarMass) {
        this.molarMass = molarMass;
    }

    //extra fields
    final private int molarMass;
}

get:

Elements.valueOf("he")

Upvotes: 3

Priyamal
Priyamal

Reputation: 2969

you can use a hashmap for this.

HashMap<String,PeriodObject> peridicTable = new   HashMap<String,PeriodObject>();
periodictable.add("he",object);
periodicTableobject = periodictable.get("he");

Upvotes: 1

Nicolas Filotto
Nicolas Filotto

Reputation: 44965

You should store everything into a Map with the elementSymbol as key and your object as value, then you will be able to access to your object by calling map.get(elementSymbol).

More details about the class Map here

Upvotes: 3

Related Questions