2787184
2787184

Reputation: 3881

How to copy value from one list to another list having different objects

I have MaterailInfo and StyleInfo, I want to set styleDescription based on StyleNumber matching with materialNumber. I am using 2 for loops, is there any alternative solution?

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

Upvotes: 1

Views: 56

Answers (3)

Muhammad Suleman
Muhammad Suleman

Reputation: 2922

You can do this using one for loop like this

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

Note: i am assuming you have balanced lists in term of size.

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

If you use a Map instead of a List to store your data, you can get away with doing only a single loop:

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

This code will leave the style description empty if the style number does not match any known material number.

Upvotes: 1

Uma Kanth
Uma Kanth

Reputation: 5629

If your numbers can't have duplicates, using a HashMap instead of classes can be a bit faster.

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

    }
}

Upvotes: 0

Related Questions