Reputation: 55
So I have a bunch of java classes that are structured like this
class Something {
public static String name;
public static String description;
public void run(String[] args);
}
The problem is, they are all different types. So if I try to make a HashSet of them and iterate over them
HashSet<Object> things = new HashSet<Object>();
things.add(new Something());
things.add(new Otherthing());
things.forEach(thing -> {
thing.run();
})
it gives this error
symbol: variable run
location: variable thing of type java.lang.Object
So how can I make an iterable Set of these objects and use their properties?
Upvotes: 0
Views: 77
Reputation: 26
They need to be of the same type to be used like that. Luckily, we can fix this by implementing a fitting functional interface.
public class Something implements Runnable {
public String name;
public String description;
@Override
public void run() {
System.out.println("something");
}
}
Runnable has the method run() that takes no arguments and returns nothing (aka it has to do something).
So this works only if you're not actually going to be using the String array as an argument, as you don't include it in your calling of run(). However, if you do need it, you must change the implemented interface to Consumer instead of Runnable, with accept() instead, as this method actually takes an argument as opposed to run().
public class Main {
public static void main(String[] args) {
HashSet<Runnable> things = new HashSet<>();
things.add(new Something());
things.add(new Something2());
things.forEach(thing -> {
thing.run();
});
}
}
Now, we set the HashSet to the type of the implemented interface, and it works!
Here's the other class:
public class Something2 implements Runnable {
public String name;
public String description;
@Override
public void run() {
System.out.println("something2");
}
}
Now you can create as many classes as you want and have their methods do different things, while still being able to gather them in the same set(as long as they implement the same type)!
Side note: if you didn't need the attributes, you could've used lambda expressions to implement different versions of run() (or accept(), or any other functional interface-method).
Upvotes: 1