1919import java .nio .charset .StandardCharsets ;
2020import java .util .Collection ;
2121import java .util .Collections ;
22+ import java .util .Comparator ;
2223import java .util .LinkedHashMap ;
2324import java .util .LinkedHashSet ;
2425import java .util .Map ;
2526import java .util .Map .Entry ;
27+ import java .util .NavigableMap ;
2628import java .util .Set ;
29+ import java .util .TreeMap ;
2730import java .util .regex .Matcher ;
2831import java .util .regex .Pattern ;
2932
3033import org .springframework .lang .Nullable ;
3134import org .springframework .util .Assert ;
3235import org .springframework .util .StringUtils ;
36+ import org .springframework .util .comparator .NullSafeComparator ;
3337
3438/**
3539 * Bucket is the data bag for Redis hash structures to be used with {@link RedisData}.
3640 *
3741 * @author Christoph Strobl
3842 * @author Mark Paluch
43+ * @author Stefan Berger
3944 * @since 1.7
4045 */
4146public class Bucket {
@@ -45,19 +50,22 @@ public class Bucket {
4550 */
4651 public static final Charset CHARSET = StandardCharsets .UTF_8 ;
4752
48- private final Map <String , byte []> data ;
53+ /**
54+ * The Redis data as {@link Map} sorted by the keys.
55+ */
56+ private final NavigableMap <String , byte []> data = new TreeMap <>(
57+ new NullSafeComparator <>(Comparator .<String > naturalOrder (), true ));
4958
5059 /**
5160 * Creates new empty bucket
5261 */
5362 public Bucket () {
54- data = new LinkedHashMap <>();
63+
5564 }
5665
5766 Bucket (Map <String , byte []> data ) {
5867
5968 Assert .notNull (data , "Initial data must not be null!" );
60- this .data = new LinkedHashMap <>(data .size ());
6169 this .data .putAll (data );
6270 }
6371
@@ -151,14 +159,7 @@ public Map<String, byte[]> asMap() {
151159 */
152160 public Bucket extract (String prefix ) {
153161
154- Bucket partial = new Bucket ();
155- for (Map .Entry <String , byte []> entry : data .entrySet ()) {
156- if (entry .getKey ().startsWith (prefix )) {
157- partial .put (entry .getKey (), entry .getValue ());
158- }
159- }
160-
161- return partial ;
162+ return new Bucket (data .subMap (prefix , prefix + Character .MAX_VALUE ));
162163 }
163164
164165 /**
0 commit comments