My English is not good. So I try to explain my question in code. Please try to answer me in code. It makes me understand easily.
I have a question: In C++,we can realize that like these codes.
int max(int x,int y)
{
return (x>y)?x:y;
}
float max(float x,float y)
{
return (x>y)?x:y;
}
I can do this to expand code
template <class T>
T max(T x, T y)
{
return (x>y)?x:y;
}
How can I realize the similar function in Java.
You can use generics. Because generics do not work with primitives, and objects cannot be compared with greater-than and less-than operators, we must instead restrict the input types to Comparables (things which can be compared) so that we can use the compareTo method instead:
public static <T extends Comparable<T>> T max(T first, T second)
{
return first.compareTo(second) >= 0 ? first : second;
}
Sample usage:
max(1, 2)
will autobox the primitives to Integers (which do implement Comparable<Integer>) and return 2.
The underlying assumption in the c++ code is that the T type supports the > operator. Java doesn't have operator overloading, but the equivalent would be to limit the code to Ts that implement the Comparable interface:
public static <T extends Comparable<T>> T max(T x, T y)
{
if (x.compareTo(y) > 0) {
return x;
}
return y;
}
Or, alternatively, allow the caller to pass a custom Comparator:
public static <T> T max(T x, T y, Comparator<T> cmp)
{
if (cmp.compare(x, y) > 0) {
return x;
}
return y;
}
Use generics. They are like templates but work in runtime
package test;
class BoxPrinter<T> {
private T val;
public BoxPrinter(T arg) {
val = arg;
}
public String toString() {
return "{" + val + "}";
}
public T getValue() {
return val;
}
}
https://www.geeksforgeeks.org/generics-in-java/
More examples here
Related
I am not primarily a Java programmer... I would like to find a corresponding Java syntax for class storing a function pointer (templatized) as a variable. The function pointer points to a function "outside" the class. The original code is in C++11:
#include <memory>
template <typename T>
using p_function = T(*)(T, T, T);
template <typename T>
class A
{
private:
int k;
p_function<T> pf;
public:
A() { pf = NULL; k = 0; }
A(p_function<T> pf_, int k_) { pf = pf_; k = k_; }
T getF(const T a1, const T a2, const T a3) const { return pf(a1, a2, a3); }
};
template <typename T>
T f1(T x, T y, T z) { return x + y + z; }
template <typename T>
T f2(T x, T y, T z) { return x - y - z; }
int main()
{
A<double> aa (f1<double>, 1.0);
double val= aa.getF(1.0, 2.0, 3.0);
}
Thinking about the problem, is it reasonable to use the interface?
public interface Function <T> {
T pf(T x, T y, T z);
}
or, is there any better way? Java is relatively rapidly develops, there might be "straighter" constructions than few years ago. There are several requirements which I am not able to join together. Could I ask for a short code sample in Java? Thank you very much for your help.
Use java 8. That uses "functional" interfaces (indeed) where an interface defines just one single function.
To not overuse the existing Function class, introduce your own name.
#FunctionalInterface
public interface TriFunction<T> {
T apply(T x, T y, T z);
}
Marking it with the FunctionalInterface annotation is a practice that prevents adding a second function and such.
class Foo {
public static Bar hop(Bar x, Bar y, Bar z) { ... }
}
TriFunction<Bar> pf = Foo::hop;
TriFunction<Integer> pg = (x, y, z) -> x + y + z;
Bar bara = pf.apply(a, b, c);
For primitive types better define own interfaces without generic parameter types. Above pg needs 3 times to unbox the wrapper objects, and one time to box it again to an object.
The package java.util.function contains many functional interfaces, like BinaryOperator and IntBinaryOperator.
In Java 8, you can use method references. More information here: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
Basically, Java 8 gives interfaces with only one method the special property that they can be used (sort of) like function pointers. You can assign a lambda or a method reference to an object of such a type.
For example, somewhat related to your question:
public class HelloWorld {
public interface Function <T> {
T op(T x, T y);
}
public static class Functions {
static int add(int x, int y) { return x + y; }
static int sub(int x, int y) { return x - y; }
}
static Function<Integer> f1, f2; // <-- "function pointer"
public static void main(String []args) {
f1 = Functions::add; // <-- static method reference
f2 = Functions::sub; // <-- static method reference
System.out.println("Test: " + f1.op(1,2) + ", " + f2.op(1,2));
}
}
This code prints, as you'd expect:
Test: 3, -1
So that part of your question should work. However, the part where you define a generic addition is more problematic, because Java doesn't allow you to overload the operator '+'. So the following will not compile in Java:
T add(T x, T y) {
return x + y; // compile error -> no '+' defined for T
}
If you need T to be base types, you'll need to define your f1 and f2 for each base type you want to use. See also this question: Can I do arithmetic operations on the Number baseclass?
I am not sure if I get your question correctly, but have a look at this stackoverflow post.
There are several answers on how to implement function pointer in java.
EDIT
I am not experienced enough in C++ to provide a code sample.
EDIT 2
According to the post I mentioned above, you could try something like this:
public class WithFunction {
//Empty constructor, can be left out
public WithFunction () {...}
//The function you want to reference
public int myReferencedFunction () {...}
}
Then
public class MethodCaller {
public static Object call (Object theObject, String methodName) {
return theObject.getClass().getMethod(methodName).invoke(theObject);
//catch Exceptions
}
}
Then you can have it like
public static void main (String [] args) {
WithFunction obj1 = new WithFunction();
Object result = MethodCaller.call (obj1, "toString");
int result = (int) MethodCaller.call (obj1, "myReferencedFunction");
}
Notice:
You need to catch a lot of exceptions. Strong error handling needed..
If you use an interface, you can also implement it multiple times and should have the freedom you need
I want to store two primitive values (and I'm including String as a primitive here) inside a field. For example, I may want to store a String and an int as below:
("hello", 42)
So if the type I'm looking for is X, I'd like to be able to declare a field
private X myX = new X("hello", 42);
or some other incantation that gives me the same result.
I'm trying to figure out what type that field has to be. It has to accept any Java primitive type and String so it could be a String + int or String + float... actually any combination of primitive (+ String) types. Essentially, to borrow a concept from functional languages, I just want a tuple that's restricted to primitives. But Java doesn't have that.
Since they are primitives, generics don't work very well. And I'm not sure how I feel about boxing/unboxing.
What is the best data type/structure I can use in Java to do it?
Don't think that there is a way to do it with built-in Java libraries. However one could write Tuple class identical to Tuple2 class in Scala language like this:
public class Tuple<K, V> {
private K first;
private V second;
// create tuple only with static method "of"
private Tuple(K first, V second) {
this.first = first;
this.second = second;
}
public static <K, V> Tuple<K, V> of(K a, V b) {
return new Tuple<K, V>(a, b);
}
public Tuple<V, K> swap() {
return new Tuple<V, K>(second, first);
}
public K getFirst() {
return this.first;
}
public V getSecond() {
return this.second;
}
#Override
public String toString() {
return "(" + first.toString() + "," + second.toString() + ")";
}
public static void main(String[] args) {
Tuple myTuple = Tuple.of("hello", 2);
System.out.println("toString: "+myTuple);
System.out.println("First: "+myTuple.getFirst());
System.out.println("Swap: "+ myTuple.swap());
}
}
Output:
toString: (hello,2)
First: hello
Swap: (2,hello)
If you really want to store the values as one primitive type, you have to 'encode' them.
For example:
String field = "hello;42";
String[] values = field.split(";");
int intValue = Integer.parseInt(values[1]);
Since you're working with only primitives (and String) it seems that you'd like to avoid auto-boxing and unboxing if possible. That means you can't use generics. (Primitive types can't be used as type arguments.) It also seems as though you'd like to avoid unnecessary promotion (e.g. if you stored floats as doubles you'd preserve the original value, but lose the fact that it's actually a float).
A rather elaborate solution would be to create a set of PrimitivePair classes, one for each combination of primitive types that you want to store, along with a factory class for constructing them easily. For example, you might have the following three classes to store various pairs of primitives:
package primitivepair;
public class PrimitivePairStringInt
{
final String s;
final int i;
PrimitivePairStringInt(final String s, final int i)
{
this.s = s;
this.i = i;
}
public String getFirstValue()
{
return s;
}
public int getSecondValue()
{
return i;
}
}
package primitivepair;
public class PrimitivePairFloatDouble
{
final float f;
final double d;
PrimitivePairFloatDouble(final float f, final double d)
{
this.f = f;
this.d = d;
}
public float getFirstValue()
{
return f;
}
public double getSecondValue()
{
return d;
}
}
package primitivepair;
public class PrimitivePairCharByte
{
final char c;
final byte b;
PrimitivePairCharByte(final char c, final byte b)
{
this.c = c;
this.b = b;
}
public char getFirstValue()
{
return c;
}
public byte getSecondValue()
{
return b;
}
}
They would be created by the following factory class:
package primitivepair;
public class PrimitivePairFactory
{
public static PrimitivePairCharByte createPrimitivePair(final char c, final byte b)
{
return new PrimitivePairCharByte(c, b);
}
public static PrimitivePairFloatDouble createPrimitivePair(final float f, final double d)
{
return new PrimitivePairFloatDouble(f, d);
}
public static PrimitivePairStringInt createPrimitivePair(final String s, final int i)
{
return new PrimitivePairStringInt(s, i);
}
}
You can of course add more classes and factory methods for other combinations.
I wanted to make an abstract PrimitivePair class that would have the creation methods and method declarations for the getFirstValue() and getSecondValue() methods, but that would have required that return type covariance works for primitives, and I don't believe it does.
Java does not have a built-in Tuple type.
You might use the standard Map.Entry<K,V> but I would personally suggest Apache Commons Lang3's Tuple type if possible.
I have an Operator interface for handling math operator that has two method like so:
public interface Operator
{
double calculate(double firstNumber,double secondNumber);
char getSign();
}
for each operator I have a class that implement Operator interface like so:
public class Plus implements Operator
{
public double calculate(double firstNumber,double secondNumber)
{
return firstNumber + secondNumber;
}
public char getSign()
{
return '+';
}
}
And so on...
In this code I use Reflections :
Reflections reflections = new Reflections("mypackage");
Set<Class<? extends Operator>> classes = reflections.getSubTypesOf(Operator.class);
Reflections is not the part of java Reflection API.I should just use java Reflection capability.
Can anyone help me to change this code that only use java Reflection API?
Instead of using the Reflections API, you can
search your class path for directories and JARs
for the directories look for each class file
for the JAR scan through the files for each class file
read the byte code of the class file with a library like ASM.
if the class implement your interface add it
otherwise check all the super classes and interfaces of the class to see if they implement the interface.
The reason you have to read the byte code is you want to avoid loading all the classes just to see the inheritance hierarchy, esp as some of the classes might not load or could take a long time.
Needless to say, using a library which does this for you is easier. If youw ant to write this yourself I suggest you read the source of the Reflections API to see how it does it.
A simpler solution is to use an enum
enum Operators implement Operator {
PLUS {
public double calculate(double x, double y) {
return x + y;
}
public char getSign() {
return '+';
}
},
MINUS {
public double calculate(double x, double y) {
return x - y;
}
public char getSign() {
return '-';
}
},
TIMES {
public double calculate(double x, double y) {
return x * y;
}
public char getSign() {
return '*';
}
},
DIVIDE {
public double calculate(double x, double y) {
return x / y;
}
public char getSign() {
return '/';
}
}
}
To get all the operators you can use
Operator[] operators = Operators.values();
You cannot scan the class path for the sub types. If you can correlate, even for JPA you would have to specify the entity names as part of configuration. Use a similar approach to specify the list of classes you like to scan through and check if the instanceof. Reflections API only can help with you in that case if not.
I was reading about currying in functional-programming, and I have a very basic question:
If I have two functions in Java
int add(int x, int y){
return x+y;
}
and I create another method
int increment(int y){
return add(1, y);
}
In the above code, when I wrote increment function, did I actually curry add ?
You have partially applied add. This is related to currying.
In some languages that support partial application, functions are curried by default. you might be able write code like:
increment = add(1)
println(increment(2))
# => 3
A curried function allows you to partially apply that function directly. Java doesn't support that kind of thing without extra machinery.
EDIT:
In Java 8, with lambdas and java.util.function, you can define a curry function.
import java.util.function.Function;
public class Example {
public static <T, U, R> Function<T, Function<U, R>> curry(BiFunction<T, U, R> f) {
return t -> u -> f.apply(t, u);
}
public static int add(int x, int y) {
return x + y;
}
public static void main(String[] args) {
Function<Integer, Function<Integer, Integer>> curriedAdd = curry(Example::add);
// or
// BiFunction<Integer, Integer, Integer> add = (x, y) -> x + y;
// curriedAdd = curry(add);
Function<Integer, Integer> increment = curriedAdd.apply(1);
System.out.println(increment.apply(4));
}
}
EDIT #2:
I was wrong! I've corrected/modified my answer. As sepp2k pointed out this is only partial function application. The two concepts are related and often confused. In my defense there's a section on the currying Wikipedia page about the mixup.
No, you just call it. You need to pass function as argument, and return partial evaluation of that function to call it currying.
I would like to perform an operation on two generics argument of the same type both extending Number.
Is it Possible?
I always used to call methods on generic arguments, but seems there is some problem using operators (The operator + is undefined for the argument type(s) T, T).
public static <T extends Number> T sum(T a, T b){
return a+ b;
}
What am I doing wrong?
EDIT:
I try to improve a little bit my question. I understood that operators are not defined for type Number. It's a bit sad this thing because it would be nice to perform such an operation without introducing new interfaces like suggested by #Victor Sorokin.
But I still don't understand one thing: if operators are not implemented in the class Number, then at least in Double class should be implemented because I can use + operator with double.
Neither these line of code will compile:
public static <T extends Double> T sum(T a, T b){
T c = a +b;
}
why?
It's not possible because Number doesn't have a + operator associated with it. In particular, you can't do this:
Number a = new Integer(1);
Number b = new Integer(2);
Number c = a + b;
There is no + operator for classes in Java (except String and there's implicit conversion for other types via toString() when one of arguments is String). So, make you type implement some interface, say
interface Valuable {
// use richest built-in numeric type
double value();
Valuable value(double v);
}
public static <T extends Valuable> T sum(T a, T b){
return a.value(a.value() + b.value());
}
Ugly, isn't it? =D
Fix 2022 code above is wrong, as Valuable#value can't produce instance of subtype T, so we need to go a bit more hairy:
interface Valuable<T extends Valuable<T>> {
// use richest built-in numeric type
double value();
T value(double v);
}
class Impl implements Valuable<Impl> {
private final double v;
Impl(double v) {
this.v = v;
}
#Override
public double value() {
return v;
}
#Override
public Impl value(double v) {
return new Impl(v);
}
#Override
public String toString() {
return "Impl{" +
"v=" + v +
'}';
}
}
class Scratch {
public static void main(String[] args) {
Impl a = new Impl(1), b = new Impl(-1);
System.out.println(a + " + " + b + " = " + sum(a, b));
}
public static <T extends Valuable<T>> T sum(T a, T b){
return a.value(a.value() + b.value());
}
}