How to use SCAN commands in Jedis

I don't like flag variables

Jedis jedis = new Jedis("localhost");

ScanParams scanParams = new ScanParams().count(10).match("*");
String cur = SCAN_POINTER_START;
do {
    ScanResult<String> scanResult = jedis.scan(cur, scanParams);

    // work with result
    scanResult.getResult().stream().forEach(System.out::println);
    cur = scanResult.getStringCursor();
} while (!cur.equals(SCAN_POINTER_START));

A suggestion to the example above. You can specify the key match within the scanParams class. See below.

ScanParams scanParams = new ScanParams();
    scanParams.match("*");

    String cursor = redis.clients.jedis.ScanParams.SCAN_POINTER_START;
    boolean cycleIsFinished = false;
    while (!cycleIsFinished) {

        ScanResult<String> scanResult = jedisRead.scan(cursor, scanParams);
        List<String> result = scanResult.getResult();

        /*
         * do what you need to do with the result
         */



        cursor = scanResult.getStringCursor();
        if (cursor.equals("0")) {
            cycleIsFinished = true;
        }
    }

In the good tradition of answering own questions, here is what I found out:

String key = "THEKEY";
ScanParams scanParams = new ScanParams().count(100);
String cur = redis.clients.jedis.ScanParams.SCAN_POINTER_START; 
boolean cycleIsFinished = false;
while(!cycleIsFinished) {
  ScanResult<Entry<String, String>> scanResult = 
        jedis.hscan(key, cur, scanParams);
  List<Entry<String, String>> result = scanResult.getResult();

  //do whatever with the key-value pairs in result

  cur = scanResult.getStringCursor();
  if (cur.equals("0")) {
    cycleIsFinished = true;
  }                 
}

The important part is that cur is a String variable and it is "0" if the scan is complete.

With the help of ScanParams I was able to define the approximate size of each chunk to get from the hash. Approximate, because the hash might change during the scan, so it may be that an element is returned twice in the loop.