Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-重新排序映射<;字符串,列表<;字符串>&燃气轮机;_Java_Android_Arrays_Sorting_Hashmap - Fatal编程技术网

Java-重新排序映射<;字符串,列表<;字符串>&燃气轮机;

Java-重新排序映射<;字符串,列表<;字符串>&燃气轮机;,java,android,arrays,sorting,hashmap,Java,Android,Arrays,Sorting,Hashmap,我有一个叫做数据的Map,看起来像这样 data = {Salad=[Salad Bar Station, Green Pepper & Tomato Salad, Lo Mein Noodle Salad], Pizza=[Cheese Pizza - Slice, Pepperoni Pizza, Tomato Bruschetta Flatbread Pizza, Herb Seasoned Breadsticks, Spaghet

我有一个叫做数据的Map>,看起来像这样

data = {Salad=[Salad Bar Station, Green Pepper & Tomato Salad, Lo Mein Noodle Salad], 
        Pizza=[Cheese Pizza - Slice, Pepperoni Pizza, Tomato Bruschetta Flatbread Pizza, Herb 
               Seasoned Breadsticks, Spaghetti Sauce with Tomato Bits], 
        Dessert=[Carnival Cookies, Sweet Cinnamon Brownies, Oreo Crumble Pudding Cup, 
                 Cherry Jell-O Parfait], 
        Cold Cereal=[Miscellaneous/Peripherals]}
data = Pizza=[Cheese Pizza - Slice, Pepperoni Pizza, Tomato Bruschetta Flatbread Pizza, Herb 
               Seasoned Breadsticks, Spaghetti Sauce with Tomato Bits], 
       Salad=[Salad Bar Station, Green Pepper & Tomato Salad, Lo Mein Noodle Salad],
       Cold Cereal=[Miscellaneous/Peripherals]
       Dessert=[Carnival Cookies, Sweet Cinnamon Brownies, Oreo Crumble Pudding Cup, 
                 Cherry Jell-O Parfait]}
现在我想重新排序,使它看起来像这样

data = {Salad=[Salad Bar Station, Green Pepper & Tomato Salad, Lo Mein Noodle Salad], 
        Pizza=[Cheese Pizza - Slice, Pepperoni Pizza, Tomato Bruschetta Flatbread Pizza, Herb 
               Seasoned Breadsticks, Spaghetti Sauce with Tomato Bits], 
        Dessert=[Carnival Cookies, Sweet Cinnamon Brownies, Oreo Crumble Pudding Cup, 
                 Cherry Jell-O Parfait], 
        Cold Cereal=[Miscellaneous/Peripherals]}
data = Pizza=[Cheese Pizza - Slice, Pepperoni Pizza, Tomato Bruschetta Flatbread Pizza, Herb 
               Seasoned Breadsticks, Spaghetti Sauce with Tomato Bits], 
       Salad=[Salad Bar Station, Green Pepper & Tomato Salad, Lo Mein Noodle Salad],
       Cold Cereal=[Miscellaneous/Peripherals]
       Dessert=[Carnival Cookies, Sweet Cinnamon Brownies, Oreo Crumble Pudding Cup, 
                 Cherry Jell-O Parfait]}
现在我有一些关于如何做这件事的伪代码,只是不太确定如何在代码中做

所以我想要一个数组,其中包含我想要的键的顺序。那就好像

String[] desiredOrder = {"Pizza","Salad","Cold Cereal"};
int destination = 0;
然后吃点类似的东西

for (int i=0; i < desiredOrder.length; i++) {
     //then find if the first desiredOrder object is in the data array
         //then check if it is in the right spot
             if (it is in right spot) {
                 //do nothing
             }
             else {
                  //move it to the right spot
             }
 }
for(int i=0;i
如果您能帮我填写剩下的内容,以便将键及其值移动到正确的位置,我将不胜感激

提前谢谢你的帮助

编辑

为了做到这一点,我正在从web解析xml,这就是我正在做的,注意我刚刚将它更改为LinkedHashMap,所以现在它按照xml的顺序得到它,但我仍然希望按照我自己的顺序重新排列它

String currentDay = "";
String currentMeal = "";
String counter = "";

 LinkedHashMap<String, List<String>> itemsByCounter = new LinkedHashMap<String , List<String>>();
        List<String> items = new ArrayList<String>();
while (eventType != XmlResourceParser.END_DOCUMENT) {
            String tagName = xmlData.getName();
            blogPost = new HashMap<String, String>();

            switch (eventType) {
                case XmlResourceParser.START_TAG:
                    if (tagName.equalsIgnoreCase("day")) {
                        currentDay = xmlData.getAttributeValue(null, "name");
                    }
                    if (tagName.equalsIgnoreCase("meal")) {
                        currentMeal = xmlData.getAttributeValue(null, "name");
                    }
                    if (tagName.equalsIgnoreCase("counter") && currentDay.equalsIgnoreCase(day) && currentMeal.equalsIgnoreCase(meal)) {
                        counter = xmlData.getAttributeValue(null, "name");
                    }

                    break;
                case XmlResourceParser.TEXT:
                    if (currentDay.equalsIgnoreCase(day) && currentMeal.equalsIgnoreCase(meal)) {
                        if (xmlData.getText().trim().length() > 0) {
                            items.add(xmlData.getText());

                        }

                    }
                    break;

                case XmlPullParser.END_TAG:
                    if (tagName.equalsIgnoreCase("counter")) {
                        if (items.size() > 0) {
                            itemsByCounter.put(counter, items);
                            items = new ArrayList<String>();
                        }
                    }
                    break;
            }
            eventType = xmlData.next();
        }
String currentDay=”“;
字符串currentdemine=“”;
字符串计数器=”;
LinkedHashMap itemsByCounter=新LinkedHashMap();
列表项=新建ArrayList();
while(eventType!=XmlResourceParser.END_文档){
字符串标记名=xmlData.getName();
blogPost=newhashmap();
开关(事件类型){
案例XmlResourceParser.START_标记:
if(标记名.equalsIgnoreCase(“日”)){
currentDay=xmlData.getAttributeValue(null,“名称”);
}
if(标记名.equalsIgnoreCase(“MEIN”)){
currentMine=xmlData.getAttributeValue(null,“名称”);
}
if(标记名.equalsIgnoreCase(“计数器”)&¤tDay.equalsIgnoreCase(day)&&CurrentDime.equalsIgnoreCase(Dine)){
计数器=xmlData.getAttributeValue(null,“名称”);
}
打破
案例XmlResourceParser.TEXT:
if(currentDay.equalsIgnoreCase(day)和&CurrentDine.equalsIgnoreCase(Dine)){
if(xmlData.getText().trim().length()>0){
add(xmlData.getText());
}
}
打破
case XmlPullParser.END_标记:
if(标记名.equalsIgnoreCase(“计数器”)){
如果(items.size()>0){
itemsByCounter.put(计数器,项目);
items=newarraylist();
}
}
打破
}
eventType=xmlData.next();
}
抱歉,它很长,但基本上它只是抓取项目,添加一个作为键,然后将其下的项目作为值

下面是我如何用数据填充ListView

MyCustomAdapter mAdapter = new MyCustomAdapter();
    for (int i = 0; i < data.size(); i++) {
        Object[] array = data.keySet().toArray();
        String string = array[i].toString();
        mAdapter.addSeparatorItem(string);
        for (int i2 = 0; i2 < data.get(string).size(); i2++) {
            mAdapter.addItem(data.get(string).get(i2));
        }

    }
public class MyCustomAdapter extends BaseAdapter{
    ArrayList mData = new ArrayList();
    LayoutInflater mLayoutInflater;

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

    private TreeSet mSeparatorsSet = new TreeSet();

    public MyCustomAdapter() {
        mLayoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    public void addSeparatorItem(final String item) {
        mData.add(item);
        // save separator position
        mSeparatorsSet.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position).toString();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        int type = getItemViewType(position);
        System.out.println("getView " + position + " " + convertView + " type = " + type);
        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
                case TYPE_ITEM:
                    convertView = mLayoutInflater.inflate(R.layout.item_cell_view, parent, false);
                    holder.textView = (TextView)convertView.findViewById(R.id.text);
                    break;
                case TYPE_SEPARATOR:
                    Context context = DiningItemsActivity.this;
                    convertView = mLayoutInflater.inflate(R.layout.header_cell_view, parent, false);
                    holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
                    convertView.setBackgroundColor(colorBar);
                    break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.textView.setText(mData.get(position).toString());
        return convertView;
    }

    public class ViewHolder {
        public TextView textView;
    }
MyCustomAdapter mAdapter=new MyCustomAdapter();
对于(int i=0;i