gpt4 book ai didi

java - 如何对 Android 应用程序的 HashMap 中的数据进行排序?

转载 作者:行者123 更新时间:2023-12-01 13:47:12 25 4
gpt4 key购买 nike

我试图在 listdataHeader 中按升序对 arr.get() 值进行排序,以便它以以下形式打印出 listDataheader:名称1 1.0名称3 2.0名称5 3.0name2 4.0 等

而不是我预定义的格式

我尝试对 HashMap 进行排序,但当我打开此 Pane 时应用程序崩溃了,listdataheader 如何按随机生成的数组值的升序排列?

package com.example.listview;

import info.androidhive.expandablelistview.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

public class MainActivity extends Activity {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);

// preparing list data
prepareListData();

listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

// setting list adapter
expListView.setAdapter(listAdapter);

// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {

@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});

// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition),
Toast.LENGTH_SHORT).show();
}
});

// Listview Group collasped listener
/*expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();

}
});*/

// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {

@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}

/*
* Preparing the list data
*/

String[] stations = {"shell","Esso","Tesco","Asda","BP","Texaco",};
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();

ArrayList<Float> arr = new ArrayList<Float> ();
for (int i = 0 ; i <= 12 ; i++)
{
arr.add(i, (float) (((Math.random() * 90) + 10)/10)); // add random values to array
}


// Adding child data
listDataHeader.add("Shell distance: "+ arr.get(0));
listDataHeader.add("Esso Distance:" +arr.get(1));
listDataHeader.add("Tesco Distance "+arr.get(2));
listDataHeader.add("Asda Distance "+arr.get(3));
listDataHeader.add("BP Distance "+arr.get(4));
listDataHeader.add("Texaco Distance "+arr.get(5));

// Adding child data
List<String> shell = new ArrayList<String>();
shell.add("Distance: "+arr.get(0));
shell.add("Fuel price: 141p/litre");
shell.add("Petrol price: 134p/litre");

//esso details
List<String> esso = new ArrayList<String>();
esso.add("Distance: "+arr.get(1));
esso.add("Fuel price: 145p/litre");
esso.add("Petrol price: 131p/litre");

//tesco
List<String> tesco_energy = new ArrayList<String>();
tesco_energy.add("Distance: "+arr.get(2));
tesco_energy.add("Fuel price: 144p/litre");
tesco_energy.add("Petrol price: 139p/litre");

//asda
List<String> asda = new ArrayList<String>();
asda.add("Distance: "+arr.get(3));
asda.add("Fuel price: 139p/litre");
asda.add("Petrol price: 131p/litre");

//bp
List<String> bp = new ArrayList<String>();
bp.add("Distance: "+arr.get(4));
bp.add("Fuel price: 144p/litre");
bp.add("Petrol price: 139p/litre");

//texaco
List<String> texaco = new ArrayList<String>();
texaco.add("Distance: "+arr.get(5));
texaco.add("Fuel price: 144p/litre");
texaco.add("Petrol price: 139p/litre");

listDataChild.put(listDataHeader.get(0), shell); // Header, Child data
listDataChild.put(listDataHeader.get(1), esso);
listDataChild.put(listDataHeader.get(2), tesco_energy);
listDataChild.put(listDataHeader.get(3), asda);
listDataChild.put(listDataHeader.get(4), bp);
listDataChild.put(listDataHeader.get(5), texaco);

/*HashMap<String, Double> sorted = new HashMap<String, Double>();
sorted = sortHashMap((HashMap<String, Double>) listDataHeader);
for (String cityName : sorted.keySet()){
System.out.println(cityName + " " + sorted.get(cityName));
}*/
}

//sort map
private HashMap<String, Double> sortHashMap(HashMap<String, Double> input){
Map<String, Double> tempMap = new HashMap<String, Double>();
for (String wsState : input.keySet()){
tempMap.put(wsState,input.get(wsState));
}

List<String> mapKeys = new ArrayList<String>(tempMap.keySet());
List<Double> mapValues = new ArrayList<Double>(tempMap.values());
HashMap<String, Double> sortedMap = new LinkedHashMap<String, Double>();
TreeSet<Double> sortedSet = new TreeSet<Double>(mapValues);
Object[] sortedArray = sortedSet.toArray();
int size = sortedArray.length;
for (int i=0; i<size; i++){
sortedMap.put(mapKeys.get(mapValues.indexOf(sortedArray[i])),
(Double)sortedArray[i]);
}
return sortedMap;
}

}

最佳答案

我猜你想按值排序。在这种情况下,你可以这样做:

public Map<String, Double> sortMap(Map<String, Double> unsortMap) {

List<Map.Entry<String, Double>> list = new LinkedList<Entry<String, Double>>(
unsortMap.entrySet());

// sort list based on comparator
Collections.sort(list, new Comparator<Object>() {
@SuppressWarnings("unchecked")
public int compare(Object o1, Object o2) {
return ((Map.Entry<String, Double>) o1).getValue().compareTo(
((Map.Entry<String, Double>) o2).getValue());
}
});

// put sorted list into map again
Map<String, Double> sortedMap = new LinkedHashMap<String, Double>();
for (Iterator<Entry<String, Double>> it = list.iterator(); it.hasNext();) {
Map.Entry<String, Double> entry = it.next();
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}

但是,如果您想按键更改排序:

    return ((Map.Entry<String, Double>) o1).getValue().compareTo(
((Map.Entry<String, Double>) o2).getValue());

     return ((Map.Entry<String, Double>) o1).getKey().compareTo(
((Map.Entry<String, Double>) o2).getKey());

关于java - 如何对 Android 应用程序的 HashMap 中的数据进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20272094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com