Eclipse would not directly help you in such a task - it can only help in creating a program - it is not created by itself.
If adding a new library to a project is not an option, you can write your own generic solution. However, I highly recommend that you take a look at adding some shared library such as Guava or Apache Commons, as suggested earlier.
The two tasks you are trying to achieve can be more broadly described as filtering and matching (aka selection, projection). Filtering can be generalized using Predicate, a simple function that returns whether an object should be included in a new collection. The mapping can be achieved using a common function that takes one source object (in your case, MyObject) and returns an object of another type (in your case String).
These plural operations (collection) are more easily supported by languages, some of which are compiled in the JVM, such as python (jython), groovy, and scala. But I assume that this is not an option to use another language for the project.
So, I will demonstrate how this can be achieved in Java, although the syntax is a bit clunkier, since we will use anonymous classes for the predicate and converter.
There is a simple DIY filtering example for What is the best way to filter a Java collection? , so I will use it in this example.
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 perform 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 it would be very simple to change the predicate to filter other objects or to create different lines or even other types instead of a script: just change the apply () and convert () functions! (Alright, and some generic tips :)).
Disclaimer: the code is not fully verified, just deployed for demonstration, and may have some other problems (for example, allocating an ArrayList for new collections). This is why it is usually best to use proven and debugged third-party libraries!