How to load 10 records from a Java list?

Asked

Viewed 224 times

0

I’m with this Java method;

public String detalhesHistorico() {
    listaHistoricoRemessa = historicoRemessaService.listar(historicoRemessa);
    return "ajax-modal-historico";
}

Is there any java method that I can automatically load 10 records from the database?

I need to do it because there are 120 thousand more records and when I press screen it takes a lot. It would only be a palliative solution.

  • It takes too long in processing Java or in the course of consultation?

  • he performs a consultation!

  • Well, here at the company we do a technique of scroller-infinity.limiting the queries to 40 records per page, and trigging at the end of the scroller a new query by 40 records. You can implement Paging as well .

  • @Isaíasdelimacoelho how I do it?

  • Maybe what I need is a java method that is applied to the variable List

  • I’ll post the code in the answer to how to do it.

  • You managed to solve your problem ?

  • If the problem is even already query, there is not much to do besides improve the query, or adopt some caching strategy (ehcache, redis for example)

Show 3 more comments

2 answers

1

/ Java code to show the use of limit() function 
import java.util.stream.Stream; 
import java.util.ArrayList; 
import java.util.List; 
 class gfg{ 

     // Function to limit the stream upto given range, i.e, 3 
     public static Stream<String> limiting_func(Stream<String> ss, int range){ 
         return ss.limit(range); 
     } 

     // Driver code 
     public static void main(String[] args){ 

         // list to save stream of strings 
         List<String> arr = new ArrayList<>(); 

         arr.add("geeks"); 
         arr.add("for"); 
         arr.add("geeks"); 
         arr.add("computer"); 
         arr.add("science"); 

         Stream<String> str = arr.stream(); 

         // calling function to limit the stream to range 3 
         Stream<String> lm = limiting_func(str,3); 
         lm.forEach(System.out::println); 
     } 
 } 

Exit :

geeks for geeks

Link https://www.geeksforgeeks.org/stream-limit-method-in-java/

1

You will not automatically return the 10 bank records(for this, you would need to change the query), however, it is possible for you to capture these 10 records from lista complete.

Use the List<E> subList(int fromIndex,int toIndex) to capture the target list as follows:

historicoRemessaService.listar(historicoRemessa).subList(0, 10);

Note also here.

  • Sublist() will not help because the slowness is in the query. You have to change the query itself.

  • It was worth the feedback @Piovezan, before I had asked him in the comments of the question itself and the author said he performs yes the query, I imagined that the problem was in the application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.