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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:31:26+00:00 2026-06-09T13:31:26+00:00

This question is in response to another question by opensas : building a generic

  • 0

This question is in response to another question by opensas: building a generic initializer function in java

From his question it became clear that he needs to convert from any data type T1 to another type T2. When I say “data type” here, I mean types limited to those commonly used to represent raw data: Integer, String, Date, etc. For the purpose of this question we can consider primitives to be boxed.

I’m wondering if there is any API that supports conversion between types where both the input and output are generalized to a set of supported data types. I had a look at Apache Commons’ beanutils.converters package, but there’s a separate converter class for each known input. I’m looking for any functionality that implements something like the following signature:

static <IN, OUT> OUT convert(IN value, Class<OUT> targetType);

or else

static <IN, OUT> OUT convert(IN value, OUT defaultValue);

It really wouldn’t be too hard to implement this kind of mapping oneself, either using a bunch of else if blocks pointing to the various Commons Converters, or else a Map<Class<?>, Converter> for the same purpose. But I’m wondering if this kind of functionality is supported somewhere.

Also, if this winds up being a duplicate I apologize. I tried finding similar questions and was surprised when I found none matching this situation.

EDIT: so an example of this code in action would be:

Integer i = GenericConverter.convert("123", Integer.class);    //returns 123
Date d = GenericConverter.convert(1313381772316L, Date.class); //returns today's date
Boolean b = GenericConverter.convert(0, Boolean.class);        //returns false
Long l = GenericConverter.convert("asdf", Long.class);         //RuntimeException

UPDATE: The BalusC code I linked falls close to the mark, and Bohemian’s answer is a nice lightweight solution (although it doesn’t work for Boolean conversions). He’s also right that Dates should be probably be handled separately if we want to generalize conversion of these other data types. I’m still hoping for additional answers though – especially if there is more of a hands-off API available somewhere.

  • 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-09T13:31:28+00:00Added an answer on June 9, 2026 at 1:31 pm

    In JDK 8 this can be easily implemented with the new java.util.functions.Mapper interface and a lambda expression.

    Mapper<String,Integer> atoi = s -> Integer.valueOf(s);
    Integer r = atoi.map("10");
    

    Using method references it can be even simpler:

    Mapper<String, Integer> atoi = Integer::new;
    Integer r = atoi.map("10");
    

    Or things like:

    List<Long> dates = asList(1344754620310L,1344754854877L);
    List<Date> asDates = dates.map(Date::new).into(new ArrayList<Date>());
    

    Or cool conversions like:

    List<Integer> myInts = "5,4,3,2,1,0,6,7,8,9"
      .splitAsStream(",")
      .map(Integer::new)
      .into(new ArrayList<Integer>());
    

    In the current implementation of the JDK8 API, a few default mappers have been defined (i.e. LongMapper, IntMapper, DoubleMapper) and there’s a utility class called Mappers that defines some others like a string mapper, and identity mapper, a constant mapper, etc.

    I am not sure if this is what you are after, but certainly it must be a nice way to implement it.

    Cases like the one you suggest for:

    static <IN, OUT> OUT convert(IN value, Class<OUT> targetType);
    

    Can be implemented with the Mappers utility class:

    Mapper<String, Integer> atoi = Mappers.instantiate(String.class, Integer.class);
    Integer r = atoi.map("10");
    

    And your signature:

    static <IN, OUT> OUT convert(IN value, OUT default);
    

    Could be implemented with something like:

    Mapper<String, Integer> atoi = chain(substitute(null, "0"), Integer::new);
    Integer r = atoi.map(null); //produces 0
    

    As such, a code like this…

    List<String> data = asList("0", null, "2", null, "4", null, "6");
    List<Integer> myInts = data.map(chain(substitute(null, "0"), Integer::new)).into(new ArrayList<Integer>());
    System.out.println(myInts);
    

    Would yield: [0, 0, 2, 0, 4, 0, 6]

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

Sidebar

Related Questions

I have been looking at the answer to this question: Pulling details from response
In response to another question of mine, someone suggested that I avoid long lines
This question is like another , except that one is asked in the context
This is in response to this question in the answers section of another question.
For this question I'm going to quote another user who got no response to
This question is related to another one that I posted recently: Understanding HttpServletRequest and
This question and my answer below are mainly in response to an area of
I posted this question on Reddit Programming and did not get a single response.
I've asked this question on the Awesomium forms but I haven't received any response
I'd like to base this question on the basis of the response posted by

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.