Eclipse would not directly help you in such task - it can only help in creating the program - it doesn't create on itself.
If adding a new library to the project is not an option, you can write your own generic solution. However, I strongly suggest you'd look into adding some common library like Guava or Apache Commons as suggested before.
The two tasks that you are attempting to achieve can be more generally described as filtering and mapping (aka selecting, projecting).
The filtering can be generalized by using a Predicate - a simple function which will return whether the object should be included in the new collection.
The mapping can be achieved by a generic function which takes one source object (in your case, MyObject), and returns an object of another type (in your case, String).
Those set (collection) operations are supported more easily by languages, some of them compiled to the JVM, such as python (jython), groovy and scala. But I'm guessing it's not an option to use a different language for the project.
So I'll demonstrate how it can be achieved in Java, though the syntax is a bit clunkier, since we'll use anonymous classes for the predicate and the convertor.
There is a simple example of a DIY filtering at What is the best way to filter a Java Collection? , so I'll use it in the example.
The two interfaces we need:
public interface Convertor<FROM, TO> {
TO convert(FROM from);
}
public interface Predicate<T> {
boolean apply(T type);
}
And the actual "library":
import java.util.ArrayList;
import java.util.Collection;
public class CollectionUtils {
public static <FROM, TO> Collection<TO> select(Collection<FROM> from, Convertor<FROM, TO> convertor) {
Collection<TO> result = new ArrayList<TO>();
for (FROM fromItem: from) {
result.add(convertor.convert(fromItem));
}
return result;
}
public static <T> Collection<T> filter(Collection<T> target, Predicate<T> predicate) {
Collection<T> result = new ArrayList<T>();
for (T element: target) {
if (predicate.apply(element)) {
result.add(element);
}
}
return result;
}
}
Now we can easily achieve the two tasks:
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
ArrayList<MyObject> objects = new ArrayList<MyObject>();
objects.add(new MyObject("first"));
objects.add(new MyObject("second"));
objects.add(new MyObject("third"));
// Now do Task A and Task B
// Task A: Filter
Collection<MyObject> filtered = CollectionUtils.filter(objects, new Predicate<MyObject>() {
@Override
public boolean apply(MyObject myObject) {
return myObject.myProperty.equals("second");
}
});
for (MyObject myObject: filtered) {System.out.println(myObject.myProperty);}
System.out.println();
// TASK B: Map/Select
Collection<String> transforemd = CollectionUtils.select(objects, new Convertor<MyObject, String>() {
@Override
public String convert(MyObject from) {
return from.myProperty;
}
});
for (String str: transforemd) {System.out.println(str);}
}
}
It would be very simple now to change the predicate to filter other objects, or create different Strings, or even other types instead of script: just change the apply() and convert() functions! (Oh well, and some generics hints :) ).
Disclaimer: The code is not thoroughly tested, just rolled out for demonstration, and may have some other issues (such as allocating ArrayList for new collections). This is why it's usually better to use well tested and debugged 3rd party libraries!