In-class-15
Class: CSCE-314
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
public class FridayStreamsReflectionStarter {
public static void main(String[] args) {
List<Person> people = createSampleData();
// You will fill in these methods.
todoBasicStream(people);
todoMappingExample(people);
todoFoldExample(people);
inspectIntegerClass();
}
// --- Sample data setup ---
private static List<Person> createSampleData() {
// Feel free to add/change people, but keep at least 5–6 entries.
return List.of(
new Person("Zoe", 18, "Bryan"),
new Person("Alex", 22, "College Station"),
new Person("Jordan", 25, "Houston"),
new Person("Priya", 30, "Dallas"),
new Person("Miguel", 28, "Austin"),
new Person("Taylor", 19, "Bryan")
);
}
// --- TODO 1: Basic stream usage ---
/**
* TODO 1:
* (a) Use a stream to print ALL people in the list, one per line.
* Hint: We did this Wednesday
*
* (b) Also use a stream + filter to print only some subset
* (e.g., age >= 21, or city == "Bryan").
*/
private static void todoBasicStream(List<Person> people) {
System.out.println("=== TODO 1: Basic stream to see the data ===");
// 1(a): print everyone using a stream
System.out.println("People:");
List<Person> showPeople =
people.stream()
.collect(Collectors.toList());
showPeople.forEach(name -> System.out.println(" " + name));
// 1(b): print only some filtered subset using filter(...)
System.out.println("\nPeople at least 21 (using streams):");
List<Person> adults =
people.stream()
.filter(p -> p.getAge() >= 21) // predicate lambda
.collect(Collectors.toList());
adults.forEach(p -> System.out.println(" " + p));
}
// --- TODO 2: Mapping example ---
/**
* TODO 2:
* Use a stream + map to create a NEW List<Person> where one
* field is changed, WITHOUT mutating the original list.
*
* Examples (pick ONE):
* - age + 1 for everyone
* - uppercase all names
* - prepend "City of " to each city
*
* Steps:
* 1) people.stream().map(p -> new Person(...)).collect(Collectors.toList())
* 2) Print the new list to verify the transformation.
*/
private static void todoMappingExample(List<Person> people) {
System.out.println("\n=== TODO 2: Mapping example (create transformed list) ===");
// 2: implement the mapping and print the transformed list
System.out.println("\nPeople with \"City of\" added:");
List<Person> namesWithCity =
people.stream()
.map(p -> new Person(p.getName(), p.getAge(), "City of " + p.getCity()))
.collect(Collectors.toList());
namesWithCity.forEach(p -> System.out.println(" " + p));
}
// --- TODO 3: Folding / reduction example ---
/**
* TODO 3:
* Use a stream to fold the list into ONE answer.
*
* Choose something DIFFERENT from what we did Wednesday.
* Example ideas:
* - total number of characters in all names combined
* - minimum age
* - maximum age
* - a single comma-separated String of all names
*
* Requirements:
* - Use people.stream()
* - Use map(...) if needed
* - Use reduce(...) OR a terminal operation like sum(), min(), max()
* - Print the result with a label.
*/
private static void todoFoldExample(List<Person> people) {
System.out.println("\n=== TODO 3: Folding / reduction example ===");
// 3: implement fold/reduced (total characters across all names)
long totalNameChars = people.stream()
.mapToInt(p -> p.getName().length())
.sum();
System.out.println("Total characters across all names: " + totalNameChars);
}
// --- TODO 4: Reflection on java.lang.Integer ---
/**
* TODO 4:
* Use reflection to inspect the java.lang.Integer class.
*
* Steps:
* 1) Get the Class object for Integer:
*
* 2) Print all declared FIELDS:
*
* 3) Print all declared METHODS:
*
*/
private static void inspectIntegerClass() {
System.out.println("\n=== TODO 4: Inspect java.lang.Integer with reflection ===");
Class<Integer> clazz = Integer.class; // Get the Class object
System.out.println("Class name: " + clazz.getName());
// 4(a): list all declared fields (type + name)
System.out.println("\nDeclared fields:");
Field[] fields = clazz.getDeclaredFields();
for (Field f : fields) {
System.out.println(" " + f.getType().getSimpleName() + " " + f.getName());
}
// 4(b): list all declared methods (return type + name + param types)
System.out.println("\nDeclared methods (return type + name + param types):");
Method[] methods = clazz.getDeclaredMethods();
for (Method m : methods) {
String params = Arrays.stream(m.getParameterTypes())
.mapgetSimpleName
.collect(Collectors.joining(", "));
System.out.println(" " + m.getReturnType().getSimpleName() + " " + m.getName() + "(" + params + ")");
}
}
// --- Simple Person class used for the stream examples ---
static class Person {
private final String name;
private final int age;
private final String city;
public Person(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getCity() {
return city;
}
@Override
public String toString() {
return name + " (" + age + ", " + city + ")";
}
}
}