根据比较对象的变量将两个列表合并。

huangapple go评论74阅读模式
英文:

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 还应具有以下属性:

  1. C 中元素的位置应使得来自 A 和 B 的元素进行比较,具有相同航空公司的元素相邻放置。例如,合并列表 = A.united, B.united, A.emirates, B.emirates,依此类推。通过匹配,我的意思是将元素放在彼此之后。

  2. 完成步骤 1 后,应查找之前未添加到 C 中的 A 和 B 中的元素。将所有这些元素与另一个列表中的所有元素进行匹配。例如,如果之前没有将 a(5) 添加到列表中,则它将与 b(1-5) 进行匹配。

  3. 然后,对 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&lt;Flights&gt;a
    list&lt;Flights&gt;b 
    list&lt;Flights&gt;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

  1. 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

  1. 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)

  2. 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&lt;Flights&gt; secondaryFlights  // filled a
List&lt;Flights&gt; primaryFlights    // filled b
List&lt;Flights&gt; 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[] = { &quot;A&quot;, &quot;E&quot;, &quot;I&quot; };
  String b[] = { &quot;O&quot;, &quot;U&quot; };
  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&lt;String&gt;list1 = new ArrayList&lt;String&gt;();
	list1.add(&quot;tommy&quot;);
	list1.add(&quot;Alex&quot;);
	list1.add(&quot;Bryan&quot;);
	list1.add(&quot;Tom&quot;);
	list1.add(&quot;William&quot;);
	list1.add(&quot;Willy&quot;);

	List&lt;String&gt;list2 = new ArrayList&lt;String&gt;();
	list2.add(&quot;Alex&quot;);
	list2.add(&quot;William&quot;);
	list2.add(&quot;Candice&quot;);
	list2.add(&quot;Jane&quot;);
	list2.add(&quot;Parker&quot;);
	list2.add(&quot;tommy&quot;);
	
	List&lt;String&gt;merged = new ArrayList&lt;String&gt;();
	
	for ( String str1 : list1) {
		for ( String str2 : list2) {
			if ( str1.equals(str2)) {
				merged.add(&quot;[ &quot; + str1);
				merged.add(str2 + &quot;]&quot;);
			}
		}
	}
	for ( String str1 : list1) {
		for ( String str2 : list2) {
			if ( !str1.equals(str2)) {
				merged.add(&quot;[ &quot; + str1);
				merged.add(str2 + &quot;]&quot;);

		}
	}
		}
	
	System.out.println(merged);

答案3

得分: 0

看完你的例子后,你首先需要匹配一次,然后按顺序列出所有不匹配的项,你可以按照以下方式操作:

  • 创建第二个列表的一个set,然后循环遍历第一个列表。如果第一个列表中的元素存在于该set中,就将它们添加到结果中。

  • 对于不匹配的项,创建一个新的set以避免重复的结果。如果这对元素是未访问过的,你可以将其添加到结果中。你可以以方便且不在任何情况下重叠的方式创建这对元素。以你的1,2,32,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 and 2,3,10 example, I have separated integers by a pipe symbol(|). Java does seem to have Pair class too. You can use it if you like.

Snippet:

private static List&lt;Integer&gt; getData(List&lt;Integer&gt; l1,List&lt;Integer&gt; l2){
    List&lt;Integer&gt; res = new ArrayList&lt;&gt;();
    Set&lt;Integer&gt; set = new HashSet&lt;&gt;(l2);
    for(int i=0;i&lt;l1.size();++i){
        int x = l1.get(i);
        if(set.contains(x)){
            res.add(x);
            res.add(x);
        }
    }
    Set&lt;String&gt; pairs = new HashSet&lt;&gt;();
    for(int i=0;i&lt;l1.size();++i){
        int x = l1.get(i);
        for(int j=0;j&lt;l2.size();++j){
            int y = l2.get(j);
            int min = Math.min(x,y);
            int max = Math.max(x,y);
            if(x != y &amp;&amp; !pairs.contains(min + &quot;|&quot; + max)){
                res.add(x);
                res.add(y);
                pairs.add(min + &quot;|&quot; + max);
            }       
        }
    }        
    
    return res;
}

Driver code:

public static void main(String[] args) {
        List&lt;Integer&gt; l1 = Arrays.asList(1,2,3);
        List&lt;Integer&gt; 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.

huangapple
  • 本文由 发表于 2020年7月24日 01:46:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63060203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定