Pulling specific fields from a list of objects.
When you have a list of full objects but only need one piece of information from each — sometimes only from the ones that qualify — accumulation with filtered extraction is the pattern.
An ArrayList of objects often carries more information than any single question needs. You may be holding ArrayList<Student> but the caller only wants the names of students who passed.
The combined pattern has two moves: filter (decide which objects qualify) and extract (pull one field from each qualifier into a new collection). The result is a fresh ArrayList whose element type is usually different from the source.
This pattern appears in nearly every AP CSA class-design FRQ that involves a list of objects — it's the natural way to summarize a collection.
Filter, then extract, then add.
A single accumulator list collects results across the traversal.
The template
public ArrayList<ReturnType> methodName(ArrayList<SourceType> source) {
ArrayList<ReturnType> result = new ArrayList<ReturnType>();
for (SourceType item : source) {
if (/* filter condition on item */) {
result.add(item.getSomeField());
}
}
return result;
}
Three things change per problem: the source type, the return type, and the filter condition. The skeleton stays identical.
Concrete example
Assume a Student class with getName() and getGpa(). Return the names of students whose GPA is at least 3.0:
public ArrayList<String> namesOfPassingStudents(ArrayList<Student> students) {
ArrayList<String> names = new ArrayList<String>();
for (Student s : students) {
if (s.getGpa() >= 3.0) {
names.add(s.getName());
}
}
return names;
}
Why enhanced for works here
Because we're reading from the source and writing to a different list, there's no modification of the source. The enhanced-for loop is clean and safe.
Type alignment
The result list's element type must match what getSomeField() returns. If getName() returns String, the result must be ArrayList<String> — never the original object type.
What if no objects qualify?
The loop runs but never adds. The method returns an empty ArrayList — not null. This is the expected AP CSA behavior.
Terms used in object-list problems.
Used in FRQ rubrics for class-collection questions.
- Accumulator list — the new
ArrayListthat collects results. - Source list — the input
ArrayListof objects being traversed. - Filter condition — the boolean expression that decides which objects qualify.
- Field extraction — calling an accessor to pull one piece of data from an object.
- Element type — the generic parameter in
ArrayList<E>. - Accessor — a
getXxxmethod on the source object.
How filtered extraction is tested.
Return-type matching and accessor calls are the most-graded items.
What FRQ graders check:
- Exact return type.
ArrayList<String>for names,ArrayList<Integer>for ints — notArrayList<Student>. - Use of given accessor methods. Don't invent new ones; use the signatures the prompt provides.
- Correct filter direction. "At least" includes the boundary; "more than" excludes it.
- Empty-list handling. Return an empty
ArrayListif nothing qualifies — nevernull.
MCQs often show the extraction code and ask "what's in the result list?" — trace by walking the source one object at a time.
Extract IDs of expensive books.
A standard filtered-extraction problem from a class spec.
Setup. A Book class has getId() returning int and getPrice() returning double. Given ArrayList<Book> library, return an ArrayList<Integer> of IDs for books that cost more than $50.
Step 1. Identify the return type: ArrayList<Integer> because IDs are int (autoboxed to Integer).
Step 2. Write the template with the correct types.
Step 3. Filter with getPrice() > 50; extract with getId().
public ArrayList<Integer> expensiveBookIds(ArrayList<Book> library) {
ArrayList<Integer> ids = new ArrayList<Integer>();
for (Book b : library) {
if (b.getPrice() > 50) {
ids.add(b.getId());
}
}
return ids;
}
Trace. If the library has four books with prices 30, 60, 45, 80 and IDs 1, 2, 3, 4 respectively, the result is [2, 4].
Why this matters. Notice how the result element type differs from the source type. This is the most common student confusion — they accidentally write ArrayList<Book> as the return type and add whole books instead of IDs.
Errors that lose easy FRQ points.
Most come from wrong types or missing accessors.
- Wrong generic type on the result. Adding
Stringto anArrayList<Student>won't compile. - Adding the whole object instead of the field.
result.add(s)when you meantresult.add(s.getName()). - Direct field access.
s.namewon't compile when fields areprivate; uses.getName(). - Inverted filter. Returning passing students vs failing students — read the prompt twice.
- Returning
nullon empty. Return the empty list instead. - Using
ArrayList<int>. Won't compile; useArrayList<Integer>.
Filtered extraction at a glance.
A checklist for every problem of this shape.
Filtered Extraction Reference
AP Quick Reference| Step | Action | AP Exam Tip |
|---|---|---|
| 1 | Create empty result list | Element type matches what you'll add. |
| 2 | Loop over source objects | Enhanced for is clean — no mutation of source. |
| 3 | Apply filter condition | Read the prompt for exact boundary. |
| 4 | Extract field via accessor | Use the exact method name from the spec. |
| 5 | Add to result list | Add the field's value, not the object. |
| 6 | Return result | Never null — empty list if nothing qualifies. |
How to internalize filtered extraction.
One template, many variants — drill until it's reflex.
Use the three practice tools below this lesson in order:
- MCQ Practice — for each problem, write down the source type, the return type, the filter, and the extractor before tracing. Most wrong answers fail one of those four checks.
- Java Lab — implement filtered extraction for several class specs: names of passing students, IDs of expensive books, titles of long songs. Re-use the same template every time.
- FRQ Practice — write the result-list declaration first with the correct generic type, then the loop, then the filter, then the add. Match the prompt's accessor names exactly.
If you can declare the result list's type correctly within five seconds of reading the prompt, you've mastered this pattern.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.