Example usage for java.util HashMap clear

List of usage examples for java.util HashMap clear

Introduction

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

Prototype

public void clear() 

Source Link

Document

Removes all of the mappings from this map.

Usage

From source file:controllers.SnLocationsController.java

public static void trendingFsqLocations(String lat, String lng) {

    String appid = params._contains(PARAM_APPID) ? params.get(PARAM_APPID) : "";
    String limit = params._contains(PARAM_LIMIT) ? params.get(PARAM_LIMIT) : "";
    limit = verifyRecordLimit(limit);/*from  w w  w  .  j  a va  2 s.  c  om*/
    String radius = params._contains(PARAM_RADIUS) ? params.get(PARAM_RADIUS) : "";
    radius = verifyRadius(radius);
    String herenow = params._contains(PARAM_HERENOW) ? params.get(PARAM_HERENOW) : "true";// by default, thats true!
    String query = params._contains(PARAM_QUERY) ? params.get(PARAM_QUERY) : "";

    Logger.info("PARAMS -> appid:%s ; lat,lng:%s,%s ; radius:%s ; limit:%s ; herenow:%s ; query:%s", appid, lat,
            lng, radius, limit, herenow, query);

    // using Async jobs
    ResponseModel responseModel = new ResponseModel();
    ResponseMeta responseMeta = new ResponseMeta();
    LinkedList<Object> dataList = new LinkedList<Object>();

    HashMap params = new HashMap();
    String cacheKey = CACHE_KEYPREFIX_TRENDING + "geo:" + lat + "," + lng;
    if (!StringUtils.isEmpty(limit))
        cacheKey += "|" + limit;
    if (!StringUtils.isEmpty(query))
        cacheKey += "|" + query;

    try {

        //dataList = (LinkedList<Object>) Cache.get(cacheKey);
        dataList = Cache.get(cacheKey, LinkedList.class);
        if (dataList == null) {

            dataList = new LinkedList<Object>();

            params.clear();
            if (!StringUtils.isEmpty(lat) && !StringUtils.isEmpty(lng))
                params.put("ll", lat + "," + lng);
            if (!StringUtils.isEmpty(limit))
                params.put(PARAM_LIMIT, limit);

            if (!StringUtils.isEmpty(radius))
                params.put(PARAM_RADIUS, radius);
            /*params.put(PARAM_RADIUS,
                  !StringUtils.isEmpty(radius)?
                radius
                :Play.configuration.getProperty("fsqdiscovery.trending.API_LOCO_SEARCHDISTANCE")
                  );*/
            if (!StringUtils.isEmpty(query))
                params.put(PARAM_QUERY, query);

            FoursquareTrendingPoiJob mFoursquareTrendingPoiJob = new FoursquareTrendingPoiJob();
            mFoursquareTrendingPoiJob.setReqParams(params);
            dataList.addAll((LinkedList<Object>) mFoursquareTrendingPoiJob.doJobWithResult());

            //Logger.info("adding to cache!!! %s", dataList.size());
            //Cache.set(cacheKey, dataList, CACHE_TTL_TRENDING);
            if (dataList.size() > 0) {
                Logger.info("adding to cache!!! %s", dataList.size());
                Cache.set(cacheKey, dataList, CACHE_TTL_TRENDING);
            } else {
                Logger.info("NO NEED to cache, dataList.size(): 0");

                response.status = Http.StatusCode.OK;
                responseMeta.code = response.status;
                responseModel.meta = responseMeta;
                responseModel.data = dataList;

                renderJSON(LocoUtils.getGson().toJson(responseModel));
            }
        } else {
            Logger.info("Found in CACHE!!! %s", dataList.size());
        }

        if ("true".equalsIgnoreCase(herenow)) {
            // HereNow part
            params.clear();
            FoursquareDiscoverHereNowJob mFoursquareDiscoverHereNowJob = new FoursquareDiscoverHereNowJob();
            mFoursquareDiscoverHereNowJob.setReqParams(params);
            mFoursquareDiscoverHereNowJob.setPoiList(dataList);
            dataList = new LinkedList<Object>();//dataList.clear();
            dataList.addAll((LinkedList<Object>) mFoursquareDiscoverHereNowJob.doJobWithResult());
        } else {
            Logger.info("herenow param is NOT set true, skip loading hereNow!!! herenow: %s", herenow);
        }

        response.status = Http.StatusCode.OK;
        responseMeta.code = response.status;
        responseModel.meta = responseMeta;
        responseModel.data = dataList;

        renderJSON(LocoUtils.getGson().toJson(responseModel));
    } catch (Exception ex) {

        responseMeta.code = Http.StatusCode.INTERNAL_ERROR;
        gotError(responseMeta, ex);
        //renderJSON(responseModel);
    }
}

From source file:controllers.SnLocationsController.java

public static void discoverFsqLocations(String lat, String lng) {

    String appid = params._contains(PARAM_APPID) ? params.get(PARAM_APPID) : "";
    String limit = params._contains(PARAM_LIMIT) ? params.get(PARAM_LIMIT) : "";
    limit = verifyRecordLimit(limit);//from  w w  w.  j av  a  2  s . c om
    String radius = params._contains(PARAM_RADIUS) ? params.get(PARAM_RADIUS) : "";
    radius = verifyRadius(radius);
    String herenow = params._contains(PARAM_HERENOW) ? params.get(PARAM_HERENOW) : "true";// by default : true
    String query = params._contains(PARAM_QUERY) ? params.get(PARAM_QUERY) : "";

    Logger.info("PARAMS -> appid:%s ; lat,lng:%s,%s ; radius:%s ; limit:%s ; herenow:%s ; query:%s", appid, lat,
            lng, radius, limit, herenow, query);

    ResponseModel responseModel = new ResponseModel();
    ResponseMeta responseMeta = new ResponseMeta();
    LinkedList<Object> dataList = new LinkedList<Object>();

    HashMap params = new HashMap();
    String cacheKey = CACHE_KEYPREFIX_NEARBY + "geo:" + lat + "," + lng;
    if (!StringUtils.isEmpty(limit))
        cacheKey += "|" + limit;
    if (!StringUtils.isEmpty(query))
        cacheKey += "|" + query;

    try {

        //dataList = (LinkedList<Object>) Cache.get(cacheKey);
        dataList = Cache.get(cacheKey, LinkedList.class);
        if (dataList == null) {

            dataList = new LinkedList<Object>();

            params.clear();
            if (!StringUtils.isEmpty(lat) && !StringUtils.isEmpty(lng))
                params.put("ll", lat + "," + lng);
            if (!StringUtils.isEmpty(limit))
                params.put(PARAM_LIMIT, limit);

            if (!StringUtils.isEmpty(radius))
                params.put(PARAM_RADIUS, radius);
            /*params.put(PARAM_RADIUS,
                  !StringUtils.isEmpty(radius)?
                radius
                :Play.configuration.getProperty("fsqdiscovery.discovery.API_LOCO_SEARCHDISTANCE")
                  );*/
            if (!StringUtils.isEmpty(query))
                params.put(PARAM_QUERY, query);

            FoursquareDiscoverPoiJob mFoursquarePoiJob = new FoursquareDiscoverPoiJob();
            mFoursquarePoiJob.setReqParams(params);
            dataList.addAll((LinkedList<Object>) mFoursquarePoiJob.doJobWithResult());

            if (dataList.size() > 0) {
                Logger.info("adding to cache!!! %s", dataList.size());
                Cache.set(cacheKey, dataList, CACHE_TTL_NEARBY);
            } else {
                Logger.info("NO NEED to cache, dataList.size(): 0");

                response.status = Http.StatusCode.OK;
                responseMeta.code = response.status;
                responseModel.meta = responseMeta;
                responseModel.data = dataList;

                renderJSON(LocoUtils.getGson().toJson(responseModel));
            }
        } else {
            Logger.info("Found in CACHE!!! %s", dataList.size());
        }

        if ("true".equalsIgnoreCase(herenow)) {
            // HereNow part
            params.clear();
            FoursquareDiscoverHereNowJob mFoursquareDiscoverHereNowJob = new FoursquareDiscoverHereNowJob();
            mFoursquareDiscoverHereNowJob.setReqParams(params);
            mFoursquareDiscoverHereNowJob.setPoiList(dataList);
            dataList = new LinkedList<Object>();//dataList.clear();
            dataList.addAll((LinkedList<Object>) mFoursquareDiscoverHereNowJob.doJobWithResult());

            // remove items which doesn't have any hereNow in it!!!
            try {
                PoiModelFoursquare fsqPoi = null;
                LinkedList<Object> dataListFiltered = new LinkedList<Object>();
                for (Object obj : dataList) {
                    fsqPoi = (PoiModelFoursquare) obj;

                    //if (fsqPoi.herenow==null || fsqPoi.herenow.size()==0) dataList.remove(obj);
                    if (fsqPoi.herenow != null && fsqPoi.herenow.size() > 0)
                        dataListFiltered.add(obj);
                }
                Logger.info("dataList.size(): %s | dataListFiltered.size(): %s", dataList.size(),
                        dataListFiltered.size());
                dataList = new LinkedList<Object>();//dataList.clear();
                dataList.addAll(dataListFiltered);
            } catch (Exception ex) {
                Logger.warn("exception while filtering out hereNow : %s", ex.toString());
            }
        } else {
            Logger.info("herenow param is NOT set true, skip loading hereNow!!! herenow: %s", herenow);
        }

        response.status = Http.StatusCode.OK;
        responseMeta.code = response.status;
        responseModel.meta = responseMeta;
        responseModel.data = dataList;

        renderJSON(LocoUtils.getGson().toJson(responseModel));
    } catch (Exception ex) {

        responseMeta.code = Http.StatusCode.INTERNAL_ERROR;
        gotError(responseMeta, ex);
        //renderJSON(responseModel);
    }
}

From source file:net.issarlk.androbunny.inkbunny.API.java

public static String extractTree(Pattern pattern, String data) {
    Matcher matches = pattern.matcher(data);
    if (matches.matches()) {
        data = matches.group(1);/*from www .  j  a v  a 2  s  .c o m*/
        StringBuilder result = new StringBuilder();
        //Extract
        int taglevel = 0;

        CharacterIterator it = new StringCharacterIterator(data);
        int mode = 0;
        int inparamname = 0;
        int inparamvalue = 0;
        int outsidetags = 0;
        int intagname = 0;
        char paramvaluechar = '"';
        HashMap<String, String> params = new HashMap<String, String>();
        StringBuilder tagname = new StringBuilder();
        StringBuilder paramname = new StringBuilder();
        StringBuilder paramvalue = new StringBuilder();
        StringBuilder text = new StringBuilder();
        for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {

            if (c == '<') {
                //start of tag
                c = it.next();
                params.clear();
                outsidetags = 0;
                //Output text
                //Log.d(TAG, repeat(' ', taglevel) + text.toString());
                result.append(text);
                if (c == '/') {
                    //Start of closing tag
                    mode = 3;
                    intagname = 1;
                    c = it.next();
                } else {
                    //Start of openning tag
                    mode = 2;
                    intagname = 1;
                }
                tagname = new StringBuilder();
            }
            if (intagname == 1) {
                if (c < 'a' || c > 'z') {
                    intagname = 0;
                } else {
                    tagname.append(c);
                }
            }
            if ((mode == 2 || mode == 3) && intagname == 0 && inparamname == 0 && inparamvalue == 0 && c >= 'a'
                    && c <= 'z') {
                inparamname = 1;
                paramname = new StringBuilder();
            }
            if (inparamname == 1) {
                if (c == '=') {
                    inparamname = 0;
                    inparamvalue = 1;
                    paramvaluechar = it.next();
                    paramvalue = new StringBuilder();
                    continue;
                } else if (c != ' ') {
                    paramname.append(c);
                }
            }
            if (inparamvalue == 1) {
                if (c == paramvaluechar) {
                    //End of value
                    inparamvalue = 0;
                    params.put(paramname.toString(), paramvalue.toString());
                } else {
                    paramvalue.append(c);
                }
            }
            if (mode == 2 && inparamvalue == 0 && c == '/') {
                c = it.next();
                if (c == '>') {
                    //tag closed at its end
                    mode = 4;
                    if (tagname.toString().equals("br")) {
                        result.append("<br/>");
                    }
                } else {
                    c = it.previous();
                }
            }
            if (c == '>') {
                if (mode == 2) {
                    //openning
                    taglevel += 1;
                    outsidetags = 1;
                    text = new StringBuilder();
                    String paramstr = "";
                    for (String key : params.keySet()) {
                        paramstr += " " + key + "=\"" + params.get(key) + "\"";
                    }
                    //Log.d(TAG, repeat(' ', taglevel - 1) + "<" + tagname.toString() + paramstr + ">");
                    result.append("<" + tagname.toString() + paramstr + ">");
                    mode = 0;
                    continue;
                } else if (mode == 3) {
                    //Closing
                    taglevel -= 1;
                    outsidetags = 1;
                    text = new StringBuilder();
                    //Log.d(TAG, repeat(' ', taglevel) + "</" + tagname.toString() + ">");
                    result.append("</" + tagname.toString() + ">\n");
                    mode = 0;
                    if (taglevel <= 0) {
                        break;
                    }
                    continue;
                } else if (mode == 4) {
                    //closed at end
                    outsidetags = 1;
                    text = new StringBuilder();
                    //Log.d(TAG, repeat(' ', taglevel) + "<" + tagname.toString() + "/>");
                    result.append("<" + tagname.toString() + "/>\n");
                    mode = 0;
                    continue;
                }
            }
            if (outsidetags == 1) {
                //Append char
                text.append(c);
            }
        }

        String res = result.toString();
        return res;
    }
    return null;
}

From source file:Main.java

public static int getFrequentElement(int[] bcp) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    ArrayList<Integer> count = new ArrayList<Integer>();
    ArrayList<Integer> uniId = new ArrayList<Integer>();
    int id = 0;/*from w  ww.  j  a va2s . c o m*/

    for (int col = 0; col < bcp.length; col++) {
        //System.out.print(bcp[col] + "\t");
        int no = 0;
        if (!map.containsKey(bcp[col])) {
            map.put(bcp[col], id++);
            count.add(1);
            uniId.add(bcp[col]);
        } else {
            no = map.get(bcp[col]);
            count.set(no, count.get(no) + 1);
        }
    }

    int maximum = Integer.MIN_VALUE;
    int maxId = Integer.MIN_VALUE;
    for (int i = 0; i < count.size(); i++) {
        //System.out.print(uniId.get(i) + ":" + count.get(i) + ",\t");
        if (maximum < count.get(i)) {
            maximum = count.get(i);
            maxId = uniId.get(i);
        }
    }
    //System.out.println();

    map.clear();
    uniId.clear();
    count.clear();
    return maxId;
}

From source file:Main.java

public static boolean getFrequentElementBinary(int[] sample) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    ArrayList<Integer> count = new ArrayList<Integer>();
    ArrayList<Integer> uniId = new ArrayList<Integer>();
    int id = 0;//from www.  j  av a 2s . c  om

    for (int col = 0; col < sample.length; col++) {
        // System.out.print(bcp[col] + "\t");
        int no = 0;
        if (!map.containsKey(sample[col])) {
            map.put(sample[col], id++);
            count.add(1);
            uniId.add(sample[col]);
        } else {
            no = map.get(sample[col]);
            count.set(no, count.get(no) + 1);
        }
    }

    int maximum = Integer.MIN_VALUE;
    int maxId = Integer.MIN_VALUE;
    for (int i = 0; i < count.size(); i++) {
        // System.out.print(uniId.get(i) + ":" + count.get(i) + ",\t");
        if (maximum < count.get(i)) {
            maximum = count.get(i);
            maxId = uniId.get(i);
        }
    }
    // System.out.println();

    map.clear();
    uniId.clear();
    count.clear();
    if (maxId == 1)
        return true;
    else
        return false;
}

From source file:br.bookmark.db.util.ResultSetUtils.java

/**
 * Return a ArrayList of beans populated from a ResultSet.
 *
 * @param resultSet The ResultSet whose parameters are to be used
 * to populate bean properties//  www  . j  a  v  a 2 s .c  om
 * @param target An instance of the bean to populate
 * @exception SQLException if an exception is thrown while setting
 * property values, populating the bean, or accessing the ResultSet
 */
public static Collection getCollection(Object target, ResultSet resultSet) throws Exception {

    // Check prerequisites
    if ((target == null) || (resultSet == null))
        throw new GenericDAOException("getCollection: Null parameter");

    // Acquire resultSet MetaData
    ResultSetMetaData metaData = resultSet.getMetaData();
    int cols = metaData.getColumnCount();

    // Create hashmap, sized to number of columns
    HashMap properties = new HashMap(cols, 1);

    // Use ArrayList to maintain ResultSet sequence
    ArrayList list = new ArrayList();

    // Acquire target class
    Class factory = target.getClass();

    // Scroll to next record and pump into hashmap
    while (resultSet.next()) {
        for (int i = 1; i <= cols; i++) {
            putEntry(properties, metaData, resultSet, i, target.getClass());
        }
        Object bean = factory.newInstance();
        BeanUtils.copyProperties(bean, properties);
        list.add(bean);
        properties.clear();

    } // end while

    return ((Collection) list);

}

From source file:pt.ua.ieeta.dicom.DICOMTemplateRuleImp.java

@Override
public void setMapping(HashMap<Integer, Integer> maps) {
    HashMap<String, String> map = rule.get("map");
    map.clear();
    for (Entry<Integer, Integer> e : maps.entrySet()) {
        map.put(dicom.nameOf(e.getKey()), dicom.nameOf(e.getValue()));
    }/*from  w  ww.  ja v a 2s.c o  m*/
}

From source file:pt.ua.ieeta.dicom.DICOMTemplateRuleImp.java

@Override
public void setI18n(HashMap<String, String> i18ns) {
    HashMap<String, String> i18n = rule.get("i18n");
    i18n.clear();
    i18n.putAll(i18ns);/*www.j  av a 2 s.com*/
}

From source file:pt.ua.ieeta.dicom.DICOMTemplateRuleImp.java

@Override
public void setMatchFields(HashMap<Integer, String> match) {
    HashMap<String, String> matchfields = rule.get("matchfields");
    matchfields.clear();
    for (Entry<Integer, String> e : match.entrySet()) {
        matchfields.put(dicom.nameOf(e.getKey()), e.getValue());
    }/*  w w  w  . ja  v a2 s.c o m*/
}

From source file:erainformatica.tbjhelper.helpers.TelegramBotReceiver.java

@Override
public void run() {
    HashMap parameters = new HashMap();
    while (true) {
        parameters.clear();
        if (offset != null)
            parameters.put("offset", offset.toString());
        if (timeout != null)
            parameters.put("timeout", timeout.toString());
        String url = Utility.buildAPIUrl(this.api_key, TelegramBot.API_OPERATION_GET_UPDATES, parameters);
        TelegramRequestResult<JSONObject> result = JSONHelper.readJsonFromUrl(url);
        if (result.isRequestOk()) {
            Update[] updates;/*from w w  w .  jav a2  s  . c o  m*/
            JSONObject r = result.get();
            if (r.getBoolean("ok") == true) {
                try {
                    updates = (Update[]) Utility.castJsonToObject(r.getJSONArray("result").toString(),
                            TelegramBot.TYPE_ARRAY_OF_UPDATES);
                } catch (Exception e) {
                    updates = null;
                }
                if (updates != null && updates.length > 0) {
                    try {
                        if (this.onUpdate(updates)) {
                            offset = updates[updates.length - 1].getUpdate_id() + 1;
                        }
                    } catch (Exception e) {

                    }
                }
            }
        }
    }
}