Question: Need help on a Java Problem. I'm new to Java. I encountered a problem and have no idea how to solve it. The picture below

Need help on a Java Problem. I'm new to Java. I encountered a problem and have no idea how to solve it.

The picture below is code in file that needs to fill [Intersection.java]

Need help on a Java Problem. I'm new to Java. I encountered

Following is the test in file[IntersectionTest.java]

import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; class IntersectionTest { @Test void should_not_contain_elements_that_exist_in_just_one_iterable() { List left = Arrays.asList(1, 2, 2, 3, 3, 3, 4, 4, 4, 4); List right = Arrays.asList(5, 4, 4, 3, 3, 3, 2, 2, 2, 2); final List intersection = Intersection.getIntersection(left, right); assertEquals(0, countAppearance(intersection, 1)); assertEquals(0, countAppearance(intersection, 5)); } @Test void should_get_correct_appearance() { List left = Arrays.asList(1, 2, 2, 3, 3, 3, 4, 4, 4, 4); List right = Arrays.asList(5, 4, 4, 3, 3, 3, 2, 2, 2, 2); final List intersection = Intersection.getIntersection(left, right); assertEquals(2, countAppearance(intersection, 2)); assertEquals(3, countAppearance(intersection, 3)); assertEquals(2, countAppearance(intersection, 4)); } }

The following code is in [IntersectionTestUtil.java]

import java.util.List; import java.util.Objects; class IntersectionTestUtil { public static  int countAppearance(List list, T item) { return (int)list.stream().filter(i -> Objects.equals(i, item)).count(); } }

import java.util.List; public class Intersection { /** *

Returns a {@Link List} containing the intersection of the given {@link Iterablets.

*

The appearance of each element in the returned {@link List} will be equal to the minimum * of the appearance of that element in the two given {@ink Iterable}s.

* * @param left the first collection, must not be null. * @param right the second collection, must not be null. * @param the generic type that is able to represent the types contained in both input collections. * @return the intersection of the two collections * @throws IllegalArgumentException if either collection is null */ public static List getIntersection final Iterable left, final Iterable right ) { // TODO: Please implement the method. // // TODO: You can add helper method if you want. // } // TODO: You can add helper class if you want. //

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!