简体   繁体   中英

merging 2 lists into 1 list by mapping

I Have two Lists each containing rows from sql query containing 2 elements(columns) operator and id i want to map operator with both the lists and prints id from both the lists(ids will be different corresponding to operator in both the lists)

operator can not be key in this situation.

    **List 1**            **Lists 2**
  operator1, id1       operator4, id7
  operator1, id2       operator3, id8
  operator2, id3       operator2, id9
  operator2, id4       operator2, id10
  operator3, id5       operator1, id11
  operator4, id6       operator1, id12

final output should be something like this

    **List3**
operator1, id1,id2,id11,id12
operator2, id3,id4,id9,id11
operator3, id5,id8
operator4, id6,id7

in what way can i implement?

Instead of List3 create a map as target source. Map will have operator as key and comma separated ids as value.

  • Read the content of list by iterating it.
  • Split the content with comma as a delimiter.
  • If operator is not present in map then add it else read the value, append the id and put it back in the map.
  • In the end it is just a matter of iterating the map to print what you need.

Easiest way is to use Map to group by operators.Actually you can use Map instead of List but you can also iterate through map to append key and values and insert them back to the list. You can use the following code to achieve the result you want.

public static void main(String[] args) {
    List<Object> list1 = new ArrayList<Object>();
    list1.add("operator1, id1");
    list1.add("operator1, id2");
    list1.add("operator2, id3");
    list1.add("operator2, id4");
    list1.add("operator3, id5");
    list1.add("operator4, id6");

    List<Object> list2 = new ArrayList<Object>();
    list2.add("operator4, id7");
    list2.add("operator3, id8");
    list2.add("operator2, id9");
    list2.add("operator2, id10");
    list2.add("operator1, id11");
    list2.add("operator1, id12");

    Map<String, String> map = new HashMap<String, String>();
    List<Object> list3 = new ArrayList<Object>();

    Iterator<Object> it1 = list1.iterator();
    Iterator<Object> it2 = list2.iterator();

    while (it1.hasNext() && it2.hasNext()) {
        String[] line1 = ((String) it1.next()).split(",");
        map.put(line1[0], map.get(line1[0]) == null ? line1[1] : map.get(line1[0]) + ", " + line1[1]);

        String[] line2 = ((String) it2.next()).split(",");
        map.put(line2[0], map.get(line2[0]) == null ? line2[1] : map.get(line2[0]) + ", " + line2[1]);
    }

    for (Map.Entry<String, String> entry : map.entrySet()) {
        String item = entry.getKey() + "," + entry.getValue();
        System.out.println(item);
        list3.add(item);
    }
}

After running the code above the result will be :

operator4, id6,  id7
operator1, id1,  id2,  id11,  id12
operator3, id5,  id8
operator2, id3,  id4,  id9,  id10

A slightly more compact and perhaps useful approach:

Map<String, List<String>> map = new HashMap<String, List<String>>();
for ( List<String> list : lists ) // List1 and List2 specifically
{
    for ( String s : list )
    {
        if ( map.get( getOperator( s ) ) == null ) 
        { 
            map.put( getOperator( s ) , new ArrayList<String>() ); 
        }
        map.get( getOperator( s ) ).add( getId( s );
    }
}

map.entrySet(); // will be a more useful version of your List3

The methods getOperator( String ) and getId( String ) does the obvious.

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