2

I will try my best to explain my problem and hopefully someone can help me.

Pair class:

public class Pair {
   String key;
   Class<?> value;
   public Pair(String key, Class<?> value){
      this.key = key;
      this.value = value;
   };
   // you have the setter and getter methods
}

Pairs class:

public class Pairs {
   Pair[] paris = new Pair[0];
   // you have the setter and getter methods
   public void addPair(Pair pair) {
      // assume it will add a pair to the array
   }
}

Problem: I need to load data from a database table. The column types are different here. There are BOOLEAN, VARCHAR, DATE and others. So I need to read and store the data with corresponding java type into the Pair object. How do you convert from generic type to String or Boolean? And how do you do the other way around?

I found an answer for converting generic type to String:

Class<?> value = getValue();
if (value.isInstance(String.class))
String newValue = (String)(Object) value; // is it correct?

Then how can I convert a String to Class< ?> and store the data into the arraylist? Because I want to create:

Pair pair = new Pair("name", value); // but value can be String, Integer, or Boolean

Thanks.

8
  • 5
    I would just not do this; it looks horrible. Java is a strongly typed language, if you want a weakly typed language; use one. Further, your value is a Class not an instance of a class. Commented Aug 16, 2016 at 21:06
  • @BoristheSpider the snippets are not good, I agree, but when dealing with data such as Json or data from a database, there's always a certain amount of type unsafety when deserializing or decoding the data, regardless of the language type system Commented Aug 16, 2016 at 21:10
  • Why not define a class that represents your tuple? Instead of each column separately. Commented Aug 16, 2016 at 21:11
  • 2
    @Dici not in Java. If you're getting data from somewhere you know what that data is. If you do not; then how can you know what you want to do with it? In Java you would create objects to represent the data and then read the data into those. If you want to do this in a type unsafe way; use Groovy... Commented Aug 16, 2016 at 21:12
  • 1
    @Dici that is my point; to be useful data has a structure - this is of course typesafe. There are plenty of frameworks for specifying this structure, for example JSON schemas, and plenty of frameworks for writing code to bind this typesafe data to your class hierarchy. Obviously, when it comes to runtime - someone might feed you the wrong data... Commented Aug 16, 2016 at 21:37

1 Answer 1

4

I would start, by making Pair generic. Something like,

public class Pair<T> {
   String key;
   T value;
   public Pair(String key, T value){
      this.key = key;
      this.value = value;
   };
   // ...
}

Then you instantiate a Pair for the correct column type. Like,

Pair<String> p = new Pair<>("a", "b");

or

Pair<Integer> p = new Pair<>("a", 1);
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I need =] thank you so much. Sorry for horrible codes like this. I am glad I find an answer here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.