-1

My problem Statement is that i will have a Map and want to write a generic method such that user can pass three thing(Map,Keyname,ObjectType) and we will be getting the Object that is value after type-casting based on what user pass

This is the dummy code which i tried. I am able to achieve part of it

public class Convert {

    public static void main(String args[]) {

        School school = new School();
        school.setName("sa");

        Student st = new Student();
        st.setName("asni");
        school.getStudentSet().add(st);

        Map<String,Object> am = new HashMap<>();
        am.put("St",st ); // value is Student Object
        am.put("S",school.getStudent()); // value is Set<Student>
        am.put("school", school); // value is School Object

        Student s = getvalue(am,"St",Student.class);//this is working
        School sd = getvalue(am,"school",School.class);//this is working
        Student st1 = new Student();
        st1.setName("aaa");
        getvalue(am,"S",Set.class).add(school); //in this school object is getting add which should not happen
        System.out.println();
    }

    public static <T> T getvalue(Map<String,Object> am,String name, Class<T> rtype) {
        return rtype.cast(am.get(name));
    }
}

I should be getting all the desired object after getting typecasted based on the input provided by the user

Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
key
  • 21
  • 2
  • What is the problem you have? The code you have there is working. And what do you mean by "is getting add which should not happen"? That is exactly what `add()` is doing. – Progman Aug 16 '19 at 21:52
  • If you see "S"will give set of student so basically only student object should be added not the school object. This is what i want to solve. – key Aug 17 '19 at 06:20

1 Answers1

0

You can do this by rewrite the getvalue() method and remove the Class<T> rtype argument. This is needed because you can't do Set<Student>.class. Instead, specify the type at the method call. The code will look like this:

public static <T> T getvalue(Map<String,Object> am, String name) {
    return (T)am.get(name);
}

You can use this method as follow:

public static void main(String[] args) throws Exception {

    Map<String, Object> foobar = new HashMap<String, Object>();
    foobar.put("abc", 42);
    foobar.put("def", "something");
    foobar.put("ghi", new HashSet<Integer>());
    System.out.println(foobar);

    String s = getvalue(foobar, "def");
    System.out.println(s);
    Integer i = getvalue(foobar, "abc");
    System.out.println(i);

    Test.<Set<Integer>>getvalue(foobar, "ghi").add(90); // fine
    Test.<Set<Integer>>getvalue(foobar, "ghi").add("something else"); // compile error

    Test.<Set>getvalue(foobar, "ghi").add("something else"); // be careful, this is working, even though it's unexpected.
}

(Test is the class these methods were in)

Progman
  • 16,827
  • 6
  • 33
  • 48
  • it won't serve the purpose. because you are doing type casting after calling the getvalue method – key Aug 17 '19 at 15:21
  • @key The casting is inside the `getvalue()` method, not outside. And the cast will be the same as using the `cast()` method on the `Class>` object. – Progman Aug 17 '19 at 17:28
  • ya but in my scenario i want to do the casting in the getValue() method that is why i came up with this logic – key Aug 18 '19 at 14:01
  • @key The casting **is** done inside the method, not outside. See the expression `(T)am.get(name);`, it is placed inside the `getvalue()` method. – Progman Aug 18 '19 at 14:03