英文:
Combine 2 lists according to compared object's variable
问题
所以我有两个对象列表 A 和 B。我将它们合并成一个列表 C,该列表是从 Flights 类中获取的。
public class Flights {
String airline;
}
其中,
列表如下:
List<Flights> a;
List<Flights> b;
List<Flights> c;
合并后的列表应该始终从 A 和 B 中交替取元素。
例如:c = a(1), b(1), a(3), b(4)。C 还应具有以下属性:
-
C 中元素的位置应使得来自 A 和 B 的元素进行比较,具有相同航空公司的元素相邻放置。例如,合并列表 = A.united, B.united, A.emirates, B.emirates,依此类推。通过匹配,我的意思是将元素放在彼此之后。
-
完成步骤 1 后,应查找之前未添加到 C 中的 A 和 B 中的元素。将所有这些元素与另一个列表中的所有元素进行匹配。例如,如果之前没有将 a(5) 添加到列表中,则它将与 b(1-5) 进行匹配。
-
然后,对 A 中的每个元素进行遍历,并确保与 B 中的所有元素匹配。
示例:
假设 A 有 1、2、3,B 有 2、3、10,则输出为 [2,2],[3,3],[1,2],[1,3],[1,10],[2,3],[2,10],[3,10]。
括号表示每个匹配。
我的当前代码:
List<Flights> secondaryFlights; // 填充了 a
List<Flights> primaryFlights; // 填充了 b
List<Flights> flightsList; // 空的 c
for (Flights p : primaryFlights) {
for (Flights s : secondaryFlights) {
if (p.getAirline().equals(s.getAirline())) {
flightsList.add(p);
flightsList.add(s);
}
}
}
while (!flightsList.containsAll(primaryFlights) || !flightsList.containsAll(secondaryFlights)) {
}
英文:
So I have got 2 object lists A and B. I will call merged list c. That is from the class Flights.
public class Flights{
String airline;
}
Where
The list looks like this
list<Flights>a
list<Flights>b
list<Flights>c
The combined list should always have alternating element from A and B.
e.g. c = a(1), b(1), a(3), b(4). C also should have the following properties
- The position of elements in C should be such that elements in A are compare to B where elements that have the same airline is placed beside one another. E.g. merged list = A.united, B.united, A.emirates, B.emirates and so on
By matching I mean placing the elements after the other
-
After completing step 1, it should then find the elements in A and B that were previously not added to C. Match all those elements to all elements from the other list. E.g. if a(5) wasn't added to the list previously, it will be matched with b(1-5)
-
Then, go through each element in A and make sure there is a match with all elements in B.
Example
Say A has 1,2,3 and B has 2,3,10, then the output as [2,2],[3,3],[1,2],[1,3],[1,10],[2,3],[2,10],[3,10]
The brackets mean each match.
My current code
List<Flights> secondaryFlights // filled a
List<Flights> primaryFlights // filled b
List<Flights> flightsList // Empty c
for ( Flights p : primaryFlights) {
for ( Flights s : secondaryFlights) {
if ( p.getAirline().equals(s.getAirline())) {
flightsList.add(p);
flightsList.add(s);
}
}
}
while( !flightsList.containsAll(primaryFlights) || !flightsList.containsAll(secondaryFlights)) {
}
答案1
得分: 0
获取更多理解:
String a[] = { "A", "E", "I" };
String b[] = { "O", "U" };
List list = new ArrayList(Arrays.asList(a));
list.addAll(Arrays.asList(b));
Object[] c = list.toArray();
System.out.println(Arrays.toString(c));
英文:
Get more understanding from
String a[] = { "A", "E", "I" };
String b[] = { "O", "U" };
List list = new ArrayList(Arrays.asList(a));
list.addAll(Arrays.asList(b));
Object[] c = list.toArray();
System.out.println(Arrays.toString(c));
答案2
得分: 0
Fixed with something like this
List<String> list1 = new ArrayList<String>();
list1.add("tommy");
list1.add("Alex");
list1.add("Bryan");
list1.add("Tom");
list1.add("William");
list1.add("Willy");
List<String> list2 = new ArrayList<String>();
list2.add("Alex");
list2.add("William");
list2.add("Candice");
list2.add("Jane");
list2.add("Parker");
list2.add("tommy");
List<String> merged = new ArrayList<String>();
for (String str1 : list1) {
for (String str2 : list2) {
if (str1.equals(str2)) {
merged.add("[ " + str1);
merged.add(str2 + "]");
}
}
}
for (String str1 : list1) {
for (String str2 : list2) {
if (!str1.equals(str2)) {
merged.add("[ " + str1);
merged.add(str2 + "]");
}
}
}
System.out.println(merged);
英文:
Fixed with something like this
List<String>list1 = new ArrayList<String>();
list1.add("tommy");
list1.add("Alex");
list1.add("Bryan");
list1.add("Tom");
list1.add("William");
list1.add("Willy");
List<String>list2 = new ArrayList<String>();
list2.add("Alex");
list2.add("William");
list2.add("Candice");
list2.add("Jane");
list2.add("Parker");
list2.add("tommy");
List<String>merged = new ArrayList<String>();
for ( String str1 : list1) {
for ( String str2 : list2) {
if ( str1.equals(str2)) {
merged.add("[ " + str1);
merged.add(str2 + "]");
}
}
}
for ( String str1 : list1) {
for ( String str2 : list2) {
if ( !str1.equals(str2)) {
merged.add("[ " + str1);
merged.add(str2 + "]");
}
}
}
System.out.println(merged);
答案3
得分: 0
看完你的例子后,你首先需要匹配一次,然后按顺序列出所有不匹配的项,你可以按照以下方式操作:
-
创建第二个列表的一个
set
,然后循环遍历第一个列表。如果第一个列表中的元素存在于该set
中,就将它们添加到结果中。 -
对于不匹配的项,创建一个新的
set
以避免重复的结果。如果这对元素是未访问过的,你可以将其添加到结果中。你可以以方便且不在任何情况下重叠的方式创建这对元素。以你的1,2,3
和2,3,10
的例子为例,我使用了竖线符号(|
)来分隔整数。Java 似乎也有Pair
类,如果喜欢的话可以使用它。
代码片段:
private static List<Integer> getData(List<Integer> l1, List<Integer> l2) {
List<Integer> res = new ArrayList<>();
Set<Integer> set = new HashSet<>(l2);
for (int i = 0; i < l1.size(); ++i) {
int x = l1.get(i);
if (set.contains(x)) {
res.add(x);
res.add(x);
}
}
Set<String> pairs = new HashSet<>();
for (int i = 0; i < l1.size(); ++i) {
int x = l1.get(i);
for (int j = 0; j < l2.size(); ++j) {
int y = l2.get(j);
int min = Math.min(x, y);
int max = Math.max(x, y);
if (x != y && !pairs.contains(min + "|" + max)) {
res.add(x);
res.add(y);
pairs.add(min + "|" + max);
}
}
}
return res;
}
驱动代码:
public static void main(String[] args) {
List<Integer> l1 = Arrays.asList(1, 2, 3);
List<Integer> l2 = Arrays.asList(2, 3, 10);
System.out.println(getData(l1, l2).toString());
}
注意: 你可以将相同的逻辑应用于你的Flight
类数据集。要比较字符串,你可以使用compareTo
方法。
英文:
Looking at your examples, you need matched once first and all unmatched ones in their order, so you can do like below:
-
Create a
set
of second list and loop over the first list. If element in first list exists in the set, add them. -
For unmatched ones, create a new set to avoid duplicating results. If the pair is un-visited, you can add it to the result. You can create the pair in a way it is convenient and doesn't overlap under any scenario. Taking your
1,2,3
and2,3,10
example, I have separated integers by a pipe symbol(|
). Java does seem to havePair
class too. You can use it if you like.
Snippet:
private static List<Integer> getData(List<Integer> l1,List<Integer> l2){
List<Integer> res = new ArrayList<>();
Set<Integer> set = new HashSet<>(l2);
for(int i=0;i<l1.size();++i){
int x = l1.get(i);
if(set.contains(x)){
res.add(x);
res.add(x);
}
}
Set<String> pairs = new HashSet<>();
for(int i=0;i<l1.size();++i){
int x = l1.get(i);
for(int j=0;j<l2.size();++j){
int y = l2.get(j);
int min = Math.min(x,y);
int max = Math.max(x,y);
if(x != y && !pairs.contains(min + "|" + max)){
res.add(x);
res.add(y);
pairs.add(min + "|" + max);
}
}
}
return res;
}
Driver code:
public static void main(String[] args) {
List<Integer> l1 = Arrays.asList(1,2,3);
List<Integer> l2 = Arrays.asList(2,3,10);
System.out.println(getData(l1,l2).toString());
}
Note: You can replicate the same logic for your Flight
class dataset. To compare strings, you can use compareTo
method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论