从Map中设置数组列表中的值

时间:2018-05-24 18:01:59

标签: java elasticsearch arraylist

我从elasticsearch获取记录并尝试以列表形式返回。但是从sourceAsMap

添加到列表时出现错误

请在下面找到我的代码。

SearchHit[] searchHits = searchResponse.getHits().getHits();
    List<Product> productList=new ArrayList<Product>();
    for (SearchHit hit : searchHits) {
        // get each hit as a Map

        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        product=new Product();
        product.setName(sourceAsMap.get("name").toString());
        productList.add(product.setName(sourceAsMap.get("name").toString()));  // throwing error in this line

    }

    return productList;
}   

请找我的POJO课程:

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Product {

    private String id;
    private String name;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

1 个答案:

答案 0 :(得分:1)

我相信您收到1/0。您的The method add(void) is undefined for the type Product方法返回类型为Product#setName,因此您实际上是在尝试将void添加到void { {1}}。您应该List而不是Product

您的代码应如下所示:

productList.add(product)