Skip to content
Permalink
Browse files
Reimplement SortedProperties#keys() a la Java 8.
Use final.
  • Loading branch information
garydgregory committed Sep 28, 2021
1 parent 78c4b0f commit a18086f23be65370e36c90cc5dd213b9ec0b1b3e
Showing 1 changed file with 5 additions and 10 deletions.
@@ -26,6 +26,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections4.iterators.IteratorEnumeration;

@@ -43,21 +44,15 @@ public class SortedProperties extends Properties {

@Override
public synchronized Enumeration<Object> keys() {
final Set<Object> keySet = keySet();
final List<String> keys = new ArrayList<>(keySet.size());
for (final Object key : keySet) {
keys.add(key.toString());
}
Collections.sort(keys);
return new IteratorEnumeration<>(keys.iterator());
return new IteratorEnumeration<>(keySet().stream().map(Object::toString).sorted().collect(Collectors.toList()).iterator());
}

@Override
public Set<Map.Entry<Object, Object>> entrySet() {
Enumeration<Object> keys = keys();
Set<Map.Entry<Object, Object>> entrySet = new LinkedHashSet<>();
final Enumeration<Object> keys = keys();
final Set<Map.Entry<Object, Object>> entrySet = new LinkedHashSet<>();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
final Object key = keys.nextElement();
entrySet.add(new AbstractMap.SimpleEntry<>(key, getProperty((String) key)));
}
return entrySet;

0 comments on commit a18086f

Please sign in to comment.