JAVA Question
a) Write a generic method
public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col)
in a Utils class that takes as parameter a collection of
Pair<K,V> objects
and returns a new collection object (use ArrayList<...>) with
the pair elements from collection col
sorted in ascending order. For comparing pairs, the K type must
implement Comparable<....>
Use the proper type constraints.
b)
Write a main() function in Utils.java that tests the
sortPairCollection() with K=String and V=Integer.
import javafx.util.Pair;
import java.util.ArrayList;
import java.util.Collection;
public class Utils {
public static <K extends Comparable<K>, V> Collection<Pair<K, V>> sortPairCollection(Collection <Pair<K, V>> col) {
Collection<Pair<K, V>> sorted = new ArrayList<>();
while (!col.isEmpty()) {
Pair<K, V> min = null;
for(Pair<K, V> pair: col) {
if(min == null || pair.getKey().compareTo(min.getKey()) < 0) {
min = pair;
}
}
sorted.add(min);
col.remove(min);
}
return sorted;
}
public static void main(String[] args) {
Collection<Pair<String, Integer>> collection = new ArrayList<>();
collection.add(new Pair<>("ronaldo", 7));
collection.add(new Pair<>("messi", 10));
collection.add(new Pair<>("bale", 11));
collection.add(new Pair<>("isco", 23));
Collection<Pair<String, Integer>> sorted = sortPairCollection(collection);
System.out.println(sorted);
}
}
![[bale-11, isco-23, messi-10, ronaldo-7] Process finished with e xit code 0](http://img.homeworklib.com/questions/2962ae60-286d-11eb-aa22-37a19ce3591c.png?x-oss-process=image/resize,w_560)
JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a...
*Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...
java Write a generic method that takes an ArrayList of objects (of a valid concrete object type) and returns a new ArrayList containing no duplicate elements from the original ArrayList. The method signature is as follows: public static ArrayList UniqueObjects(ArrayList list) Test the method with at least 2 array lists of different concrete object types. Label your outputs properly and show the array lists content before and after calling method UniqueObjects
JAVA Write a static method that takes an array of a generic type as its only argument. The method should display the array and return the number of elements in the array. Test the method in main with at least three arrays of objects. SAMPLE OUTPUT Here is an Integer array 12 21 7 16 8 13 That array held 6 elements Here is a String array one two three four That array held 4 elements Here is a Double...
//Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a test program that does the following operations: 4. Creates an empty ArrayList of Integer elements; 5. Generates 20 integer values, drawn at random between 0 and 9 (included), and adds them to the array list; 6. Calls the method sort and prints both the original list, and the sorted one.
23.8 (Generic insertion sort) Write the following two generic methods using insertion sort. The first method sorts the elements using the Comparable interface, and the second uses the Comparator interface.(Use Java program) public static > void insertionSort(E[] list) public static void insertionSort(E[] list, Comparator comparator)
JAVA - Given two List objects (input and output), write a generic method that 1) clears output list if it contains any element, and 2) copies every element of input list to the output list in the same order and position. Note that the type of output list can be any super type of the input list (e.g. type(output)=Numeric & type(input)=Double). import java.util.List; import java.util.ArrayList; public class Collections { // Modify method signature if needed & implement the body of...
A java exercise please!
Write a static method named swapHavles that takes an ArrayList of integers as a a parameter and returns a new ArrayList that has every element in the second half of the original ArrayList swapped with every element in the first half of the original ArrayList. Note: • You can assume that the ArrayList passed to this method as a parameter always contains an even number of elements and will always contain at least two elements. For...
Please Do It With Java. Make it copy paste friendly so i can
run and test it.
Thnak You.
Write programs for challenges 1 and 2 and 6 at the end of the chapter on Generics. 1) Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of T. Write a public method named add, which...
this is a generic class in java how implement sort method public class ArrayBag<T> { private T[] data; private int manyItems; // TODO change and implement this method to use the generic type // Sort the elements based on the comparator passed to this method // You must use Selection Sort algorithm public void sort(Comparator<T> comp) { } }
Please use java language in an easy way and comment as much as you can! Thanks (Write a code question) Write a generic method called "findMin" that takes an array of Comparable objects as a parameter. (Use proper syntax, so that no type checking warnings are generated by the compiler.) The method should search the array and return the index of the smallest value.