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 3323718
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:20:09+00:00 2026-05-17T23:20:09+00:00

I want to create a very generic utility method to take any Collection and

  • 0

I want to create a very generic utility method to take any Collection and convert it into a Collection of a user selectable class that extends from Number (Long, Double, Float, Integer, etc.)

I came up with this code that uses Google Collections to transform the Collection and to return an Immutable List.

import java.util.List;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
/**
     * Takes a {@code List<String>} and transforms it into a list of the
     * specified {@code clazz}.
     * 
     * @param <T>
     * @param stringValues
     *            the list of Strings to be used to create the list of the
     *            specified type
     * @param clazz
     *            must be a subclass of Number. Defines the type of the new List
     * @return
     */
    public static <T extends Number> List<T> toNumberList(List<String> stringValues, final Class<T> clazz) {
        List<T> ids = Lists.transform(stringValues, new Function<String, T>() {
            @SuppressWarnings("unchecked")
            @Override
            public T apply(String from) {
                T retVal = null;
                if (clazz.equals(Integer.class)) {
                    retVal = (T) Integer.valueOf(from);
                } else if (clazz.equals(Long.class)) {
                    retVal = (T) Long.valueOf(from);
                } else if (clazz.equals(Float.class)) {
                    retVal = (T) Float.valueOf(from);
                } else if (clazz.equals(Double.class)) {
                    retVal = (T) Double.valueOf(from);
                } else {
                    throw new RuntimeException(String.format("Type %s is not supported (yet)", clazz.getName()));
                }
                return retVal;
            }
        });
        return ImmutableList.copyOf(ids);
    }

It can be used like this:

// Convert List<String> to List<Long>
List<Long> ids = MiscUtils.toNumberList(productIds, Long.class);

Is my code overkill or how would you simplify it and at the same time keep it generic enough?

  • 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-05-17T23:20:09+00:00Added an answer on May 17, 2026 at 11:20 pm

    I think the most important aspect of this code is the Function as opposed to the method itself. I also don’t think it makes sense to switch over the subclasses you allow in the Function body, as you already know what type of Number you want to return at the time the Function is created. It’s also slightly problematic that your method fails if given, say, BigInteger.class.

    Given this, what I would do is create a utility class (let’s call it Numbers) and provide methods on it that each return a Function (which can be an enum singleton) for parsing a String as a specific type of Number. That is:

    public class Numbers {
      public static Function<String, Integer> parseIntegerFunction() { ... }
      public static Function<String, Long> parseLongFunction() { ... }
      ...
    }
    

    They could each be implemented something like this:

    public static Function<String, Integer> parseIntegerFunction() {
      return ParseIntegerFunction.INSTANCE;
    }
    
    private enum ParseIntegerFunction implements Function<String, Integer> {
      INSTANCE;
    
      public Integer apply(String input) {
        return Integer.valueOf(input);
      }
    
      @Override public String toString() {
        return "ParseIntegerFunction";
      }
    }
    

    This can then be used however users want:

    List<String> strings = ...
    List<Integer> integers = Lists.transform(strings, Numbers.parseIntegerFunction());
    

    This approach has quite a few advantages over yours:

    • Doesn’t require any switching in the Function… we know what type of number we’re creating and just do that. Faster.
    • Is more flexible, in that each Function can be used wherever… users aren’t forced to use it the way your method does (copying the transformed values into an ImmutableList.
    • You only create the Functions you actually want to allow. If there’s no BigInteger parsing function, users just can’t call that, as opposed to having it be completely legal to do that at compile time and then fail at runtime like in your example.

    As a side note, I’d recommend making the return type of any method that returns an ImmutableList be ImmutableList rather than List… it provides information that is useful to clients of the method.

    Edit:

    If you really need something more dynamic (i.e. you want classes that have an instance of some Class<T extends Number> to be able to transform Strings to that Number type) you could also add a lookup method like:

    public static <T extends Number> Function<String, T> parseFunctionFor(Class<T> type) {
      // lookup the function for the type in an ImmutableMap and return it
    }
    

    This has the same problems as your original method, though, if there’s a Number subclass that you don’t provide a Function for. It also doesn’t seem like there would be many situations where this would be useful.

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

Sidebar

Related Questions

I want to create a simple http proxy server that does some very basic
I want to create a very basic 3D modeling tool. The application is supposed
I want to create a very simple HTML/AJAX based GUI for a Python program.
I want to create a function that performs a function passed by parameter on
I want create a drop shadow around the canvas component in flex. Technically speaking
I want to create a Java application bundle for Mac without using Mac. According
I want to create a client side mail creator web page. I know the
I want to create a draggable and resizable window in JavaScript for cross browser
I want to create an allocator which provides memory with the following attributes: cannot
I want to create my Rails application with MySQL, because I like it so

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.