Home > Blockchain >  how to print content of arraylist in my console
how to print content of arraylist in my console

Time:12-19

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"

CodePudding user response:

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[])

CodePudding user response:

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 f.x. https://www.jetbrains.com/help/idea/generate-tostring-dialog.html

  • Related