簡體   English   中英

如何將值從一個列表復制到另一個具有不同對象的列表

[英]How to copy value from one list to another list having different objects

我有MaterailInfo和StyleInfo,我想基於與MaterialNumber匹配的StyleNumber設置styleDescription。 我正在使用2 for循環,還有其他解決方案嗎?

MaterailInfo:

class MaterailInfo {
    private String materialNumber;
    private String materialDescription;

    public MaterailInfo(String materialNumber, String materialDescription) {
        this.materialNumber = materialNumber;
        this.materialDescription = materialDescription;
    }

    // getter setter methods

}

StyleInfo:

class StyleInfo {
    private String StyleNumber;
    private String styleDescription;

    public StyleInfo(String styleNumber, String styleDescription) {
        StyleNumber = styleNumber;
        this.styleDescription = styleDescription;
    }

    // getter setter toString methods

}

TEST12:

public class TEst12 {

    public static void main(String[] args) {
        List<MaterailInfo> mList = new ArrayList<MaterailInfo>();
        mList.add(new MaterailInfo("a", "a-desc"));
        mList.add(new MaterailInfo("b", "b-desc"));
        mList.add(new MaterailInfo("c", "c-desc"));

        List<StyleInfo> sList = new ArrayList<StyleInfo>();
        sList.add(new StyleInfo("a", ""));
        sList.add(new StyleInfo("b", ""));
        sList.add(new StyleInfo("c", ""));

        for (MaterailInfo m : mList) {
            for (StyleInfo s : sList) {
                if (s.getStyleNumber().equals(m.getMaterialNumber())) {
                    s.setStyleDescription(m.getMaterialDescription());
                }
            }
        }

        System.out.println(sList);
    }
}

如果使用Map而不是List來存儲數據,則可以只執行一個循環即可:

Map<String, String> mMap = new HashMap<String, String>();
mMap.put("a", "a-desc");
mMap.put("b", "b-desc");
mMap.put("c", "c-desc");

Map<String, String> sMap = new HashMap<String, String>();
sMap.put("a", "");
sMap.put("b", "");
sMap.put("c", "");

for (Map.Entry<String, String> entry : mMap.entrySet()) {
    sMap.put(entry.getKey(), mMap.get(entry.getKey());
}

如果樣式編號與任何已知的材料編號不匹配,則此代碼會將樣式說明保留為空。

如果您的數字不能重復,那么使用HashMap而不是類可能會更快一些。

import java.io.*;
import java.util.*;
import java.util.Map.Entry;


public class Demo {

    public static void main(String[] args) {

        HashMap<String, String> mList = new HashMap();
        HashMap<String, String> sList = new HashMap();
        mList.put("a", "a-desc");
        mList.put("b", "b-desc");
        mList.put("c", "c-desc");
        sList.put("a", "");
        sList.put("b", "");
        sList.put("c", "");

        Iterator entries = sList.entrySet().iterator();
        while (entries.hasNext()) {
            Entry entry = (Entry) entries.next();
            if (mList.containsKey(entry.getKey())) {
                sList.put((String) entry.getKey(), mList.get(entry.getKey()));
            }
        }

        for (Map.Entry<String, String> entry : sList.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }

    }
}

您可以使用一個for loop來執行此操作

for (int i = 0; i < mList.size(); i++) {
   sList.get(i).setStyleDescription(mList.get(i).getMaterialDescription());  
}

注意:我假設您在規模方面有平衡的清單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM