Skip to content

Commit

Permalink
feat: adds method for random sublist (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
vilkg authored Mar 22, 2021
1 parent 1585471 commit c06de3c
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/main/java/org/hisp/dhis/utils/DataRandomizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -149,6 +145,16 @@ public static <T> T randomElementFromList( List<T> list )
Random rand = new Random();
return list.get( rand.nextInt( list.size() ) );
}

public static <T> List<T> randomElementsFromList( List<T> list, int elements )
{
Collections.shuffle( list );
if ( elements > list.size() )
{
elements = list.size();
}
return list.subList( 0, elements );
}

/**
* Generates random boolean value
Expand Down

0 comments on commit c06de3c

Please sign in to comment.