Question

JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
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

Add a comment
Know the answer?
Add Answer to:
JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

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

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

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

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

  • A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers...

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

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

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

    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.

  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • JAVA (implementing a collection class) write a completed program (must include MAIN) and straight to the output based on 4. Based on the implementation of ArrayIntlist or ArrayList, write a class Sor...

    JAVA (implementing a collection class) write a completed program (must include MAIN) and straight to the output based on 4. Based on the implementation of ArrayIntlist or ArrayList, write a class SortedIntList or SortedList that provides most of the same operations but maintains its elements in sorted order. When a new value is added to the sorted list rather than appending it to the end of the list, it is placed in the appropriate index to maintain sorted order of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT