Here you can find the source of extractFromPropertiesAsList(String prefix, Properties properties)
Parameter | Description |
---|---|
prefix | for selecting the properties from which the list should be extracted |
properties | properties from which to extract from |
public static List<String> extractFromPropertiesAsList(String prefix, Properties properties)
//package com.java2s; //License from project: Apache License import java.util.*; public class Main { /**/*from w ww . jav a 2 s . c om*/ * Extract from given properties a list of string values. The prefix is used to determine the subset of the * given properties from which the list should be extracted, the rest is used as a numeric index. If the rest * is not numeric, the order is not determined (all those props are appended to the end of the list) * * @param prefix for selecting the properties from which the list should be extracted * @param properties properties from which to extract from * @return parsed list or null if no element with prefixes exists */ public static List<String> extractFromPropertiesAsList(String prefix, Properties properties) { TreeMap<Integer, String> orderedMap = new TreeMap<>(); List<String> rest = new ArrayList<>(); Enumeration names = properties.propertyNames(); String prefixP = prefix + "."; while (names.hasMoreElements()) { String key = (String) names.nextElement(); if (propMatchesPrefix(prefixP, key)) { String index = key.substring(prefixP.length()); String value = properties.getProperty(key); try { Integer nrIndex = Integer.parseInt(index); orderedMap.put(nrIndex, value); } catch (NumberFormatException exp) { rest.add(value); } } } List<String> ret = new ArrayList<>(orderedMap.values()); ret.addAll(rest); return ret.size() > 0 ? ret : null; } private static boolean propMatchesPrefix(String prefix, String key) { return key.startsWith(prefix) && key.length() >= prefix.length(); } }