- How can I map a String to a function in Java?
- Solution 2
- Solution 3
- Solution 4
- Related videos on Youtube
- Matt Ball
- Comments
- Java map string to function
- Java java map string string in c
- Java map string string in java
- C Map String to Function
- Java: is there a map function?
- Java can we use map function in c
- Java how to map in a function
- How to iterate over Map and Map
using the same recursion function? - Java 8 Converting List<Map<String,String> to Map<String,Map<String,String>>
- Java map string string forearch java
- Java iterate over map string string java
- Java: How to fill placeholders in a text with Map<String,String>?
- Java using map string string jpa
- Java java map of string and list
- Java java map list to string
- Map a string with a function on Typescript
- Mapping strings to functions in javascript
- Java 8 generic map of functions
- Map function in java 8 stream
- Java 8 — Map between class to one of its function
- How to use map-function in SPARK with Java
- Most elegant way to join a Map to a String in Java 8
- Converting string to to Map in Java
How can I map a String to a function in Java?
Couldn’t you do String to Method ? Then you can cache the methods you need to execute.
Solution 2
There aren’t any first-class standalone functions, but you can do what you want with an interface. Create an interface that represents your function. For example, you might have the following:
public interface ComputeString
Then you can create a Map object like you want in the first place. Using a map will be much faster than reflection and will also give more type-safety, so I would advise the above.
Solution 3
While you can’t have first class functions, there are anonymous classes which can be based on an interface:
interface ProcessingMethod < String method(); >Map methodMap = new HashMap(); methodMap.put("abc", new ProcessingMethod() < String method() < return "xyz" >>); methodMap.put("def", new ProcessingMethod() < String method() < return "uvw" >>); methodMap.get("abc").method();
Solution 4
This example uses an enum of named functions and an abstract FunctionAdapter to invoke functions with a variable number of homogeneous parameters without reflection. The lookup() function simply uses Enum.valueOf , but a Map might be worth it for a large number of functions.
Related videos on Youtube
Matt Ball
Comments
Currently, I have a bunch of Java classes that implement a Processor interface, meaning they all have a processRequest(String key) method. The idea is that each class has a few (say, <10) member Strings , and each of those maps to a method in that class via the processRequest method, like so:
class FooProcessor implements Processor < String key1 = "abc"; String key2 = "def"; String key3 = "ghi"; // and so on. String processRequest(String key) < String toReturn = null; if (key1.equals(key)) toReturn = method1(); else if (key2.equals(key)) toReturn = method2(); else if (key3.equals(key)) toReturn = method3(); // and so on. return toReturn; >String method1() < // do stuff >String method2() < // do other stuff >String method3() < // do other other stuff >// and so on. >
- My keys would have to be named the same as the method — or be named in a particular, consistent way so that it’s easy to map them to the method name.
- This seems WAY slower than the if-else statements I have right now. Efficiency is something of a concern because these methods will tend to get called pretty frequently, and I want to minimize unnecessary overhead.
TL; DR: I’m looking for a clean, minimal-overhead way to map a String to some sort of a Function object that I can invoke and call (something like) getReturnType() on. I don’t especially mind using a 3rd-party library if it really fits my needs. I also don’t mind using reflection, though I would strongly prefer to avoid using reflection every single time I do a method lookup — maybe using some caching strategy that combines the Map with reflection.
Thoughts on a good way to get what I want? Cheers!
Couldn’t you do String to Method (java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/Method.html)? Then you can cache the methods you need to execute.
That would mean having an implementing class for every single function I want to invoke, which seems clunky and requires a bit of boilerplate for every single function. Doesn’t sound that awesome to me.
@Bears, yeah it is clunky, but you can construct anonymous classes, which should make it somewhat less clunky, like map.put(str,new ComputeString()
Java map string to function
You maybe like other following related articles
Java java map string string in c
also available via Map map = Splitter.on(«|»).withKeyValueSeparator, Using Java8: private static Map prepareMap() < Map, map = new LinkedHashMap<>(); map.put(«key1», «val1»); map.put(«key2,
Java map string string in java
> Because entry.getValue() is not a Function, into String[] using Java 8 way., ,String>)map.values(); where, map is of type Map result = new TreeMap<>(); for(Map getMapsFromArrayOfMaps( Collection
C Map String to Function
:string> map; map[10] = «Hello»; map[1] = «boys»; map[901] = «girls» , first string to a function., I was thinking of making two arrays, one of strings, and another with their corresponding function pointers, So, what’s the best way to execute a function based on the input of a string?, How do I map a string to a certain function?
Java: is there a map function?
(Java) [duplicate]»> map function . Is there something like this in Java already?, > output = Collections2.transform(input, new Function() < @Override, 8, there is actually a map function, so I'd probably write the code in a more concise way: , Collectionhex = input.stream() .map(Integer. project that supports a variety of mapping formats written in Java.
Java can we use map function in c
Solution 1: Since java is not a functional, public interface function < void execute(); >And (in java, > integers = FluentIterable .from(strings) .transform(new Function
Java how to map in a function
How to iterate over Map and Map using the same recursion function?
> and List< List< String>> using the same function,I can write something like this:, < String,String>and Map< String,Map< String,String>> similarly, I tried: , >>>>(); this map i filled data in java and render to html page but, using std::cin; using std::endl; using std::string; using std::map; int main() < map
Java 8 Converting List<Map<String,String> to Map<String,Map<String,String>>
List to Map Of Map i.e., Map>; In List of Map I, NY=> I want to convert it by using Java, But this should do what you want under the circumstances: Map toMap(Div div) < Mapresult
Java map string string forearch java
(this — string,string) contains key in function parameter thanks for help, key, Map> map) < return map.values() .stream, String ) that I'd like code on the other side of the java interface to be able to populate, mapping) < // use mapping like Mapwithout, But with Java 8, there has been a large number of methods added using Lambda functions, which would be
Java iterate over map string string java
One possible way (without functional code): Map>> yourMap = . ; //iterate over outer map for(Map.Entry locationsMap, Hashtable>> map = new Hashtable&, Solution 2: If you can use java 8,
Java: How to fill placeholders in a text with Map<String,String>?
Since I expect to use several place holders I have used java Map<>., Map propMap = new HashMap(); propMap.put, public String createNotification(Map properties) < String, , you need to do something like this: MapyourMap = //. for, p> I think %s is Python’s grammar to place holder, can’t use this in Java
Java using map string string jpa
Java java map of string and list
>> testMap = foos.stream() .collect(Collectors.groupingBy, in this case ) based on the two provided functions
Java java map list to string
using Java 8 streams?, [] result = clients .stream() .map(client->client.getEmail()) .toArray(String[]::new)
Map a string with a function on Typescript
Question: Is it possible to map a string to a function using, For example : this.keyBinding = new Map(); this.keyBinding, code> object from a string,function dictionnary., ; return entity; >], ]; const entityMap : Map = new Map(entityTagDictionnary, >constructor argument: const entityMap: Map = new Map
Mapping strings to functions in javascript
method when using it to perform a function on an array of strings., basically what I want is a map from string => function in
Java 8 generic map of functions
Map function in java 8 stream
I have to iterate on same list and apply different mapping functions., that accepts String and a Function
Java 8 — Map between class to one of its function
How to use map-function in SPARK with Java
> csvData = sc.textFile(csvInput, 1); JavaRDD words = csvData.map(new Function, -600″ title=»Data analytics on Cassandra»>data-analytics-using-cassandra-and-spark/ tutorial in Java, «, «cc_balance», SomeColumns(«customerid», «card», «balance», «updated_at»)) Java, , 1); JavaRDD counts = csvData.map(new Function
Most elegant way to join a Map to a String in Java 8
entry set, then map each entry to the string representation you want, joining them in a single string, ()); original.put(«bar», new Column()); Map copy = original.entrySet(), map = new HashMap<>(); map.put(«test1», 1); map.put(«test2», 2); Map map2 = new HashMap<>(); map.forEach(map2::put); System.out.println(«map: «, A lambda expression is one or more line of code which works like function or method.
Converting string to to Map in Java
ObjectMapper code to parse the string into a Map: private static final ObjectMapper, OBJECT_MAPPER = new ObjectMapper(); Map map = OBJECT_MAPPER.readValue(«jsonString, «, new TypeReference