List of usage examples for java.util Map containsKey
boolean containsKey(Object key);
From source file:name.abhijitsarkar.algorithms.core.Sorter.java
public static Integer[] countingSort(final int[] arr) { final Map<Integer, Pair> countMap = new HashMap<>(); for (final int i : arr) { Pair p = new Pair(i, 1); if (countMap.containsKey(i)) { p.value += countMap.get(i).value; }/*ww w .j a va 2s .c om*/ countMap.put(i, p); } BinarySearchTree<Pair> bst = new BinarySearchTree<>(countMap.values()); final List<Pair> sortedPairs = BinaryTreeWalker.recursiveInorder(bst.root()); return CollectionUtils.collect(sortedPairs, TransformerUtils.<Pair, Integer>invokerTransformer("getKey")) .toArray(new Integer[] {}); }
From source file:lu.lippmann.cdb.weka.SilhouetteUtil.java
/** * res.get(clusterIdx) = silhouettes values map for instances * @param result//from w w w .j av a 2 s . co m * @return */ protected static Map<Integer, List<Double>> computeSilhouette(final Instances ds, final WekaClusteringResult result) { final List<Instances> clusters = result.getClustersList(); final List<Double> res = new ArrayList<Double>(); final int cs = clusters.size(); final double[] ass = result.getAss(); final EuclideanDistance euclidian = new EuclideanDistance(ds); //euclidian.setDontNormalize(true); ds.setClassIndex(-1); //Loop through every cluster final int dss = ds.numInstances(); for (int i = 0; i < dss; i++) { final int clusterIndex = (int) ass[i]; double distAo = -1; double distBo = Double.MAX_VALUE; boolean distanceIsZero = false; for (int j = 0; j < cs; j++) { //Compute distCo, for all C double distCo = 0; Instances cluster = result.getClustersList().get(j); final int cls = cluster.numInstances(); for (int k = 0; k < cls; k++) { distCo += euclidian.distance(cluster.instance(k), ds.instance(i)); } distCo /= cls; if (j != clusterIndex && distCo < distBo) { distBo = distCo; } if (j == clusterIndex) { if (distCo == 0) { distanceIsZero = true; } else { distAo = distCo; } } } if (distanceIsZero) { res.add(0d); } else { res.add((distBo - distAo) / Math.max(distAo, distBo)); } } final Map<Integer, List<Double>> sils = new HashMap<Integer, List<Double>>(); for (int i = 0; i < dss; i++) { final int clusterIndex = (int) ass[i]; if (!sils.containsKey(clusterIndex)) { sils.put(clusterIndex, new ArrayList<Double>()); } sils.get(clusterIndex).add(res.get(i)); } for (final List<Double> list : sils.values()) { Collections.sort(list, Collections.reverseOrder()); } return sils; }
From source file:com.google.enterprise.adaptor.experimental.Sim.java
static String extractFeedFromMultipartPost(InputStream in, long len, String contentType) throws NoXmlFound { HttpExchangeUploadInfo uploadInfo = new HttpExchangeUploadInfo(in, len, contentType); try {//from w w w . ja v a2 s . c o m Map<String, byte[]> parts = splitMultipartRequest(uploadInfo); if (!parts.containsKey("data")) { throw new NoXmlFound(); } return new String(parts.get("data"), UTF8); } catch (IOException ie) { throw new NoXmlFound(); } }
From source file:com.rmn.qa.AutomationRunRequest.java
/** * Generates a AutomationRunRequest object from the capabilities passed in * @param capabilities/*from ww w . java 2s . c o m*/ * @return */ public static AutomationRunRequest requestFromCapabilities(Map<String, Object> capabilities) { String capabilityBrowser = (String) capabilities.get(CapabilityType.BROWSER_NAME); String capabilityBrowserVersion = null; if (capabilities.containsKey(CapabilityType.VERSION)) { capabilityBrowserVersion = (String) capabilities.get(CapabilityType.VERSION); } String capabilityOs = (String) capabilities.get(CapabilityType.PLATFORM); return new AutomationRunRequest(null, null, capabilityBrowser, capabilityBrowserVersion, capabilityOs); }
From source file:com.symbian.driver.utils.TestDriverConfigurator.java
/** * launchConfig2TDConfig : Fills TDConfig from the launch configuration * /*w ww. j a v a 2 s.com*/ * @param ILaunchConfiguration * aLaunchCongiguration * @return void * @throws ParseException : * when updating the TDConfig fails * @throws CoreException : * when reading the launch configuration fails * */ private static void convertLaunchConfig2TDConfig(ILaunchConfiguration aConfig) throws ParseException, CoreException { TDConfig lConfig = TDConfig.getInstance(); Map lAttributes = aConfig.getAttributes(); // EPOC ROOT if (lAttributes.containsKey(DriverLaunchConstants.EPOC_ROOT)) { lConfig.setPreferenceFile(TDConfig.EPOC_ROOT, new File((String) lAttributes.get(DriverLaunchConstants.EPOC_ROOT))); } // Entry point address : DRIVER + SUITE String lDriver = null; File lDriverFile = null; URI lSuite = null; if (lAttributes.containsKey(DriverLaunchConstants.DRIVER)) { lDriver = (String) lAttributes.get(DriverLaunchConstants.DRIVER); lDriverFile = new File(lDriver); } String lSuiteString = null; if (lAttributes.containsKey(DriverLaunchConstants.ENTRY_POINT_ADDRESS)) { lSuiteString = (String) lAttributes.get(DriverLaunchConstants.ENTRY_POINT_ADDRESS); } if (lDriver != null) { // set the xml root lConfig.setPreferenceFile(TDConfig.XML_ROOT, new File(lDriver).getParentFile()); lSuite = URI.createFileURI(lDriverFile.getAbsolutePath()); if (lSuiteString != null) { lSuite = lSuite.appendFragment(lSuiteString); } lConfig.setPreferenceURI(TDConfig.ENTRY_POINT_ADDRESS, lSuite); } // Platform if (lAttributes.containsKey(DriverLaunchConstants.PLATFORM)) { lConfig.setPreference(TDConfig.PLATFORM, (String) lAttributes.get(DriverLaunchConstants.PLATFORM)); } // Variant if (lAttributes.containsKey(DriverLaunchConstants.VARIANT)) { lConfig.setPreference(TDConfig.VARIANT, (String) lAttributes.get(DriverLaunchConstants.VARIANT)); } // Tefdeps if (lAttributes.containsKey(DriverLaunchConstants.TEF_DEPS)) { lConfig.setPreferenceBoolean(TDConfig.TEST_EXECUTE, (Boolean) lAttributes.get(DriverLaunchConstants.TEF_DEPS)); } // Clean if (lAttributes.containsKey(DriverLaunchConstants.CLEAN)) { lConfig.setPreferenceBoolean(TDConfig.CLEAN, (Boolean) lAttributes.get(DriverLaunchConstants.CLEAN)); } // Bldmake if (lAttributes.containsKey(DriverLaunchConstants.BLDMAKE)) { lConfig.setPreferenceBoolean(TDConfig.BLDMAKE, (Boolean) lAttributes.get(DriverLaunchConstants.BLDMAKE)); } // source root if (lAttributes.containsKey(DriverLaunchConstants.SOURCE_ROOT)) { lConfig.setPreferenceFile(TDConfig.SOURCE_ROOT, new File((String) lAttributes.get(DriverLaunchConstants.SOURCE_ROOT))); } // repository root if (lAttributes.containsKey(DriverLaunchConstants.REPOSITORY_ROOT)) { lConfig.setPreferenceFile(TDConfig.REPOSITORY_ROOT, new File((String) lAttributes.get(DriverLaunchConstants.REPOSITORY_ROOT))); } // result root if (lAttributes.containsKey(DriverLaunchConstants.RESULT_ROOT)) { lConfig.setPreferenceFile(TDConfig.RESULT_ROOT, new File((String) lAttributes.get(DriverLaunchConstants.RESULT_ROOT))); } // sysbin if (lAttributes.containsKey(DriverLaunchConstants.SYS_BIN)) { lConfig.setPreferenceBoolean(TDConfig.SYS_BIN, (Boolean) lAttributes.get(DriverLaunchConstants.SYS_BIN)); } // rdebug if (lAttributes.containsKey(DriverLaunchConstants.RDEBUG)) { lConfig.setPreference(TDConfig.RDEBUG, (String) lAttributes.get(DriverLaunchConstants.RDEBUG)); } // transport if (!Epoc.isTargetEmulator(lConfig.getPreference(TDConfig.PLATFORM))) { // set hardware transport if (lAttributes.containsKey(DriverLaunchConstants.TRANSPORT)) { String lTransport = ((String) lAttributes.get(DriverLaunchConstants.TRANSPORT)).toLowerCase(); if (lTransport.equals(iEmptyString)) { // default serial lTransport = "serial"; } String lIPaddress = iEmptyString; if (lAttributes.containsKey(DriverLaunchConstants.IP_ADDRESS)) { lIPaddress = (String) lAttributes.get(DriverLaunchConstants.IP_ADDRESS); } String lPort = iEmptyString; if (lAttributes.containsKey(DriverLaunchConstants.PORT)) { lPort = (String) lAttributes.get(DriverLaunchConstants.PORT); } if (lTransport.equals("serial") || lTransport.equals("usb") || lTransport.equals("bt")) { if (lPort.equals(iEmptyString)) { lPort = "1"; } lTransport = lTransport + lPort; } else if (lTransport.equals("tcp")) { if (!lIPaddress.equals(iEmptyString)) { lTransport = "tcp:" + lIPaddress; if (!lPort.equals(iEmptyString)) { lTransport = lTransport + ":" + lPort; } } } lConfig.setPreference(TDConfig.TRANSPORT, lTransport); } } // Commsdb options .. emulator if (lAttributes.containsKey(DriverLaunchConstants.COMMDB)) { String lCommDB = ((String) lAttributes.get(DriverLaunchConstants.COMMDB)).toLowerCase(); if (lCommDB.equalsIgnoreCase("overwrite")) { String lOverwrite = (String) lAttributes.get(DriverLaunchConstants.COMMDB_OVERWRITE); lConfig.setPreference(TDConfig.COMMDB, lCommDB + (!lOverwrite.equals(iEmptyString) ? "=" + lOverwrite : iEmptyString)); } else { lConfig.setPreference(TDConfig.COMMDB, lCommDB); } } // set remaining values (these are defaults as we do not support // EKA1) lConfig.setPreference(TDConfig.KERNEL, "EKA2"); lConfig.setPreference(TDConfig.BUILD_NUMBER, "tdep_build"); lConfig.setPreferenceBoolean(TDConfig.PLATSEC, true); lConfig.setPreference(TDConfig.UCC_IP_ADDRESS, ""); }
From source file:Main.java
@SuppressWarnings("unchecked") public static void mergeNestedMap(Map<Object, Object> baseMap, Map<Object, Object> overrideMap, boolean overrideEmpty) { if (baseMap == null || overrideMap == null) return;// w w w.j a va2s . com for (Map.Entry<Object, Object> entry : overrideMap.entrySet()) { Object key = entry.getKey(); Object value = entry.getValue(); if (baseMap.containsKey(key)) { if (value == null) { if (overrideEmpty) baseMap.put(key, null); } else { if (value instanceof CharSequence) { if (overrideEmpty || ((CharSequence) value).length() > 0) baseMap.put(key, value); } else if (value instanceof Map) { Object baseValue = baseMap.get(key); if (baseValue != null && baseValue instanceof Map) { mergeNestedMap((Map) baseValue, (Map) value, overrideEmpty); } else { baseMap.put(key, value); } } else if (value instanceof Collection) { Object baseValue = baseMap.get(key); if (baseValue != null && baseValue instanceof Collection) { Collection baseCol = (Collection) baseValue; Collection overrideCol = (Collection) value; for (Object overrideObj : overrideCol) { // NOTE: if we have a Collection of Map we have no way to merge the Maps without knowing the 'key' entries to use to match them if (!baseCol.contains(overrideObj)) baseCol.add(overrideObj); } } else { baseMap.put(key, value); } } else { // NOTE: no way to check empty, if not null not empty so put it baseMap.put(key, value); } } } else { baseMap.put(key, value); } } }
From source file:io.fabric8.apiman.gateway.ApimanGatewayStarter.java
private static URL waitForDependency(URL url, String serviceName, String key, String value, String username, String password) throws InterruptedException { boolean isFoundRunningService = false; ObjectMapper mapper = new ObjectMapper(); int counter = 0; URL endpoint = null;//from ww w.j av a2 s . c o m while (!isFoundRunningService) { endpoint = resolveServiceEndpoint(url.getProtocol(), url.getHost(), String.valueOf(url.getPort())); if (endpoint != null) { String isLive = null; try { URL statusURL = new URL(endpoint.toExternalForm() + url.getPath()); HttpURLConnection urlConnection = (HttpURLConnection) statusURL.openConnection(); urlConnection.setConnectTimeout(500); if (urlConnection instanceof HttpsURLConnection) { try { KeyStoreUtil.Info tPathInfo = new KeyStoreUtil().new Info(TRUSTSTORE_PATH, TRUSTSTORE_PASSWORD_PATH); TrustManager[] tms = KeyStoreUtil.getTrustManagers(tPathInfo); KeyStoreUtil.Info kPathInfo = new KeyStoreUtil().new Info(CLIENT_KEYSTORE_PATH, CLIENT_KEYSTORE_PASSWORD_PATH); KeyManager[] kms = KeyStoreUtil.getKeyManagers(kPathInfo); final SSLContext sc = SSLContext.getInstance("TLS"); sc.init(kms, tms, new java.security.SecureRandom()); final SSLSocketFactory socketFactory = sc.getSocketFactory(); HttpsURLConnection.setDefaultSSLSocketFactory(socketFactory); HttpsURLConnection httpsConnection = (HttpsURLConnection) urlConnection; httpsConnection.setHostnameVerifier(new DefaultHostnameVerifier()); httpsConnection.setSSLSocketFactory(socketFactory); } catch (Exception e) { log.error(e.getMessage(), e); throw e; } } if (Utils.isNotNullOrEmpty(username)) { String encoded = Base64.getEncoder() .encodeToString((username + ":" + password).getBytes("UTF-8")); log.info(username + ":******"); urlConnection.setRequestProperty("Authorization", "Basic " + encoded); } isLive = IOUtils.toString(urlConnection.getInputStream()); Map<String, Object> esResponse = mapper.readValue(isLive, new TypeReference<Map<String, Object>>() { }); if (esResponse.containsKey(key) && value.equals(String.valueOf(esResponse.get(key)))) { isFoundRunningService = true; } else { if (counter % 10 == 0) log.info(endpoint.toExternalForm() + " not yet up (host=" + endpoint.getHost() + ")" + isLive); } } catch (Exception e) { if (counter % 10 == 0) log.info(endpoint.toExternalForm() + " not yet up. (host=" + endpoint.getHost() + ")" + e.getMessage()); } } else { if (counter % 10 == 0) log.info("Could not find " + serviceName + " in namespace, waiting.."); } counter++; Thread.sleep(1000l); } return endpoint; }
From source file:net.sourceforge.fenixedu.presentationTier.docs.academicAdministrativeOffice.ApprovementInfoForEquivalenceProcess.java
static protected String getAcademicUnitIdentifier(final Map<Unit, String> academicUnitIdentifiers, final Unit academicUnit) { if (!academicUnitIdentifiers.containsKey(academicUnit)) { academicUnitIdentifiers.put(academicUnit, identifiers[academicUnitIdentifiers.size()]); }// ww w.j a v a 2s . co m return academicUnitIdentifiers.get(academicUnit); }
From source file:de.vandermeer.skb.base.utils.Skb_STUtils.java
/** * Returns a list of arguments missing in the given StringTemplate. * In detail, the given argument list is tested against the formal arguments of the given StringTemplate. Any arguments not defined * by the StringTemplate will be added to the returned list. * @param st template to be tested/*from ww w. ja v a 2s . c om*/ * @param expectedArguments list of expected arguments * @return null if input parameters are null, * empty list if all arguments are declared by the template, * non-empty list if any expected argument is not declared in the template */ public static final List<String> getMissingSTArguments(ST st, List<String> expectedArguments) { if (st == null) { return null; } if (expectedArguments == null) { return null; } List<String> ret; Map<?, ?> formalArgs = st.impl.formalArguments; if (formalArgs == null) { ret = new ArrayList<String>(expectedArguments); } else { ret = new ArrayList<String>(); for (int i = 0; i < expectedArguments.size(); i++) { if (!formalArgs.containsKey(expectedArguments.get(i))) { ret.add(expectedArguments.get(i)); } } } return ret; }
From source file:com.fpuna.preproceso.util.Util.java
public static Double calculateShannonEntropy(double valuesD[]) { Map<String, Integer> map = new HashMap<String, Integer>(); List<String> values = new ArrayList<String>(); for (Double d : valuesD) { values.add(d.toString());/*from w w w. j a v a 2 s .c o m*/ } // count the occurrences of each value for (String sequence : values) { if (!map.containsKey(sequence)) { map.put(sequence, 0); } map.put(sequence, map.get(sequence) + 1); } // calculate the entropy Double result = 0.0; for (String sequence : map.keySet()) { Double frequency = (double) map.get(sequence) / values.size(); result -= frequency * (Math.log(frequency) / Math.log(2)); } return result; }