Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7911727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:21:22+00:00 2026-06-03T13:21:22+00:00

I find that when using java collections, especially when writing utility methods using generics,

  • 0

I find that when using java collections, especially when writing utility methods using generics, that my code is often ugly and bloated, full of null checks, nested loops, and repetition. Focusing on this one example, I’d like ideas for improvement.

Assume we have an EnumMap whose values are lists of ratings. For example, say the enums themselves represent fruit, and each value represents a list ratings given by different people.

APPLE  -> [1,   3,   4] 
ORANGE -> [2,   0,   5]

John rated apple 1, Mary rated apple 3, Steve rated apple 4
John rated orange 2, Mary rated orange 0, Steve rated orange 5
Note the specific names are irrelevant and provided only to clarify the setup

Now we want to write a utility method that accepts a data structure like the one above, and returns a list of each person’s favorite fruit. Thus the expected result for the above sample data would be: [ORANGE, APPLE, ORANGE since 2 > 1, 3 > 0, and 5 > 4.

Below is my current method for doing this. I’d like an equally (or more) efficient, but cleaner, way to write the same algorithm.

Thanks!

public class MyListUtil {

    public static <K extends Enum<K>, T extends Object & Comparable<? super T>> List<K> maxKeysByIndex(EnumMap<K, List<T>> enumMap) {
        Iterator<K> keysIter = enumMap.keySet().iterator();
        int sizeOfAllLists = enumMap.get(keysIter.next()).size();
        List<K> ret = new ArrayList<K>();

        for (int i=0; i<sizeOfAllLists; i++) {
            keysIter = enumMap.keySet().iterator();
            K maxIndexKey = null;
            T maxIndexVal = null;

            while (keysIter.hasNext()){
                K curKey = keysIter.next();
                T curVal = enumMap.get(curKey).get(i);
                if (maxIndexVal == null || curVal.compareTo(maxIndexVal) > 0) {
                    maxIndexVal = curVal;
                    maxIndexKey = curKey;
                }
            }
            ret.add(maxIndexKey);
        }

        return ret;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:21:27+00:00Added an answer on June 3, 2026 at 1:21 pm

    If you need a lot of methods that all operate on the same generic types, I think you could put the helper methods for K and T into a class, and then only specify the full generic type for the class as a whole. To use them you would create an object of that class, and then call the methods from it.

    The object would be stateless, but it gives you a syntactic way to put all the verbosity into a single place.

    public class <K extends Enum<K>, T extends Object & Comparable<? super T>> MyListUtil {
    
        public List<K> maxKeysByIndex(EnumMap<K, List<T>> enumMap) {
            ...
        //other methods
    }
    

    You could try putting the inner loop into a separate method, say:

    public K getMaxKeyFromPos(EnumMap<K, List<T>> enumMap, int pos)
    {
        K maxIndexKey = null;
        T maxIndexVal = null;
    
        for (K curKey : enumMap.keySet()) {
             T curVal = enumMap.get(curKey).get(pos);
             if (maxIndexVal == null || curVal.compareTo(maxIndexVal) > 0) {
                 maxIndexVal = curVal;
                 maxIndexKey = curKey;
             }
        }
        return maxIndexKey;
    }
    

    I’ve also changed it to for-each syntax, removing some of the iterator cruft.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Perl script (in Windows) that is using File::Find to index
-o changes the output filename (I found that using --help) But I can't find
I've only been using SSIS briefly, but I find that my complaints are numerous.
When I wait on a non-signaled Event using the WaitForSingleObject function, I find that
Possible Duplicate: Comparing Two Arrays Using Perl I am trying to find elements that
Trying to find an example that has css rollover using sprites & sliding door
In most examples that you find on the web when explicitly not using using,
I need to find some strings that the current version of Windows is using.
Below you will find my current vHost entry that I am using for a
Using recursion, find an index that cuts an array in two parts so that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.