简体   繁体   English

如何在不进行迭代的情况下获得哈希图的单独键和值

[英]How to get separate key and value of hashmap without iteration

Here key as difference of latitude and longitude and value as list of String values. 在这里,键表示纬度和经度之差,值表示字符串值的列表。 I want to do separate key and value of hashmap. 我想将哈希图的键和值分开。
Is it possible to do that??? 有可能这样做吗??? Please suggest me. 请给我建议。

MainActivity.java

public void getExpandableListData() {
    Cursor cursor = databaseHelper.getLoadMoreData(count);
    cursor.moveToFirst();
    String businessName;
    String latitude;
    String longitude;
    String categoryDescription;
    double difference;
    Log.i(TAG, "cursor.getCount() :" + cursor.getCount());
    do {
        categoryDescription = cursor.getString(cursor
                .getColumnIndex("categorydesc"));
        Log.i("categoryDescription", "" + categoryDescription);
        int categoryId = cursor.getInt(cursor.getColumnIndex("CategoryId"));
        Log.i("category Id",
                "" + cursor.getInt(cursor.getColumnIndex("CategoryId")));
        listDataHeader.add(categoryDescription);
        Log.w("list Data Header", "" + listDataHeader);
        currentLocation = new Location("");
        currentLocation.setLatitude(18.5522);
        currentLocation.setLongitude(73.8956);
        List<ChildInfo> childList = new ArrayList<ChildInfo>();
        Cursor cursorChild = databaseHelper.getChildData(categoryId);
        Map.Entry<Double, List<ChildInfo>> entry;
        List<ChildInfo> list = null;
        cursorChild.moveToFirst();
        do {
            businessName = cursorChild.getString(cursorChild
                    .getColumnIndex("BusinessName"));
            phoneNumber = cursorChild.getString(cursorChild
                    .getColumnIndex("Phone"));
            categoryDescription = cursorChild.getString(cursorChild
                    .getColumnIndex("categorydesc"));
            latitude = cursorChild.getString(cursorChild

            .getColumnIndex("Latitude"));
            longitude = cursorChild.getString(cursorChild
                    .getColumnIndex("Longitude"));
            ChildInfo childInfo = new ChildInfo(businessName, phoneNumber,
                    categoryDescription, latitude, longitude);
            childList.add(childInfo);
            Location savedLocation = new Location("databaseLocation");
            savedLocation.setLatitude(Double.parseDouble(latitude));
            savedLocation.setLongitude(Double.parseDouble(longitude));
            difference = currentLocation.distanceTo(savedLocation) * (0.001);
            hashMapLocationDifference.put(difference, childList);
            hashMapLocationDifference = sortByKeys(hashMapLocationDifference);
            Log.i("sortedHashMap:", "" + hashMapLocationDifference);
            Set<Double> keySet = new LinkedHashSet<Double>();
            keySet = hashMapLocationDifference.keySet();
            entry = hashMapLocationDifference.entrySet().iterator().next();
            list = new ArrayList<ChildInfo>();
            list = entry.getValue();
            listDataChild.put(categoryDescription, list);
        } while (cursorChild.moveToNext());
    } while (cursor.moveToNext());
    cursor.close();
}  

I don't want use iteration or loop for keyset(). 我不想为keyset()使用迭代或循环。 how to use without iteration? 如何使用而无需迭代?

您可以使用keySet和entrySet分别检索键和条目

HashMap<Double, List<ChildInfo>> hashMap= new HashMap<Double, List<ChildInfo>>();
Set<Double> keySet=hashMap.keySet();
Set<List<ChildInfo>> valueSet=hashMap.valueSet();
//Loop
for(Double d:keySet){
    List<ChildInfo> children=hashMap.get(d);
    //continue here...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM