[英]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) 我有两个列表,每个列表包含来自sql查询的行,包含2个元素(列)运算符和id我想用两个列表映射运算符并从列表中打印id(两个列表中的id对应于运算符)
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. 而不是List3创建一个地图作为目标源。 Map will have operator as key and comma separated ids as value.
Map将以操作符作为键,以逗号分隔的ID作为值。
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. 最简单的方法是使用Map按运算符分组。实际上,您可以使用Map而不是List,但您也可以迭代map以附加键和值并将它们插回列表。 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. 方法
getOperator( String )
和getId( String )
显而易见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.