Skip to main content

Command Palette

Search for a command to run...

JAVA 8 Coding Stuff

Published
•3 min read•View as Markdown

1️⃣ Lambdas & Functional Interfaces

Write a Comparator using a lambda to sort users by firstName.

Use Predicate to filter products with amount > 1000.

Use Function<User, String> to map users to their full name (firstName + secondName).

Create a custom FunctionalInterface DiscountCalculator and implement it with a lambda.

Use Supplier to supply the current time for order creation.

Use Consumer to print formatted user details.

Replace an anonymous inner class with a lambda in a Runnable example.

2️⃣ Streams – Basics

Fetch all product names using stream().map().

Get distinct product categories using map(...).distinct().

Find all users having more than one address.

Count how many products belong to Category = ELECTRONIC.

Get top 5 most expensive products.

Get all user phone numbers in a List.

Find users whose name starts with "A".

Sort addresses alphabetically.

3️⃣ Streams – Collectors

Collect all product names into a Set.

Convert list of users into a Map<id, firstName>.

Group products by category.

Find average price of products per category.

Partition products into expensive (> 500) and cheap.

Join all user names into a single comma-separated string.

Collect orders into a map of orderId → totalAmount.

Group addresses by user and count them.

4️⃣ Streams – Advanced

FlatMap all products from all orders into a single product list.

Find the highest-priced product using max().

Find the earliest order using min().

Check if any user has dob before 1990.

Check if all products have quantity > 0.

Find the first product in category GROCERY.

Skip the first 5 products and print the rest.

5️⃣ Optional

Wrap a User fetch in Optional and return default if not found.

Use Optional.map() to get user’s second name if present.

Use Optional.orElseThrow() when product not found.

Convert a nullable phone number into an Optional and print safely.

6️⃣ Method References

Replace a lambda with a static method reference (ClassName::methodName).

Replace a lambda with an instance method reference (obj::methodName).

Use constructor reference to create new User objects.

7️⃣ Date/Time API (java.time)

Convert dob (String) into LocalDate and calculate user’s age.

Find all orders placed in the last 30 days.

Find orders placed on weekends.

Format order time into dd-MM-yyyy HH:mm.

Add 7 days to order time and display expected delivery date.

8️⃣ Parallel Streams

Calculate total sales using parallel stream.

Fetch distinct product names using parallel stream.

Compare performance of sequential vs parallel stream on 10,000 products.

9️⃣ Practical + Combining Features

Write a query: Find the top 3 users who spent the most (Order.totalAmount) using streams.

For each category, find the most expensive product.

List all products ordered by users whose first name starts with "E".

Compute the total number of addresses across all users using reduce.

Implement caching using Optional + Supplier for fetching product by id.