Get the list of invoking method name's from Hashmap put()

Viewed 79

Can you help me to get the list method name's from the HashMap put method.

 Book book= new Book();
        book.setTitle("The Little Prince");
        book.setAuthor("Antoine de Saint-Exupery");
        book.setPrize(9.99);
        book.setPublisher("Delux Pop-up book");
        
        
        HashMap<String,String> map= new HashMap<String,String>();
        map.put("title",book.getTitle());
        map.put("author",book.getAuthor());
        map.put("prize",String.valueOf(book.getPrize()));
        map.put("publisher",validatePuplisher(book.getPublisher()));
        

Excepted result is : book.getTitle(),book.getAuthor(),String.valueOf(book.getPrize()),validatePuplisher(book.getPublisher()) ie just method name's not value from the method.

1 Answers

If you want to get the method names used , then enclose them in double quotes to be treated as actual string .


 HashMap<String,String> map= new HashMap<String,String>();
        map.put("title","book.getTitle()");
        map.put("author","book.getAuthor()");
        map.put("prize","book.getPrize()");
        map.put("publisher","book.getPublisher()");

Related