【问题标题】:How to get separate key and value of hashmap without iteration如何在不迭代的情况下获取hashmap的单独键和值
【发布时间】:2015-09-25 21:15:51
【问题描述】:

这里的键是纬度和经度的差异,值是字符串值的列表。我想做单独的 hashmap 键和值。
有可能这样做吗??? 请给我建议。

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();
}  

我不想对 keyset() 使用迭代或循环。不迭代如何使用?

【问题讨论】:

  • 发布更多代码并更准确地解释您的问题,谢谢。
  • 我添加了整个代码@SilentKnight

标签: android hashmap keyset entryset


【解决方案1】:

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

【讨论】:

  • 谢谢,但是如何在没有迭代的情况下使用它? @Sumighosh
【解决方案2】:
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...
}

【讨论】:

  • 谢谢,但我不想迭代或循环。可能吗? @SilenKnight
  • “我不想迭代或循环”是什么意思?如果您想要键和值的每个值,则必须这样做。 hashMap.toString() 或 keySet.toString() 的 valueSet.toString() 是你想要的吗?
  • 由于我的问题条件,我已经在 do while 循环中编写了这个条件,所以我想从 keyset() @SilentKnight 中删除迭代或循环
猜你喜欢
  • 1970-01-01
  • 2010-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 2018-09-10
  • 1970-01-01
相关资源
最近更新 更多