2014-05-18 27 views
0
@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; 
    } 
} 

我想初始化列表<地圖<產品地圖,整數>> listProduct的SalesT類的屬性,如何初始化?我試過像這樣如何初始化Java集合列表內

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

但它不工作。

謝謝。

回答

4

如果您使用JDK 7,你可以簡單地做,

listProduct = new ArrayList<>(); 

如果JDK < 7,

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

如下您可以添加地圖,上面列表中。

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

listProduct.add(productMap); 
0

如何:

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

你可以簡單地做

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