2
@Entity

public class SalesT {

    @Id
    @GeneratedValue
    private int id;

    @OneToOne
    private Customer customer;

    @OneToMany(targetEntity = Product.class)
    private List< Map<Product,Integer> > listProduct;// I want to intialize this

    public SalesT() {
    }

    public SalesT(Customer customer, List<Map<Product, Integer>> product) {
        this.customer = customer;
        this.listProduct = product;
    }
}

I want to Initialize List< Map < Product,Integer>> listProduct attribute of the SalesT Class, How do I initialize it?. I tried Like this

List<Map <Product, Integer>> listProduct = new ArrayList <HashMap <Product, Integer>>();

but it is not working.

Thank you.

3 Answers 3

9

If you're using JDK 7, you can simply do,

listProduct = new ArrayList<>();

If JDK < 7,

listProduct = new ArrayList<Map<Product, Integer>>();

You can add maps to above list as follows.

Map<Product,Integer> productMap = new HashMap<Product, Integer>();
productMap.put(new Product(), 1);
productMap.put(new Product(), 2);

listProduct.add(productMap);
Sign up to request clarification or add additional context in comments.

Comments

1

How about:

private List< Map<Product,Integer> > listProduct
            = new ArrayList<Map<Product,Integer>>();

Comments

0

You can simply do

List< Map<Product,Integer> > listProduct = new ArrayList<Map<Product,Integer>>();

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.