Can I make generic class in Java, but only without numeric types? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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?

3rd Jun 2020, 7:58 AM
Ɓbel Domonkos SƔfrƔn
Ɓbel Domonkos SƔfrƔn - avatar
1 Resposta
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 ); } }
3rd Jun 2020, 10:58 AM
zemiak