简体   繁体   中英

how to print content of arraylist in my console

SearchRequest searchRequest =  SearchRequest.of(s -> s.index("products").query(q -> q.multiMatch(
         t -> t .fields("description","name").query(text)))
         );
       SearchResponse searchResponse =  elasticsearchClient.search(searchRequest, Product.class);
       List<Hit> hits = searchResponse.hits().hits();
       List<Product> products = new ArrayList<>();
       for(Hit object : hits){
           System.out.print(((Product) object.source()));
           System.out.println(products.toString());
           products.add((Product) object.source()); 
           
           }
   
       return products;    

i want to print "products" in my console

i had tried: System.out.println(products.toString()); but it showing me a ref number only @6ae9453[] i want to display the content inside "products"

Use Arrays.toString(products) to convert the array contents to a String

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object[])

One way of solving this is to add a toString() method to you Product class. If your Product has two attributes, title and price the toString() could be something like

 public String toString(){ 
   return title=" + getTitle() + ", price=" + getPrice()
 }  

Many IDEs has support for generating toString() methods. See fx https://www.jetbrains.com/help/idea/generate-tostring-dialog.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM