Comparator
a Comparator is used to define how two objects should be compared or
sorted. You use it when you want to control how a list of objects is
ordered.
import java.util.*; //continuation
import java.util.Comparator; public class Main {
public static void main(String[] args) {
class Person { List<Person> people = new ArrayList<>();
String name; people.add(new Person("Alice", 30));
int age; people.add(new Person("Bob", 25));
people.add(new Person("Charlie", 35));
Person(String name, int age) {
this.name = name; // Sort people by age using AgeComparator
this.age = age; Collections.sort(people, new
} AgeComparator());
}
// Print sorted list
class AgeComparator implements Comparator<Person> { for (Person p : people) {
public int compare(Person p1, Person p2) { System.out.println(p.name + " is " + p.age + "
// Compare the two people by their age years old");
return p1.age - p2.age; }
} }
} }
Filter Collections
you can filter a collection (like a List or a Set ) to only keep elements that
meet certain conditions )
Program
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Filter even numbers
List<Integer> evenNumbers = numbers.stream().filter(n -> n % 2 == 0) .collect(Collectors.toList());
// Output the filtered list
System.out.println(evenNumbers);
}
}
Generation of streams
Streams refer to a sequence of elements that can be processed in a
functional style. They allow you to perform complex operations like
filtering, mapping, and reducing data in a clean and readable way
import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
// Generate a stream from the list
Stream<String> nameStream = names.stream();
// Process the stream
nameStream.filter(name -> name.startsWith("A"))
.forEach(System.out::println); // Output: Alice
}
}
.iterate(seed,next)
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
// Generate an infinite stream of numbers starting from 0
Stream<Integer> infiniteStream = Stream.iterate(0, n -> n +
1);
// Limit the stream to the first 10 numbers and print them
infiniteStream.limit(10)
.forEach(System.out::println); // Output: 0 1 2 3 4
56789
}
}
.generate(Supplier s)
import java.util.stream.Stream;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
// Generate an infinite stream of random numbers
Stream<Integer> randomNumbers = Stream.generate(() -> random.nextInt());
// Limit to 5 random numbers and print them
randomNumbers.limit(5)
.forEach(System.out::println);
}
}
.Of(your aguments…)
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
// Generate a stream of individual values
Stream<String> nameStream = Stream.of("Alice", "Bob",
"Charlie");
// Process the stream (e.g., count the number of names)
long count = nameStream.count(); // Output: 3
System.out.println(count);
}
}