0
Can I make generic class in Java, but only without numeric types?
Java generics. How to substract elements from the set, that holds the generic types?
1 Respuesta
0
// if 'substract' mean difference between two sets:
import java.util.*;
class Gen<T> {
T store;
Gen(T obj){ store = obj; }
}
public class SetDifference {
public static void main(String[] args) {
Gen<String> obj1=new Gen<>("one"), obj2=new Gen<>("two"),
obj3=new Gen<>("three"), obj4=new Gen<>("four");
HashSet<Gen<String>>
setA = new HashSet<>(Set.of(obj1, obj2, obj3, obj4) ),
setB = new HashSet<>(Set.of(obj2, obj3) );
setA.removeAll(setB);
for (Gen<String> obj: setA)
System.out.println( obj.store );
}
}