简体   繁体   中英

combine two different size lists to a Map

Iam trying to combine two lists in to one MAP. Two list sizes are not equal.

Map<String, String> finalMap = new HashMap<String, String>();
  List<String> names = new ArrayList<String>();

names list contains seloger ,france24,sports,tupple,hentry

List<String> location = new ArrayList<String>();

location list contains the values spain ,france only. i am trying to create a map like this.

finalMap.put("seloger", "spain" );
finalMap.put("france24", "france" );
finalMap.put("sports", null );
finalMap.put("tupple", null );
finalMap.put("hentry", null);

Here is the code i used

Iterator<String> names = names .iterator();
        Iterator<String> locations= location .iterator();
        for (names .hasNext();;) {
            String tag = locations.next();
            tag = null != tag ? tag : null;
            finalMap .put(names .next(), tag);
        }

Here am not able to insert to finalMap since am getting nosuchElementException .How can in insert null values for locations.next();contains no values

You should check if locations iterator has next element

if (locations.hasNext()) {
    tag = locations.next();
} else {
    tag = null;
}

尝试像这样插入

finalMap.put("sports", "");

Since every entry to a Map has to have a key but not a value, you might as well just iterate until names is done and then stop, since further values without keys can't be stored:

for (int i = 0; i < names.size(); i++) {
    finalMap.put(names.get(i), i < location.size() ? location.get(i) : null);
}

In case either of names or locations can be short:

    Map<String, String> finalMap = new HashMap<String, String>();

    List<String> name = new ArrayList<String>();
    List<String> location = new ArrayList<String>();

    // ....

    Iterator<String> names = name.iterator();
    Iterator<String> locations= location.iterator();

    while (names.hasNext() && locations.hasNext()) {
        finalMap.put(names.next(), locations.next());
    }
    while (names.hasNext()) {
        finalMap.put(names.next(), null);
    }
    while (locations.hasNext()) {
        finalMap.put(null, locations.next());
    }
public static void main(String[] args) {
    List<String> names = Arrays.asList("seloger", "france24", "sports", "tupple", "hentry");
    List<String> location = Arrays.asList("spain", "france");

    Map<String, String> map = new HashMap<String, String>();
    Iterator<String> i = location.iterator();
    for (String name : names) {
        String value = i.hasNext() ? i.next() : null;
        map.put(name, value);
    }

    System.out.println(map);
}

Ideally, you would have a zip or zipWith to combine the two lists. Unfortunately, JDK 8 doesn't seem to have that, so you have to do imperative code.

I ran the above code. It gives you the result that you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM