Example usage for java.util HashMap entrySet

List of usage examples for java.util HashMap entrySet

Introduction

In this page you can find the example usage for java.util HashMap entrySet.

Prototype

Set entrySet

To view the source code for java.util HashMap entrySet.

Click Source Link

Document

Holds cached entrySet().

Usage

From source file:com.gmt2001.HttpRequest.java

public static HttpResponse getData(RequestType type, String url, String post, HashMap<String, String> headers) {
    Thread.setDefaultUncaughtExceptionHandler(com.gmt2001.UncaughtExceptionHandler.instance());

    HttpResponse r = new HttpResponse();

    r.type = type;/*w  ww  .j a  va  2s .  c o  m*/
    r.url = url;
    r.post = post;
    r.headers = headers;

    try {
        URL u = new URL(url);

        HttpURLConnection h = (HttpURLConnection) u.openConnection();

        for (Entry<String, String> e : headers.entrySet()) {
            h.addRequestProperty(e.getKey(), e.getValue());
        }

        h.setRequestMethod(type.name());
        h.setUseCaches(false);
        h.setDefaultUseCaches(false);
        h.setConnectTimeout(timeout);
        h.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36 PhantomBotJ/2015");
        if (!post.isEmpty()) {
            h.setDoOutput(true);
        }

        h.connect();

        if (!post.isEmpty()) {
            BufferedOutputStream stream = new BufferedOutputStream(h.getOutputStream());
            stream.write(post.getBytes());
            stream.flush();
            stream.close();
        }

        if (h.getResponseCode() < 400) {
            r.content = IOUtils.toString(new BufferedInputStream(h.getInputStream()), h.getContentEncoding());
            r.httpCode = h.getResponseCode();
            r.success = true;
        } else {
            r.content = IOUtils.toString(new BufferedInputStream(h.getErrorStream()), h.getContentEncoding());
            r.httpCode = h.getResponseCode();
            r.success = false;
        }
    } catch (IOException ex) {
        r.success = false;
        r.httpCode = 0;
        r.exception = ex.getMessage();

        com.gmt2001.Console.err.printStackTrace(ex);
    }

    return r;
}

From source file:com.quarterfull.newsAndroid.reader.owncloud.OwnCloudReaderMethods.java

private static String buildIdsToJSONArrayWithGuid(HashMap<String, String> items) {
    try {//  ww  w. j a v a 2s . c om
        JSONArray jArr = new JSONArray();
        for (Map.Entry<String, String> entry : items.entrySet()) {
            JSONObject jOb = new JSONObject();
            jOb.put("feedId", Integer.parseInt(entry.getValue()));
            jOb.put("guidHash", entry.getKey());
            jArr.put(jOb);
        }

        JSONObject jObj = new JSONObject();
        jObj.put("items", jArr);

        return jObj.toString();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:de.tor.tribes.util.AttackToTextWriter.java

public static boolean writeAttacks(Attack[] pAttacks, File pPath, int pAttacksPerFile, boolean pExtendedInfo,
        boolean pZipResults) {

    HashMap<Tribe, List<Attack>> attacks = new HashMap<>();

    for (Attack a : pAttacks) {
        Tribe t = a.getSource().getTribe();
        List<Attack> attsForTribe = attacks.get(t);
        if (attsForTribe == null) {
            attsForTribe = new LinkedList<>();
            attacks.put(t, attsForTribe);
        }/*from  w  w w. ja v  a2 s .c  o m*/
        attsForTribe.add(a);
    }

    Set<Entry<Tribe, List<Attack>>> entries = attacks.entrySet();
    for (Entry<Tribe, List<Attack>> entry : entries) {
        Tribe t = entry.getKey();
        List<Attack> tribeAttacks = entry.getValue();
        List<String> blocks = new LinkedList<>();

        while (!tribeAttacks.isEmpty()) {
            List<Attack> attsForBlock = new LinkedList<>();
            for (int i = 0; i < pAttacksPerFile; i++) {
                if (!tribeAttacks.isEmpty()) {
                    attsForBlock.add(tribeAttacks.remove(0));
                }
            }

            String fileContent = new AttackListFormatter().formatElements(attsForBlock, pExtendedInfo);
            blocks.add(fileContent);
        }
        if (!pZipResults) {
            writeBlocksToFiles(blocks, t, pPath);
        } else {
            writeBlocksToZip(blocks, t, pPath);
        }

    }
    return true;
}

From source file:com.microsoft.alm.plugin.idea.common.ui.workitem.WorkItemHelper.java

public static String getFieldValue(@NotNull final WorkItem item, @NotNull final String fieldName) {
    final HashMap<String, Object> fieldMap = item.getFields();
    if (fieldMap != null) {
        // Try a case sensitive search using the Map,
        // but if that doesn't work, loop through all the fields
        if (fieldMap.containsKey(fieldName)) {
            Object value = fieldMap.get(fieldName);
            if (value != null) {
                return value.toString();
            }//from w  ww . j a  v a2  s . c  om
        } else {
            for (final Map.Entry<String, Object> entry : fieldMap.entrySet()) {
                if (fieldName.equalsIgnoreCase(entry.getKey())) {
                    if (entry.getValue() != null) {
                        return entry.getValue().toString();
                    }
                }
            }
        }
    }
    return StringUtils.EMPTY;
}

From source file:com.uber.hoodie.hive.TestUtil.java

private static HoodieCommitMetadata createLogFiles(HashMap<String, List<HoodieWriteStat>> partitionWriteStats,
        boolean isLogSchemaSimple) throws InterruptedException, IOException, URISyntaxException {
    HoodieCommitMetadata commitMetadata = new HoodieCommitMetadata();
    for (Entry<String, List<HoodieWriteStat>> wEntry : partitionWriteStats.entrySet()) {
        String partitionPath = wEntry.getKey();
        for (HoodieWriteStat wStat : wEntry.getValue()) {
            Path path = new Path(wStat.getPath());
            HoodieDataFile dataFile = new HoodieDataFile(fileSystem.getFileStatus(path));
            HoodieLogFile logFile = generateLogData(path, isLogSchemaSimple);
            HoodieDeltaWriteStat writeStat = new HoodieDeltaWriteStat();
            writeStat.setFileId(dataFile.getFileId());
            writeStat.setPath(logFile.getPath().toString());
            commitMetadata.addWriteStat(partitionPath, writeStat);
        }/*from  www  .jav a  2 s  . c o  m*/
    }
    return commitMetadata;
}

From source file:com.ibm.bi.dml.runtime.controlprogram.parfor.opt.OptimizationWrapper.java

/**
 * //  ww w .  j av  a 2  s  .  c  o  m
 * @param prog
 * @param rtprog
 * @throws LanguageException 
 */
private static void findParForProgramBlocks(DMLProgram prog, Program rtprog,
        HashMap<Long, ParForStatementBlock> sbs, HashMap<Long, ParForProgramBlock> pbs)
        throws LanguageException {
    //handle function program blocks
    HashMap<String, FunctionProgramBlock> fpbs = rtprog.getFunctionProgramBlocks();
    for (Entry<String, FunctionProgramBlock> entry : fpbs.entrySet()) {
        String[] keypart = entry.getKey().split(Program.KEY_DELIM);
        String namespace = keypart[0];
        String name = keypart[1];

        ProgramBlock pb = entry.getValue();
        StatementBlock sb = prog.getFunctionStatementBlock(namespace, name);

        //recursive find 
        rfindParForProgramBlocks(sb, pb, sbs, pbs);
    }

    //handle actual program blocks
    ArrayList<ProgramBlock> tpbs = rtprog.getProgramBlocks();
    for (int i = 0; i < tpbs.size(); i++) {
        ProgramBlock pb = tpbs.get(i);
        StatementBlock sb = prog.getStatementBlock(i);

        //recursive find
        rfindParForProgramBlocks(sb, pb, sbs, pbs);
    }
}

From source file:org.raegdan.troca.Main.java

private static void printRatesAsJSON(HashMap<String, Double> rates, boolean fancy) {
    JsonNodeFactory factory = new JsonNodeFactory(true);
    ObjectNode rootNode = new ObjectNode(factory);

    for (Entry<String, Double> rate : rates.entrySet())
        rootNode.put(rate.getKey(), rate.getValue());

    try {/*from www.j  av a 2 s  .co m*/
        out((fancy) ? new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(rootNode)
                : rootNode.toString(), true);
    } catch (JsonProcessingException e) {
        printException(e);
    }
}

From source file:com.qwazr.cluster.manager.ClusterManager.java

private static TreeMap<String, StatusEnum> getStatusMap(HashMap<String, ClusterNodeSet> nodeMap) {
    TreeMap<String, StatusEnum> statusMap = new TreeMap<String, StatusEnum>();
    if (nodeMap == null)
        return statusMap;
    for (Map.Entry<String, ClusterNodeSet> entry : nodeMap.entrySet()) {
        Cache cache = entry.getValue().getCache();
        StatusEnum status = ClusterServiceStatusJson.findStatus(cache.activeArray.length,
                cache.inactiveArray.length);
        statusMap.put(entry.getKey(), status);
    }//from  www .ja v a2s  . c o  m
    return statusMap;
}

From source file:com.jaredrummler.android.devices.Main.java

private static void createDeviceJsonFiles(List<String[]> devices) throws IOException {
    File baseDir = new File(OUTPUT_DIR, "devices");
    if (baseDir.exists()) {
        FileUtils.deleteDirectory(baseDir);
    }// w  w  w. j ava2s.  c  o  m
    baseDir.mkdirs();

    // group all devices with the same codename together
    HashMap<String, List<DeviceInfo>> map = new HashMap<>();
    for (String[] arr : devices) {
        String key = arr[2];
        List<DeviceInfo> list = map.get(key);
        if (list == null) {
            list = new ArrayList<>();
        }
        list.add(new DeviceInfo(arr[0], arr[1], arr[2], arr[3]));
        map.put(key, list);
    }

    for (Map.Entry<String, List<DeviceInfo>> entry : map.entrySet()) {
        File file = new File(baseDir, entry.getKey() + ".json");
        FileUtils.write(file, PRETTY_GSON.toJson(entry.getValue()));
    }
}

From source file:Main.java

/**
 * Op Http post request , "404error" response if failed
 * //w  w  w  .  j av  a  2 s . co  m
 * @param url
 * @param map
 *            Values to request
 * @return
 */

static public String doHttpPost(String url, HashMap<String, String> map) {

    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT);
    HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT);
    ConnManagerParams.setTimeout(client.getParams(), TIMEOUT);
    HttpPost post = new HttpPost(url);
    post.setHeaders(headers);
    String result = "ERROR";
    ArrayList<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
    if (map != null) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            BasicNameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
            pairList.add(pair);
        }

    }
    try {
        HttpEntity entity = new UrlEncodedFormEntity(pairList, "UTF-8");
        post.setEntity(entity);
        HttpResponse response = client.execute(post);

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

            result = EntityUtils.toString(response.getEntity(), "UTF-8");

        } else {
            result = EntityUtils.toString(response.getEntity(), "UTF-8")
                    + response.getStatusLine().getStatusCode() + "ERROR";
        }

    } catch (ConnectTimeoutException e) {
        result = "TIMEOUTERROR";
    }

    catch (Exception e) {
        result = "OTHERERROR";
        e.printStackTrace();

    }
    return result;
}