List of usage examples for java.util Map entrySet
Set<Map.Entry<K, V>> entrySet();
From source file:Main.java
public static void trimMap(Map map, int numberOfElements) { if (map.size() < numberOfElements) { return;/*from www . j ava 2s .co m*/ } numberOfElements = map.size() - numberOfElements; Iterator it = map.entrySet().iterator(); int counter = 0; while (it.hasNext()) { if (counter <= numberOfElements) { it.next(); counter++; } it.remove(); } }
From source file:com.github.blindpirate.gogradle.util.StringUtils.java
public static String formatEnv(Map<String, String> env) { return String.join("\n", env.entrySet().stream().map(entry -> " " + entry.getKey() + "=" + entry.getValue()) .collect(toList()));/* www . j av a2 s.co m*/ }
From source file:Main.java
public static <K, V> Map<K, V> valueSortedMap(Map<K, V> map, Comparator<Entry<K, V>> comparator) { Set<Entry<K, V>> valueSortedEntries = new TreeSet<Entry<K, V>>(comparator); for (Entry<K, V> entry : map.entrySet()) { valueSortedEntries.add(entry);/*from ww w . j a v a 2s . c o m*/ } Map<K, V> sortedMap = new LinkedHashMap<K, V>(map.size()); for (Entry<K, V> entry : valueSortedEntries) { sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; }
From source file:com.collective.celos.ci.testing.fixtures.compare.CompareHelper.java
public static <T> FixObjectCompareResult compareEntityNumber(TestRun testRun, FixObjectCreator actualDataCreator, FixObjectCreator expectedDataCreator, Map<T, Integer> expectedRes, Map<T, Integer> actualRes) throws Exception { Set<Map.Entry<T, Integer>> expectedDiffers = Sets.difference(expectedRes.entrySet(), actualRes.entrySet()); Set<Map.Entry<T, Integer>> actualDiffers = Sets.difference(actualRes.entrySet(), expectedRes.entrySet()); if (actualDiffers.size() + expectedDiffers.size() > 0) { String actualDir = getDifference(actualDiffers); String expectedDiff = getDifference(expectedDiffers); return FixObjectCompareResult.failed( "Diff:\n" + "Actual [" + actualDataCreator.getDescription(testRun) + "]:\n" + actualDir + "\n" + "Expected [" + expectedDataCreator.getDescription(testRun) + "]:\n" + expectedDiff); }//from www.j a va 2 s . co m return FixObjectCompareResult.SUCCESS; }
From source file:Main.java
public static List<NameValuePair> convertMap2List(Map<String, String> params) { if (params.isEmpty()) return null; List<NameValuePair> paramList = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> entry : params.entrySet()) { paramList.add(new BasicNameValuePair(entry.getKey().trim(), entry.getValue().trim())); }/*from w w w. j a v a 2s . c o m*/ return paramList; }
From source file:Main.java
@SuppressWarnings("unchecked") static boolean loadSharedPreferencesFromFile(Context context, File src) { boolean res = false; ObjectInputStream input = null; try {/*from w w w.j a va 2 s. c o m*/ GZIPInputStream inputGZIP = new GZIPInputStream(new FileInputStream(src)); input = new ObjectInputStream(inputGZIP); Editor prefEdit = PreferenceManager.getDefaultSharedPreferences(context).edit(); prefEdit.clear(); Map<String, Object> entries = (Map<String, Object>) input.readObject(); for (Entry<String, ?> entry : entries.entrySet()) { Object v = entry.getValue(); String key = entry.getKey(); if (v instanceof Boolean) prefEdit.putBoolean(key, ((Boolean) v).booleanValue()); else if (v instanceof Float) prefEdit.putFloat(key, ((Float) v).floatValue()); else if (v instanceof Integer) prefEdit.putInt(key, ((Integer) v).intValue()); else if (v instanceof Long) prefEdit.putLong(key, ((Long) v).longValue()); else if (v instanceof String) prefEdit.putString(key, ((String) v)); } prefEdit.commit(); res = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (input != null) { input.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return res; }
From source file:com.squarespace.template.JsonLoader.java
public static Map<String, JsonNode> loadJson(Class<?> relativeCls, String path) { try {//from w w w . j a v a 2s . c om String source = GeneralUtils.loadResource(relativeCls, path); Map<String, String> sections = TestCaseParser.parseSections(source); Map<String, JsonNode> result = new HashMap<>(); for (Map.Entry<String, String> entry : sections.entrySet()) { result.put(entry.getKey(), JsonUtils.decode(entry.getValue())); } return result; } catch (CodeException e) { throw new AssertionError("Failed to load JSON from resource '" + path + "'", e); } }
From source file:com.incapture.rapgen.output.OutputWriter.java
/** * Write templates to files. If you have files composed of multiple templates, look at {@link #writeMultiPartTemplates(String, Map)} * * @param rootFolder//w w w . java 2 s . c om * @param pathToTemplate */ public static void writeTemplates(String rootFolder, Map<String, StringTemplate> pathToTemplate) { for (Map.Entry<String, StringTemplate> entry : pathToTemplate.entrySet()) { String filename = entry.getKey(); File file = new File(rootFolder, filename); file.getParentFile().mkdirs(); StringTemplate template = entry.getValue(); try { FileUtils.write(file, template.toString()); } catch (IOException e) { System.err.println("Error writing template: " + ExceptionToString.format(e)); } } }
From source file:com.streamsets.pipeline.lib.jdbc.JdbcMetastoreUtil.java
public static LinkedHashMap<String, JdbcTypeInfo> convertRecordToJdbcType(Record record, String precisionAttribute, String scaleAttribute, JdbcSchemaWriter schemaWriter) throws OnRecordErrorException, JdbcStageCheckedException { if (!record.get().getType().isOneOf(Field.Type.MAP, Field.Type.LIST_MAP)) { throw new OnRecordErrorException(record, JdbcErrors.JDBC_300, record.getHeader().getSourceId(), record.get().getType().toString()); }//from ww w. ja v a 2 s . co m LinkedHashMap<String, JdbcTypeInfo> columns = new LinkedHashMap<>(); Map<String, Field> list = record.get().getValueAsMap(); for (Map.Entry<String, Field> pair : list.entrySet()) { if (StringUtils.isEmpty(pair.getKey())) { throw new OnRecordErrorException(record, JdbcErrors.JDBC_301, "Field name is empty"); } String fieldPath = pair.getKey(); Field currField = pair.getValue(); // Some types requires special checks or alterations JdbcType jdbcType = JdbcType.getJdbcTypeforFieldType(currField.getType()); JdbcTypeInfo jdbcTypeInfo; if (jdbcType == JdbcType.DECIMAL) { int precision = resolveScaleOrPrecisionExpression("precision", currField, precisionAttribute, fieldPath); int scale = resolveScaleOrPrecisionExpression("scale", currField, scaleAttribute, fieldPath); schemaWriter.validateScaleAndPrecision(pair.getKey(), currField, precision, scale); jdbcTypeInfo = jdbcType.getSupport().generateJdbcTypeInfoFromRecordField(currField, schemaWriter, precision, scale); } else { jdbcTypeInfo = jdbcType.getSupport().generateJdbcTypeInfoFromRecordField(currField, schemaWriter); } columns.put(pair.getKey().toLowerCase(), jdbcTypeInfo); } return columns; }
From source file:com.withub.common.SearchFilter.java
/** * searchParamskey?OPERATOR_FIELDNAME//from w w w . j a v a 2 s.co m */ public static Map<String, SearchFilter> parse(Map<String, Object> searchParams) { Map<String, SearchFilter> filters = Maps.newHashMap(); for (Map.Entry<String, Object> entry : searchParams.entrySet()) { // String key = entry.getKey(); Object value = entry.getValue(); if (value == null || StringUtils.isBlank(value.toString())) { continue; } // operatorfiledAttribute String[] names = StringUtils.split(key, "_"); if (names.length != 2) { throw new IllegalArgumentException(key + " is not a valid search filter name"); } String filedName = names[1]; Operator operator = Operator.valueOf(names[0]); // searchFilter SearchFilter filter = new SearchFilter(filedName, operator, value); filters.put(key, filter); } return filters; }