Unit 5 24cs302 Advanced Java Programming
Unit 5 24cs302 Advanced Java Programming
2
Please read this disclaimer before proceeding:
This document is confidential and intended solely for the
educational purpose of RMK Group of Educational Institutions. If you have
received this document through email in error, please notify the system
manager. This document contains proprietary information and is intended
only for the respective group/learning community as intended. If you
are not the addressee you should not disseminate, distribute, or copy
through e-mail. Please notify the sender immediately by e-mail if you
have received this document by mistake and delete this document from
your system. If you are not the intended recipient you are notified that
disclosing, copying, distributing, or taking any action in reliance on
the contents of this information is strictly prohibited.
3
24CS302
Advanced Java Programming
(Lab Integrated)
Department : CSE/IT/ADS/CSD/CSBS/ECE/CSE(CS)
Batch / Year : 2024 – 2028 / II
Created by : All Subject Handling Staff Members
Date : 25.9.2025
4
Table of Contents
Page
[Link]. Topic
No.
1 Course Objectives 6
2 Syllabus 7
3 Pre-requisites 10
4 Course Outcomes 11
5 CO- PO Mapping 12
6 Lecture Plan 13
7 Activity-based learning 14
9 Assignments I - V 47- 51
11 Part B Questions 55
13 Real-Time Applications 57
15 Assessment Schedule 61
5
1. Course Objectives
OBJECTIVES:
The Course will enable learners to:
Gain a comprehensive understanding of the Java Collections Framework and its various
Learn the details of Java I/O streams and utility classes for managing dates, numbers, and
currencies.
Develop a thorough understanding of the Stream API introduced in Java 8 and its various
operations.
Explore advanced object serialization and string tokenizing techniques, including pattern
Understand advanced Stream API features and gain proficiency in using regular expressions
6
2. Syllabus
24CS302 ADVANCED JAVA PROGRAMMING (LAB INTEGRATED) L T P C
3 0 3 4.5
UNIT I COLLECTIONS FRAMEWORK AND UTILITY CLASSES 9+9
Introduction to Collections Framework - Collection Interface- Methods in Collection
Interface - Iterable and Iterator Interfaces - List Interface- ArrayList - LinkedList - Set
Interface - HashSet- LinkedHashSet - TreeSet - Map Interface - HashMap -
LinkedHashMap- TreeMap - Queue Interface -PriorityQueue - Deque Interface - Utility
Classes.
List of Experiments
1. Write a program that measures the time taken for insertion, deletion, and search
operations on ArrayList, LinkedList, HashSet, and TreeSet for varying sizes of input
data.
2. Implement a custom data structure that combines features of a list and a set.
3. Write a Java program to create a HashMap where the keys are strings, and the values
are integers Add five key-value pairs to the map. Print all the keys and values in the
map. Remove an entry by key. Update the value associated with a specific key. Check
if the map contains a specific key and a specific value.
7
UNIT III STREAM API AND FUNCTIONAL PROGRAMMING 9+9
PARADIGMS
Overview of Stream API - Importance of Stream API in Java 8 and Beyond – Functional
Programming Concepts - Creating Streams - Stream Interface Methods - Stream
Operations - Intermediate Filtering (filter)-Mapping (map, flatMap)-Sorting (sorted)-
Distinct (distinct) - Limit and Skip (limit, skip) - Terminal Operations -Collecting Results
(collect) - Reducing and Summarizing (reduce, summaryStatistics)-Iterating (forEach) -
Matching and Finding (anyMatch, allMatch, noneMatch, findFirst, findAny) -Counting
(count).
List of Experiments
1. Write a program that performs stream operations like filtering, mapping, and
reducing.
2. Create an infinite stream generator that generates prime numbers. Implement
methods to check for primality and generate the next prime number.
3. Write a program that reads a text file containing sentences. Tokenize each sentence
into words, filter out stopwords, and print the remaining words.
8
UNIT V ADVANCED STREAM FEATURES AND REGULAR EXPRESSIONS 9+9
Importance and Use Cases of Advanced Stream Features - Creating Custom Streams -
Stream Generators ([Link], [Link]) - Infinite Streams - Using
Spliterators – Advanced Stream Operations - FlatMapping - Chaining Stream Operations
- Stream Peeking (peek) - Advanced Filtering Techniques - Introduction to Regular
Expressions - Character Classes - Quantifiers - Pattern Matching - Groups and Capturing
- Regex in Java - [Link] Package Pattern Class - Matcher Class - String
Manipulation with Regex - Splitting Strings - Replacing Text (replaceAll, replaceFirst) -
Replacing with Backreferences.
List of Experiments
1. Implement custom stream generators using [Link] and [Link]
methods.
2. Write a program that demonstrates advanced stream operations like flatMapping,
chaining stream operations, and peeking.
3. Develop a program that utilizes regular expressions to perform string manipulation
tasks such as splitting strings, replacing text, and extracting specific patterns.
TEXT BOOKS:
1. Cay S. Horstmann, "Core Java Volume I--Fundamentals," 12th Edition, 2019.
2. Joshua Bloch, "Effective Java," 3rd Edition, 2018.
[Link]-Gabriel Urma, "Java 8 in Action: Lambdas, Streams, and Functional-Style
Programming," 1st Edition, 2014. (Java 8 in Action ([Link])
REFERENCES:
1. Bruce Eckel, "Thinking in Java," 4th Edition, 2006.
2. Herbert Schildt, "Java: A Beginner's Guide," 8th Edition, 2019.
3. Richard Warburton, "Java 8 Lambdas: Pragmatic Functional Programming”,
1st Edition, 2014.
9
3. Pre-requisites
10
4. Course Outcomes
At the end of this course, the students will be able to:
CO5 Effectively use regular expressions for advanced text processing tasks. K3
11
5. CO-PO Mapping
Program Status
Program Outcomes
Course Outcomes
Outcomes PO PO PO PO PO PO PO PO PO PO PO PO PSO PSO PSO
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3
CO1 3 3 3 - - - - - - - - - 3 2 2
CO2 3 2 2 - - - - - - - - - 3 3 2
CO3 3 2 2 - - - - - - - - - 3 3 2
CO4 3 2 2 - - - - - - - - - 3 3 2
CO5 3 2 2 - - - - - - - - - 3 3 2
CO6 3 2 2 - - - - - - - - - 3 3 2
12
6. LECTURE PLAN
Sl. Taxono
No. of. Proposed Actual Pertaining Mode of
No Topics my
. Periods Date Date CO Level Delivery
Introduction to
As per
Collections As per Time
Framework - Time table table MD 1,
1,2 Collection 2 & CO1, CO6 K3
Interface- Methods & MD 4
Academic Academic
in Collection Calendar
Interface Calendar
As per
As per
Time
Time table
Iterable and table MD 1,
3 1 & & CO1,CO6 K3
Iterator Interfaces MD 4
Academic Academic
Calendar Calendar
As per
As per
Time
List Interface- Time table
table MD 1,
4 ArrayList - 1 & & CO1, CO6 K3 MD 4
LinkedList Academic Academic
Calendar Calendar
As per
As per
Set Interface - Time
Time table
HashSet- table MD 1,
5 1 & & CO1, CO6 K3
LinkedHashSet - MD 4
Academic Academic
TreeSet
Calendar Calendar
As per
As per
Map Interface - Time
Time table
HashMap - table MD 1,
6,7 2 & & CO1, CO6 K3
LinkedHashMap- MD 4
Academic Academic
TreeMap
Calendar Calendar
As per
As per
Time
Queue Interface - Time table
table MD 1,
8 PriorityQueue - 1 & & CO1, CO6 K3 MD 4
Deque Interface Academic Academic
Calendar Calendar
As per
As per
Time
Time table
table MD 1,
9 Utility Classes. 1 & & CO1, CO6 K3 MD 4
Academic Academic
Calendar Calendar
As per
As per
Time
10 Time table
table
- Lab Exercises 9 & & CO1, CO6 K3 iamneo
18 Academic Academic
Calendar Calendar
* MD1 – Oral Presentation
13
* MD 4 – Hands on using any Java IDE
7. Activity Based Learning
Common Activities
14
Crossword Puzzle Clues
Across
A stream operation that flattens nested lists into a single stream. (7 letters)
Down
A Java interface used to split data for parallel stream processing. (11 letters)
1 2 3 4 5 6 7 8 9 10
A__________
B__________
C__________
D__________
E__________
F__________
G__________
H__________
I__________
J__________
15
8. Lecture Notes
5.1 Importance and Use Cases of Advanced Stream Features :
Java’s Stream API, introduced in Java 8, has significantly enhanced how developers handle collections
and data processing. Here are some key points on the importance and use cases of advanced stream
features:
Efficiency and Performance: Streams allow for efficient data processing by enabling operations like
filtering, mapping, and reducing without the need for explicit loops.
Parallel Processing: Streams support parallel execution, which can significantly improve performance
for large datasets by leveraging multi-core processors.
Functional Programming: Streams promote a functional programming style, which can lead to more
predictable and maintainable code by avoiding mutable state and side effects.
Expressiveness: The Stream API provides a rich set of operations that can be combined in a fluent
manner, making the code more expressive and easier to understand.
Use Cases of Advanced Stream Features
[Link] Transformation: Streams can be used to transform data from one form to another. For
example, converting a list of stings to uppercase:
List<String> upperCaseNames = [Link]()
.map(String::toUpperCase)
.collect([Link]());
2. Filtering Data: Streams make it easy to filter data based on certain criteria. For instance, filtering
out even numbers from a list:
16
8. Lecture Notes
[Link] Processing: Streams can be processed in parallel to improve performance. For
Java
int parallelSum = [Link]()
.mapToInt(Integer::intValue)
.sum();
[Link] Data Pipelines: Streams can be used to create complex data processing pipelines.
Java
List<String> result = [Link]()
.filter(d -> [Link]("A"))
.map(String::toLowerCase)
.collect([Link]());
Java's stream API, introduced in Java 8, has become a powerful tool for processing sequences of
elements in a declarative and functional style. Advanced stream features enhance this capability,
offering more flexibility, performance, and expressiveness in handling data collections.
Use Cases:
Data Transformation: Converting a list of objects into another form, such as mapping user
entities to DTOs (Data Transfer Objects).
Filtering Data: Easily filtering large datasets based on specific criteria, such as finding all
active users or filtering transactions above a certain threshold.
2. Parallel Processing
Importance: Streams can be processed in parallel to leverage multi-core processors, which
improves performance when dealing with large data sets.
Use Cases:
Batch Processing: Handling large batches of data, such as processing millions of records in a
database.
Big Data: Performing complex computations over large datasets, like summing up values or
aggregating statistics.
8. Lecture Notes
3. Lazy Evaluation
Importance: Streams in Java are lazily evaluated, meaning that operations on streams are
only executed when a terminal operation is invoked. This can lead to performance
optimizations.
Use Cases:
Efficient Data Pipelines: Building pipelines that only process as much data as needed, for
example, finding the first match in a large dataset.
Resource Management: Minimizing resource usage by processing data on-demand rather
than upfront, such as reading lines from a large file only as needed.
Use Cases:
Event Handling: Using streams in event-driven systems to handle and process events
functionally.
Custom Collectors: Creating custom collectors for complex data aggregations, such as
grouping and partitioning data.
Use Cases:
Logging: Using peek() to log stream elements as they are processed, which aids in
debugging complex streams.
Testable Pipelines: Breaking down stream operations into smaller, testable units, making it
easier to verify the correctness of each stage.
Use Cases:
Custom Collectors: Implementing custom collectors for specific aggregation needs, such as
collecting statistics with custom rules.
Spliterators: Creating custom spliterators for non-standard data sources, like iterating over
data structures that aren't inherently sequential.
8. Lecture Notes
5.2 Creating Custom Streams:
Creating custom streams in Java involves defining a stream from non-standard data sources or operations
that are not directly supported by the standard Stream API. This process typically involves the use of
Spliterator, [Link], or [Link] methods.
3. Using [Link]()
[Link](): Creates an infinite stream where each element is produced by applying a function to the
previous element.
}
@Override
public boolean tryAdvance(Consumer<? super Integer> action) {
if (index < [Link]) {
[Link](array[index++]);
return true;
}
return false;
}
8. Lecture Notes
@Override
public Spliterator<Integer> trySplit() {
int length = [Link] - index;
if (length <= 1) return null;
int splitIndex = length / 2 + index;
Spliterator<Integer> spliterator = new CustomSpliterator([Link](array, index,
splitIndex));
index = splitIndex;
return spliterator;
}
@Override
public long estimateSize() {
return [Link] - index;
}
@Override
public int characteristics() {
return SIZED | SUBSIZED | ORDERED;
}
}
public class CustomStream {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
Spliterator<Integer> spliterator = new CustomSpliterator(array);
Stream<Integer> stream = [Link](spliterator, false);
[Link]([Link]::println);
}
}
8. Lecture Notes
5.3 Infinite Streams
In Java, an infinite stream is a stream that produces an unbounded number of elements, which
means it has no predefined limit.
You typically create infinite streams when you need to generate an endless sequence of elements
or a sequence that depends on some continuous conditions, like random numbers or the Fibonacci
series.
There are mainly two methods defined in the Stream class to create infinite streams and both are
static methods defined in the Stream class.
Java's Stream API provides ways to create infinite streams using methods like [Link]()
and [Link]().
[Link](): This method creates an infinite stream where each element is
generated using a Supplier. The supplier function is invoked to produce each element, and the
stream doesn't stop unless you limit it manually.
static <T> Stream<T> generate(Supplier<T> s)
Stream<Double> randomNumbers = [Link](Math::random);
[Link](5).forEach([Link]::println);
In this example, we generate an infinite stream of random numbers but limit it to 5 elements
using limit().
2. [Link]() : This method generates an infinite stream where each element is generated
based on the previous one. You provide an initial seed (starting value) and a unary operator that
computes the next value from the previous one.
static <T> Stream<T> iterate(T seed,UnaryOperator<T> fn)
Stream<Integer> evenNumbers = [Link](0, n -> n + 2);
[Link](5).forEach([Link]::println);
This example generates numbers from 1 to 10, effectively making it a finite stream.
Use Cases:
Random number generation: Infinite streams are helpful when you need to continuously
generate random data.
Fibonacci sequence: A classic example where infinite streams can be used to generate the
Fibonacci sequence.
Date/time sequence generation: You can create streams of incrementing dates or timestamps.
8. Lecture Notes
Example: Infinite Fibonacci Sequence:
import [Link];
import [Link]; // For holding two values together
Spliterator is an advanced feature used when you need control over how your data is
divided and processed in parallel streams. Java's collection classes (e.g., ArrayList, HashSet)
come with built-in spliterators that are tuned for optimal performance.
[Link] = start;
[Link] = end;
}
@Override
public boolean tryAdvance(Consumer<? super Integer> action) {
if (start < end) {
[Link](array[start++]);
return true;
}
return false;
}
@Override
public Spliterator<Integer> trySplit() {
int length = end - start;
if (length <= 1) {
return null; // No more splits possible
}
8. Lecture Notes
int mid = start + length / 2;
Spliterator<Integer> newSpliterator = new ArraySpliterator(array, start, mid);
start = mid; // Adjust the current spliterator to the second half
return newSpliterator;
}
@Override
public long estimateSize() {
return end - start;
}
@Override
public int characteristics() {
return ORDERED | SIZED | SUBSIZED;
}
}
Intermediate operations
Terminal operations
Short-circuit operations
Java Streams API offers many advanced operations to manipulate and process data efficiently. Let's
explore some of the more advanced stream operations beyond basic ones like map(), filter(), and
forEach().
flatMap():
flatMap() is used when you have a stream of collections (e.g., lists, arrays) and you want to flatten
them into a single stream. It helps in transforming nested structures into a flat structure.
List<List<String>> namesList = [Link](
[Link]("John", "Jane"),
[Link]("Tom", "Jerry"),
[Link]("Alice", "Bob")
);
reduce(): reduce() performs a reduction on the elements of the stream using an associative
accumulation function. It helps in reducing the stream elements into a single result, such as
summing or concatenating values.
collect() with Custom Collectors: While collect() is commonly used with built-in collectors
like [Link]() or [Link](), you can also define custom collectors to perform
more advanced operations.
[Link](groupedByLength);
peek(): peek() is used for debugging or intermediate actions during stream processing. It
allows you to perform an action on each element as it's passed through, without modifying the
elements.
List<String> names = [Link]("John", "Jane", "Tom", "Jerry");
[Link]()
[Link](partitioned);
skip() and limit(): These operations are often combined to implement pagination or selective data
[Link](n): Skips the first n [Link](n): Limits the stream to the first n elements.
List<String> items = [Link]("item1", "item2", "item3", "item4", "item5", "item6");
));
[Link](unmodifiableNames);
8. Lecture Notes
Parallel Streams: Parallel Streams can run stream operations in parallel using
.parallelStream(). However, it should be used carefully for operations that are CPU-bound and
where the ordering of elements isn't important.
List<Integer> numbers = [Link](1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
int sum = [Link]()
.reduce(0, Integer::sum);
[Link]("Sum: " + sum);
Collect():
The collect() method in Java Streams is a terminal operation used to transform the
elements of a stream into a different form, usually a collection (like a List, Set, or Map), or
to perform some kind of aggregation (such as summing or concatenating elements).
It is extremely flexible and can be customized to suit various requirements using
predefined or custom collectors.
<R, A> R collect(Collector<? super T, A, R> collector);
Where:
T is the type of elements in the stream.
A is the type of the accumulator used in the collector (intermediate data).
R is the type of the result (e.g., List, Set, Map).
Joining Strings: we can concatenate the elements of a stream into a single string using
[Link]().
String concatenated = [Link]("Alice", "Bob", "Charlie")
.collect([Link](", "));
[Link](concatenated);
Collecting into a Map: We can collect elements into a Map using [Link]().
The key and value can be determined by mapping functions.
import [Link];
}}
Output:
Filtered: Alice
Filtered: Charlie
Filtered: David
Mapped: ALICE
Mapped: CHARLIE
Mapped: DAVID
It’s a powerful tool that allows you to sift through data and extract what you need.
// Output:
// [John]
This is just a basic usage of the Java Stream filter method. But there’s so much more you can do
with it, including chaining multiple filters and using complex predicates. Continue reading for a
more detailed explanation and advanced usage scenarios.
8. Lecture Notes
Advanced Techniques with Java Stream Filter:
Chaining Multiple Filters:
List<Integer> numbers = [Link](1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> result = [Link]()
.filter(n -> n % 2 == 0)
.filter(n -> n > 5)
.collect([Link]());
// Output:
// [6, 8, 10]
First filter out the even numbers, and then we filter out the numbers that are greater than 5. The
result is a list of even numbers greater than 5.
You can combine multiple conditions using logical operators (e.g., &&, ||) in the filter() method.
import [Link];
import [Link];
public class MultiConditionFilter {
public static void main(String[] args) {
List<String> names = [Link]("John", "Paul", "George", "Ringo");
List<String> filteredNames = [Link]()
// Output:
// [John]
8. Lecture Notes
Using Loops for Filtering
List<Integer> numbers = [Link](1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> evenNumbers = new ArrayList<>();
The Java Regex or Regular Expression is an API to define a pattern for searching or
manipulating strings.
It is widely used to define the constraint on strings such as password and email validation. After
learning Java regex tutorial, you will be able to test your regular expressions by the Java Regex
Tester Tool.
Java Regex API provides 1 interface and 3 classes in [Link] package.
The Matcher and Pattern classes provide the facility of Java regular expression. The
[Link] package provides following classes and interfaces for regular expressions.
MatchResult interface
Matcher class
Pattern class
PatternSyntaxException class
Matcher class
It implements the MatchResult interface. It is a regex engine which is used to perform match
operations on a character sequence.
No. Method Description
1 boolean matches() test whether the regular expression matches the pattern.
2 boolean find() finds the next expression that matches the pattern.
3 boolean find(int start) finds the next expression that matches the pattern from the
given start number.
4 String group() returns the matched subsequence.
5 int start() returns the starting index of the matched subsequence.
6 int end() returns the ending index of the matched subsequence.
7 int groupCount() returns the total number of the matched subsequence.
8. Lecture Notes
Character Classes :
In the context of regular expressions, a character class is a set of characters enclosed within
square brackets. It specifies the characters that will successfully match a single character from a
given input string.
Regular expressions (also called “Regex”) are special types of pattern-matching strings that
describe a pattern in a text. A regex can be matched against another string to see whether the
string fits the pattern. In general, regex consists of normal characters, character classes, wildcard
characters, and quantifiers.
At times there’s a need to match any sequence that contains one or more characters, in any
order, that is part of a set of characters. For example, to match whole words, we want to match
any sequence of the letters of alphabets.
Character classes come in handy for such use-cases. A character class is a set of characters such
that characters are put between square brackets ‘[‘ and ‘]’. For example, class [abc] matches a, b,
c characters.
A range of characters can also be specified using hyphens. For example, to match whole words of
lowercase letters, the [a-z] class can be used.
Note that a character class has no relation with a class construct or class files in Java. Also, the
word “match” means a pattern exists in a string, it does not mean the whole string matches the
pattern. A regex pattern lets us use two types of character classes:
• Predefined character classes and
• Custom character classes
Predefined Character Classes
Some frequently used character classes are predefined in Java, these are listed below. Characters
in these classes are usually proceeded with a backslash “\” and need not reside in brackets “[”
and “]”.
8. Lecture Notes
Predefined
Meaning of predefined character classes
character classes
This special character dot (.) matches any character. One dot matches one
(.dot) (any) character, two dots match two characters and so on. Dot characters
may or may not match line terminators.
This matches any digit character. This works the same as the character
\d
class [0-9].
This matches any character except for digits. This works the same as the
\D
character class [^0-9].
This matches any whitespace character. This includes a space ‘ ‘, a tab ‘\t’,
\s a new line ‘\n’, a vertical tab ‘\x0B’, a form feed ‘\f’, a carriage return ‘\r’
and backspace character ‘\b’.
This matches any character except for the whitespace characters listed
\S
above.
This matches any word character, including both uppercase and lowercase,
\w also including digit characters and the underscore character ‘_’. This works
the same as the class [a-zA-z_0-9].
This matches any character except for word characters. This works the
\W
same as the class [^a-zA-z_0-9].
8. Lecture Notes
Custom Character Classes:
Example of custom
character class Meaning of the custom character class
This regex means pattern must start with “b” followed by any of the
vowels “a”, “e”, “i”, “o”, “u” followed by “t”.
b[aeiou]t
Strings “bat”, “bet”, “bit”, “bot”, “but” would match this regex, but “bct”,
“bkt”, etc. would not match.
Such a regex can be used to allow uppercase letters too in the previous
[bB][aAeEiIoOuU][tT]
regex. So the strings “bAT”, “BAT”, etc. would match the pattern.
Range of letters and digits can be specified in character classes using the
[a-z][0-3] hyphen “-“. Strings “a1”, “z3”, etc. match the pattern. Strings “k7”, “n9”,
etc. does not match.
More than one range can be used in a class. Strings “A1”, “b2”, etc.
[a-zA-Z][0-9]
match the pattern.
Nesting character classes simply add them, so this class is the same as
[A-F[G-Z]]
[A-Z] class.
Placing an asterisk “*” after a character class means “allow any number of occurrences of
that character class”. For example, “0*\d” regex matches any number of leading zeroes
followed by a digit.
“+” plus sign has the same effect as XX*, meaning a pattern followed by pattern asterisk.
For example, “0+\d” regex matches at least one leading zeroes followed by a digit.
“?” question mark sign allows either zero or one occurrence. For example, “\w\w-?\d\d”
regex matches 2-word characters followed by an optional hyphen and then followed by 2
digit characters.
Pattern Matching:
Pattern Class
This class is a compilation of regular expressions that can be used to define various
types of patterns, providing no public constructors. This can be created by invoking the
compile() method which accepts a regular expression as the first argument, thus
returning a pattern after execution.
8. Lecture Notes
Pattern Class Methods:
S.
No. Method Description
// Main class
class GFG {
[Link]([Link](
"geeksforge*ks", "geeksforgeeks"));
Output
true
false
8. Lecture Notes
Matcher class
This object is used to perform match operations for an input string in Java, thus interpreting the
previously explained patterns. This too defines no public constructors. This can be implemented by
invoking a matcher() on any pattern object.
It is used to test
whether the regular
7. matches() expression matches the
pattern.
8. Lecture Notes
// Main class
class GFG {
Capturing Groups
Capturing groups are a way to treat multiple characters as a single unit. They are created
by placing the characters to be grouped inside a set of parentheses.
For example, the regular expression (dog) creates a single group containing the letters "d",
"o", and "g".
Capturing groups are numbered by counting their opening parentheses from the left to the
right. In the expression ((A)(B(C))), for example, there are four such groups −
((A)(B(C)))
(A)
(B(C))
(C)
8. Lecture Notes
To find out how many groups are present in the expression, call the groupCount method on a
matcher object. The groupCount method returns an int showing the number of capturing
groups present in the matcher's pattern.
There is also a special group, group 0, which always represents the entire expression. This
group is not included in the total reported by groupCount.
Example
Following example illustrates how to find a digit string from the given alphanumeric string −
import [Link];
import [Link];
if ([Link]()) {
[Link]("Year: " + [Link](1)); // Output: Year: 1995
[Link]("Month: " + [Link](2)); // Output: Month: 06
[Link]("Day: " + [Link](3)); // Output: Day: 15
}
}
}
import [Link];
public class RegexSplitExample {
public static void main(String[] args) {
String text = "apple,banana;orange|grape";
String regex = "[,;|]"; // Match commas, semicolons, or pipes
Replacement Methods :
[Link]
Method & Description
.
import [Link];
import [Link];
Example:
import [Link];
import [Link];
public class DateFormatReplace {
public static void main(String[] args) {
String date = "2023-10-02";
// Reorder using backreferences $2 for month, $3 for day, and $1 for year
String replacement = "$2/$3/$1";
}
}
9. Assignment - I
1. Write a Java program to create a custom stream of 10 random integers between 1 and 100
using [Link](). Then filter out numbers greater than 50 and display the remaining
numbers.
2. Use [Link]() to generate the first 10 even numbers. Print the generated stream
elements.
3. Create an infinite stream of random decimal numbers using [Link](). Use the limit()
method to restrict it to the first 20 numbers and then print them.
4. Given a list of strings, write a Java program to filter out strings starting with the letter ‘A’ and
having more than 5 characters using multiple filter() operations.
5. Write a program that flattens a List<List<Integer>> using flatMap() and converts it into a single
list of integers. Print the result.
48
9. Assignment - II
1. Create a list of numbers from 1 to 20. Using stream operations, filter out even numbers,
but use the peek() method to log each number as it passes through the stream before
printing the filtered numbers.
2. Write a program that takes an input string and uses regex to replace all occurrences of
multiple spaces with a single space. Also, remove any leading or trailing spaces.
3. Given a paragraph of text, use a regular expression to split the paragraph into sentences.
Consider punctuation marks like ., !, and ? as sentence delimiters.
4. Write a program that uses regex to find all email addresses in a given text file. The email
addresses should match the pattern username@[Link] and should be displayed
on the console.
5. Write a Java program that uses regex with backreferences to find sequences of two
consecutive identical words (e.g., "hello hello") and replace them with a single instance
(e.g., "hello").
49
9. Assignment - III
1. Write a Java program to create custom streams using both [Link]() and
[Link](). Demonstrate finite and infinite streams with suitable examples.
2. Explain the concept of Spliterators in Java. Implement a custom Spliterator that divides a list
of numbers into two sub-streams and processes them in parallel.
3. Write a program to demonstrate flatMap() by transforming a stream of lists into a flat stream
of elements. Use a real-world example, such as flattening a list of employees’ names grouped
by department.
4. Implement a text search application using regex in Java. The program should allow the user
to input a text and a regex pattern, and it should display all matching words.
5. Create a Java program that uses regex quantifiers (+, *, ?, {n,m}) to validate different types
of user input such as postal codes, phone numbers, or pin codes.
50
9. Assignment - IV
1. Write a program to demonstrate advanced filtering techniques on streams. For example, filter
numbers greater than 10, divisible by 3, and less than 100, then collect and print the results.
2. Use peek() in a Java stream pipeline to debug the processing of a list of student marks. Show
how the marks are transformed from raw scores to grades step by step.
3. Write a Java program using regex groups and capturing to extract date components (day,
month, year) from a string in the format dd-mm-yyyy.
4. Demonstrate the use of replaceAll() and replaceFirst() in regex for text manipulation. Write a
program to replace all vowels in a sentence with *, but replace only the first digit found in the
string with #.
5. Write a Java program that demonstrates splitting a string using regex. For example, split a CSV
line into fields where values may be separated by commas and optional spaces.
51
10. Part A Questions & Answers
1. What is the primary importance of using streams in Java? CO5, K1
Streams provide a functional programming approach to process sequences of elements in
a declarative manner, allowing for parallel operations, better readability, and concise code.
2. What is a custom stream in Java?. CO5, K1
A custom stream is a user-defined stream where developers create a stream using either
static methods like [Link](), [Link](), or by processing a collection of custom
objects.
3. What does [Link]() do in Java? CO5, K1
[Link]() creates an infinite sequential unordered stream where each element is
generated using the provided Supplier function..
4. What is the use of [Link]() in Java? CO5, K1
[Link]() generates an infinite stream starting from an initial value and applies a given
unary operator repeatedly to produce subsequent elements..
5. What are infinite streams in Java? CO5, K1
Infinite streams are streams that do not have a predefined size and continue to generate
elements either via methods like [Link]() or [Link]() until limited by operations
like limit().
6. What is the role of Spliterator in streams? CO5, K1
A Spliterator is used for splitting the stream into smaller parts for parallel processing and
provides characteristics such as estimated size and ordering for optimization.
7. What is flatMapping in Java Streams? CO5, K1
FlatMapping is the process of transforming each element of a stream into another stream
and then flattening the resulting streams into a single stream using methods like flatMap().
8. How can stream operations be chained in Java? CO5, K1
Stream operations like map(), filter(), and reduce() can be chained by applying multiple
intermediate operations in sequence, resulting in a combined pipeline of operations on the stream
elements.
9. What is stream peeking, and how is peek() used? CO5, K1
Stream peeking is the ability to inspect the elements of a stream during its traversal without
modifying them. The peek() method is used to perform an action on each element as it passes
through the pipeline.
10. Give an example of an advanced filtering technique in streams. CO5, K1
An example of advanced filtering is combining multiple filter() methods to apply complex
conditional logic, such as filter(e -> [Link]("A") && [Link]() > 5).
11. What is a regular expression (regex)? CO5, K2
A regular expression is a sequence of characters that defines a search pattern used for
matching, locating, and manipulating text.
10. Part A Questions & Answers
12. What is the role of character classes in regular expressions? CO5, K2
Character classes define a set of characters to match, such as [a-z] for any
lowercase letter or \d for any digit.
19. How can regex be used for string manipulation in Java? CO5, K2
Regex can be used to manipulate strings by matching patterns for replacing, splitting, or
extracting specific parts of the string using methods like replaceAll() and split().
20. How do you split a string using a regex pattern in Java? CO5, K2
The split() method from the String class or the Pattern class can be used to split a string
into an array based on a regex delimiter.
21. What does replaceAll() do with regex in Java? CO5, K2
The replaceAll() method replaces all occurrences of the regex pattern in the input
string with a given replacement string.
53
10. Part A Questions & Answers
23. What are backreferences in regular expressions? CO5 ,K2
Backreferences refer to previously captured groups in a regex pattern and allow
matching the same text again within the same regex pattern.
24. What is the use of backreferences in the replaceAll() method in Java?
CO5 ,K2
Backreferences can be used in the replacement string of replaceAll() to reuse
parts of the matched pattern in the replacement, for example, replaceAll("(\\w)(\\w)",
"$2$1") swaps adjacent characters.
54
11. Part B Questions
1. Explain the importance of Advanced Stream Features in Java and discuss how
custom streams are created using [Link]() and [Link](). Provide
an example for each. (K3,CO5)
2. What are Infinite Streams in Java? How are Spliterators used with streams? Explain
their significance with an example. (K3,CO5)
3. Discuss FlatMapping in Java Streams. How does it differ from simple mapping?
Provide a use case where flatMap() is essential. (K3,CO5)
4. What is the peek() method in Java Streams? Discuss its uses and potential pitfalls.
Provide an example of how peek() can be utilized in a stream pipeline. (K3,CO5)
5. Explain the concept of regular expressions (regex) in Java and its key components
(character classes, quantifiers, groups). How does the Pattern and Matcher class
facilitate regex processing in Java? Provide an example of regex pattern matching
and string manipulation using replaceAll(). (K3,CO5)
6. Discuss Advanced Filtering Techniques in Java Streams. How do predicates help in
filtering, and how can multiple conditions be applied? Provide an example
demonstrating multiple filtering criteria using predicates. (K3,CO5)
12. Supportive Online Certifications
56
13. Real Time Applications
Mobile Applications
Artificial intelligence
Gaming Applications
Business Applications
Embedded Systems
Scientific Applications
Distributed Applications
Software Tools
57
14. Contents Beyond the Syllabus
Reactive Programming with Streams
Reactive programming is a paradigm centered around asynchronous and non-blocking data streams.
It’s designed for building systems that need to handle high-throughput, real-time data with low
latency, like user interfaces, server-side applications, and reactive systems.
Java's standard Stream API provides a powerful framework for functional and declarative data
processing, but it is primarily synchronous and pull-based—meaning the data is consumed when
requested. Reactive programming, on the other hand, emphasizes asynchronous, event-driven
streams that are push-based.
In this context, reactive streams allow you to process data as it arrives, enabling you to handle large
or infinite data streams in a way that is highly efficient and responsive.
What is Reactive Programming?
Reactive programming revolves around:
Asynchronous data flow: Data is processed as it becomes available rather than waiting for the entire
dataset.
Non-blocking execution: The system continues to work even when waiting for data, which allows it to
scale better under high load.
Event-driven systems: Reacting to changes, events, or data emissions in real-time.
The key features of reactive programming include:
Streams of data: Where sequences of events or data are emitted over time.
Backpressure: A mechanism to control the flow of data between producers and consumers, ensuring
consumers are not overwhelmed.
Concurrency and parallelism: Reactive systems efficiently handle multiple operations simultaneously.
Error handling: Errors are considered as part of the data stream and can be handled gracefully without
interrupting the whole system.
Akka Streams: A toolkit built on top of Akka’s actor system, useful for distributed, reactive systems.
Vert.x: A toolkit for building reactive applications on the JVM. It is polyglot, supporting Java, Kotlin,
Scala, etc. 58
14. Contents Beyond the Syllabus
The Reactive Streams API in Java (introduced in Java 9) provides the foundation for
building reactive systems. It consists of four main interfaces:
The main goal of Reactive Streams is to handle backpressure, which allows the
subscriber to request only as much data as it can handle from the publisher.
import [Link];
import [Link];
import [Link];
@Override
public void cancel() {
[Link]("Subscription cancelled");
}
});
};
59
14. Contents Beyond the Syllabus
Subscriber<Integer> subscriber = new Subscriber<>() {
@Override
public void onSubscribe(Subscription subscription) {
[Link](5); // Requesting 5 items from the publisher
@Override
public void onNext(Integer item) {
[Link]("Received: " + item);
}
@Override
public void onError(Throwable t) {
[Link]("Error: " + [Link]());
@Override
public void onComplete() {
[Link]("Done");
}
};
[Link](subscriber);
}
}
In this simple example, a Publisher generates a stream of integers, and a Subscriber requests
and consumes those integers. The subscriber can control how much data it wants to process via
backpressure ([Link](5)).
60
15. Assessment Schedule
Tentative schedule for the Assessment During 2024-2025 Odd Semester
S. Name of the
Assessment Start Date End Date Portion
No.
61
16. Prescribed Text Books
&
Reference Books
TEXT BOOK:
1. Cay S. Horstmann, "Core Java Volume I--Fundamentals,"
12th Edition, 2019.
62
17. Mini Project - Suggestions
1. Log File Analyzer with Streams, Spliterators, and Regex
Develop a Java application that processes large log files. Use Spliterators to split the file into sub-
streams for parallel processing, Streams to filter logs by date range, severity level (INFO, WARN,
ERROR), and keywords, and Regex to extract timestamps and IP addresses. Generate a summary
report with statistics (e.g., total errors, most frequent log messages).
87
Thank you
Disclaimer:
This document is confidential and intended solely for the educational purpose of
RMK Group of Educational Institutions. If you have received this document through
email in error, please notify the system manager. This document contains
proprietary information and is intended only to the respective group / learning
community as intended. If you are not the addressee you should not disseminate,
distribute, or copy through e-mail. Please notify the sender immediately by e-mail if you
have received this document by mistake and delete this document from your system.
If you are not the intended recipient you are notified that disclosing, copying,
distributing, or taking any action in reliance on the contents of this information is strictly
prohibited.
90