1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.collections4.properties;
19
20 import java.util.AbstractMap;
21 import java.util.AbstractMap.SimpleEntry;
22 import java.util.Enumeration;
23 import java.util.LinkedHashSet;
24 import java.util.Map;
25 import java.util.Properties;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28 import java.util.stream.Stream;
29
30 import org.apache.commons.collections4.iterators.IteratorEnumeration;
31
32
33
34
35
36
37
38
39
40
41 public class SortedProperties extends Properties {
42
43 private static final long serialVersionUID = 1L;
44
45 @Override
46 public Set<Map.Entry<Object, Object>> entrySet() {
47 final Stream<SimpleEntry<Object, Object>> stream = sortedKeys().map(k -> new AbstractMap.SimpleEntry<>(k, getProperty(k)));
48 return stream.collect(Collectors.toCollection(LinkedHashSet::new));
49 }
50
51 @Override
52 public synchronized Enumeration<Object> keys() {
53 return new IteratorEnumeration<>(sortedKeys().collect(Collectors.toList()).iterator());
54 }
55
56 private Stream<String> sortedKeys() {
57 return keySet().stream().map(Object::toString).sorted();
58 }
59 }