2 つの文字列配列リストを取り込み、どちらか一方のリストにあるが両方にはない値のリストを出力するメソッドを作成しようとしています。重複は許可されていません。
これは私がこれまでに持っているものですが、3つのjunitテストに失敗しています
public static ArrayList<String> getDifference(ArrayList<String> one, ArrayList<String> two)
{
Set<String> oneSet = new LinkedHashSet<>(one);
ArrayList<String> finalone = new ArrayList<>(oneSet);
Set<String> twoSet = new LinkedHashSet<>(two);
ArrayList<String> finaltwo = new ArrayList<>(twoSet);
Collection<String> result = new ArrayList<String>(finaltwo);
result.removeAll(finalone);
ArrayList<String> list = new ArrayList<String>(result);
return list;
}
私が失敗しているテストは、これを修正するために何ができるかについての助けにはなりません。よろしくお願いします。
@Test
public void testGetDifferenceWithEmptyListSecond() {
String[] un = { "luke", "leah", "han" };
String[] duex = { };
ArrayList<String> one = new ArrayList<String>( Arrays.asList( un ));
ArrayList<String> two = new ArrayList<String>( Arrays.asList( duex ));
ArrayList<String> actual = Lab03Two.getDifference( one, two );
assertEquals( "The number of elements is incorrect", 3, actual.size() );
assertTrue ( "The value \"luke\" was not found in the result", actual.contains( "luke" ));
assertTrue ( "The value \"leah\" was not found in the result", actual.contains( "leah" ));
assertTrue ( "The value \"han\" was not found in the result", actual.contains( "han" ));
}
@Test
public void testGetDifferenceWithOverlapAndDuplicates() {
String[] un = { "palpatine", "dooku", "vader", "sidius" };
String[] duex = { "padme", "vader", "sidius", "ackbar", "padme" };
ArrayList<String> one = new ArrayList<String>( Arrays.asList( un ));
ArrayList<String> two = new ArrayList<String>( Arrays.asList( duex ));
ArrayList<String> actual = Lab03Two.getDifference( one, two );
assertEquals( "The number of elements is incorrect", 4, actual.size() );
assertTrue ( "The value \"ackbar\" was not found in the result", actual.contains( "ackbar" ));
assertTrue ( "The value \"dooku\" was not found in the result", actual.contains( "dooku" ));
assertTrue ( "The value \"padme\" was not found in the result", actual.contains( "padme" ));
assertTrue ( "The value \"palpatine\" was not found in the result", actual.contains( "palpatine" ));
}
}
@Test
public void testGetDifferenceWithNoOverlap() {
String[] un = { "obi-wan", "jar-jar", "anakin" };
String[] duex = { "r2-d2", "c-3po" };
ArrayList<String> one = new ArrayList<String>( Arrays.asList( un ));
ArrayList<String> two = new ArrayList<String>( Arrays.asList( duex ));
ArrayList<String> actual = Lab03Two.getDifference( one, two );
assertEquals( "The number of elements is incorrect", 5, actual.size() );
assertTrue ( "The value \"obi-wan\" was not found in the result", actual.contains( "obi-wan" ));
assertTrue ( "The value \"jar-jar\" was not found in the result", actual.contains( "jar-jar" ));
assertTrue ( "The value \"anakin\" was not found in the result", actual.contains( "anakin" ));
assertTrue ( "The value \"r2-d2\" was not found in the result", actual.contains( "r2-d2" ));
assertTrue ( "The value \"c-3po\" was not found in the result", actual.contains( "c-3po" ));
}