1

I am very new in java. I want to create a kind of complex array. I think its called list/collection/map...

My data looks like

-item
   ref:GH987
   size:22
   date:1992
-item
   ref:98KJ
   size:27
   date:2000
-item
   ref:ZXJ212
   size:24
   date:1999

I do not prefer to create an item Class and store the 3 instance in an array. I found something called Map but it really confused me and I don't understand how could I access the values after creating the data. Could you help me how to start with this ?

final Map<String, List<String>> data = new LinkedHashMap<String, List<String>>();
data.put("item", new LinkedList<String>());
3
  • 1
    I do not prefer to create an item Class and store the 3 instance in an array. Why not? What is -item in your snippet? Is that a key? If it is, how do you expect the Map to work with duplicate keys? Is it just a label for the type of information? Commented Apr 26, 2016 at 16:22
  • 2
    Read this beginnersbook.com/java-collections-tutorials the section on Maps lists the different types. A map is basically a collection of <Key, Value> pairs. If you don't care about the order of your elements you can go with a HashMap. There are examples there on how to store and get elements Commented Apr 26, 2016 at 16:32
  • 2
    You should create an item class. After that you can use either a list or map depending upon your need. Commented Apr 26, 2016 at 16:32

2 Answers 2

2

You have to create an Item class, that's the whole point of OOP!

Very minimal example:

public class Item {
    public String ref;
    public int size;
    public int date;
    public Item(String ref, int size, int date) {
        this.ref = ref;
        this.size = size;
        this.date = date;
    }
}

Then it's just a List<Item> and you can access each part with myList.get(i).ref etc:

List<Item> l = new ArrayList<>();
l.add(new Item("GH987", 22, 1992));
l.add(new Item("98KJ", 27, 2000));
...
for (Item it : l)
    System.out.println("Ref: "+item.ref+", size: "+item.size+", date: "+item.date);

Now, if you really want to use a Map to store each attribute, you have to think what would be your unique key. Let's suppose it's ref, which is a String:

Map<String,Integer> sizes = new LinkedHashMap<>(); // LinkedHashMap keeps the insert order
Map<String,Integer> dates = new LinkedHashMap<>();

sizes.put("GH987", 22);
dates.put("GH987", 1992);
sizes.put("98KJ", 27);
dates.put("98KJ", 2000);

then it's difficult to access all members as they're not bundled in a single instance:

String ref = "GH987";
System.out.println("Ref: "+ref+", size: "+sizes.get(ref)+", date: "+dates.get(ref))

Here you should realize that if a Map hasn't been updated, it will return null on the value and you'll have to handle consistency yourself. It is also a pain to create so many objects just to store single attributes, which in your case are Number subclasses (e.g. Integer) instead of native types, which are far more efficient.

So do yourself a favor, and create your Item class. Then you can use a Map to quickly access a particular item based on its key, which looks like the ref member:

myMap.put(ref, new Item(ref, size, date));
Item it = myMap.get(ref);
...
Sign up to request clarification or add additional context in comments.

Comments

-1

Yes you can choose Map with a class and having it's reference as a key for ex

Map<String,Item> map = new HashMap<>();

Assuming reference is unique. You can store values like

map.put(item.getReference(),item);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.