简体   繁体   English

如何将列表元素添加到另一个列表?

[英]How to add elements of list to another list?

I have two sql queries, which returning lists. 我有两个sql查询,返回列表。 My code: 我的代码:

List<String> list = em.createNativeQuery(query1).getResultList();
list.addAll(em.createNativeQuery(query2).getResultList());

What I have: List with two sublists - sublist from query1 and sublist from query2. 我拥有:列出两个子列表 - 来自query1的子列表和来自query2的子列表。
What I need: One list with all elements from query1 and query2. 我需要的:一个包含来自query1和query2的所有元素的列表。
Any idea? 任何的想法?


Problem was in first sql query. 问题出现在第一个SQL查询中。 Result was adding in object array, therefore addAll method not solves my problem. 结果是在对象数组中添加,因此addAll方法无法解决我的问题。

addAll() will work. addAll()将起作用。 code is correct to get data from both the query 从两个查询中获取数据的代码是正确的

I created a similar situation to test what you were saying. 我创建了一个类似的情况来测试你在说什么。 Obviously I do not have access to your specific database to query, so I just focused on the point of your question; 显然我无法访问您的特定数据库进行查询,因此我只关注您的问题; the List combination. List组合。

List<String> foods = new ArrayList<String>(); //this is analogous with the list returned from your first query
foods.add("Beef");
foods.add("Chicken");
System.out.println("foods(query1):  "+foods);

List<String> fruits = new ArrayList<String>(); //this is analogous with the list returned from your second query
fruits.add("Apple");
fruits.add("Peach");
fruits.add("Pear");
System.out.println("fruits(query2):  "+fruits);


foods.addAll(fruits); //this combines the two lists (your two queries)
System.out.println("foods(after combination of queries):  "+foods);

The output was: 输出是:

foods(query1):  [Beef, Chicken]
fruits(query2):  [Apple, Peach, Pear]
foods(after combination of queries):  [Beef, Chicken, Apple, Peach, Pear]

My only thought as to why you are not getting that result would be that one of your queries is not returning anything... I would recommend trying to print out each list prior to adding them together as I did above and see if one is empty. 我唯一想到你没有得到这个结果的原因是你的一个查询没有返回任何内容......我建议尝试打印出每个列表,然后将它们添加到一起,就像我上面所做的那样,看看是否为空。

You actually want to create list of lists, ie: 您实际上想要创建列表列表,即:

List<List<String>> listOfLists = ...;

You cannot store both strings and lists of strings in the same list that is defined this way. 您不能将这些字符串和字符串列表存储在以这种方式定义的同一列表中。 If you indeed want to do this you should just define List<Object> and you will be able to store there elements of any type. 如果你确实想这样做,你应该只定义List<Object> ,你就可以存储任何类型的元素。 However it is highly not recommended. 但是,强烈建议不要这样做。 Generics have been introduced to java to prevent such usage at compile time. 已经在java中引入了泛型以防止在编译时使用这种用法。

So, what you probably really want is to creatate list of list of strings and then perform queries that return lists of strings and add result to the first list: 因此,您可能真正想要的是创建字符串列表列表,然后执行返回字符串列表的查询并将结果添加到第一个列表:

List<List<String>> list = new LinkedList<List<String>>();
list.addAll(em.createNativeQuery(query1).getResultList());
list.addAll(em.createNativeQuery(query1).getResultList());

BTW this approach is not recommended too. BTW也不推荐这种方法。 Typically if you want to create n-dimensional collection where n>1 you probably should create yet another container class that holds collection and other stuff and then create 1-dimensional collection that stores instances of this class. 通常,如果要创建n> 1的n维集合,您可能应该创建另一个容纳集合和其他东西的容器类,然后创建存储此类实例的一维集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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