0

I am having confusing over how to choose my data structure. Lets say i have following data Product,Price,Company,Total available.. which i get from db. Now i want to represent this in lets say excel or csv in same order as i get from db company wise. So i choose below data structure.

Map<String, TreeMap<Integer, TreeMap<String, String>>> .

First String represents Company Integer represents position of record in db so that i can display in same order. The TreeMap contains other values.

Can I choose any better data structure for this requirement.

2
  • why do you stick to use data structure ? there are so many other ways to store this kind of information. Commented Oct 7, 2014 at 14:16
  • 1
    "position of record in db" - There's nothing like this. If you don't use ORDER BY, than you get rows in an arbitrary order. It usually looks as if they were order by the primary key or insertion time, but this can change anytime (and actually will, a few deletions may suffice). If you used ORDER BY, than you don't need to store the Integer as you may sort and number your rows anytime. Commented Oct 7, 2014 at 16:04

3 Answers 3

4

Yes, absolutely.

A better solution would be object-oriented:

public class Product {
    private String name;
    private String company;
    private Money total;
    private boolean available;
    // Add necessary methods.
}

The data structure would be a List<Product>.

Your way is too primitive.

Sign up to request clarification or add additional context in comments.

Comments

1

Traditional data structures follow a structured programming paradigm. Object oriented programming has roots in structured programming, but adds in the concept of behavior locality. In short, the data is not just centralized, but the behavior (methods) that go with the data is centralized.

This allows data hiding (useful for maintenance, as the right data format tends to change over time), and opens the door to other more advance behaviors (polymorphisim is possible because the behavior is localized). However, it doesn't do much for a pure play data structure approach. The closest we have to old school data structures are objects which represent them.

When selecting a data structure, if you really don't have an idea of what is important, you really don't have the criteria which would allow you to select one data structure over another. Sure, you could just always use HashMap and HashSet and that would be fine a lot of the time; but, there are trivial examples where those selections are probably the worst choices. In short, you need to know the access patterns to make the right choice.

Comments

0

As duffymo suggests, you should consider an oop approach. Consider using something like the following example:

import java.util.ArrayList;

public class Product {

    private String name;
    private double price;
    private String company;
    private int total;
    private boolean available;

    public Product(String name, double price, String company, int total,
            boolean available) {
        super();
        this.name = name;
        this.price = price;
        this.company = company;
        this.total = total;
        this.available = available;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public boolean isAvailable() {
        return available;
    }

    public void setAvailable(boolean available) {
        this.available = available;
    }

    @Override
    public String toString() {
        return "Product [name=" + name + ", price=" + price + ", company="
                + company + ", total=" + total + ", available=" + available
                + "]";
    }

    public static void main(String[] args) {
        ArrayList<Product> products = new ArrayList<Product>();

        Product product1 = new Product("PlayStation 4", 300, "Sony", 10, true);
        Product product2 = new Product("XBOX One", 400, "Microsoft", 0, false);
        Product product3 = new Product("WiiU", 250, "Nintendo", 5, true);

        products.add(product1);
        products.add(product2);
        products.add(product3);

        System.out.println("-- Products --");
        for (Product product : products) {
            System.out.println(product.toString());
        }
    }
}

It will produce the following output:

-- Products --
Product [name=PlayStation 4, price=300.0, company=Sony, total=10, available=true]
Product [name=XBOX One, price=400.0, company=Microsoft, total=0, available=false]
Product [name=WiiU, price=250.0, company=Nintendo, total=5, available=true]

As you can see, you will be able to easily manage your list of items.

Hope it helps.

Clemencio Morales Lucas.

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.