2015-07-08 3 views
0

Здесь ключевое значение как разность широты и долготы и значение как список значений String. Я хочу сделать отдельный ключ и значение hashmap.
Можно ли это сделать ??? Пожалуйста, предложите мне.Как получить отдельный ключ и значение 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(); 
} 

Я не хочу использовать итерацию или цикл для набора ключей(). как использовать без итераций?

+0

пост больше кодов и объяснить ваш вопрос более точно, ТНХ. – SilentKnight

+0

Я добавил весь код @SilentKnight – abc

ответ

0

Вы можете использовать Keyset и entrySet получить ключи и записи соответственно

+0

Спасибо, но как использовать его без итерации? @Sumighosh – abc

0
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... 
} 
+0

Спасибо, но я не хочу итерации или цикла. Является ли это возможным? @SilenKnight – abc

+0

Что значит «Я не хочу итерации или цикла»? Если вам нужно каждое значение ключей и значений, вы должны сделать это. 'hashMap.toString()' или 'keySet.toString()' of 'valueSet.toString()' - это то, что вы хотите? – SilentKnight

+0

Я уже написал это условие во время цикла while из-за условия моей проблемы, поэтому я хочу удалить итерацию или цикл из keyset() @SilentKnight – abc

Смежные вопросы