Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

This repository demonstrates various ways to implement functional interfaces in Java using lambda expressions, anonymous classes, and regular classes.

Notifications You must be signed in to change notification settings

akyabhishek/DecodeJava

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

15 Commits

Repository files navigation

DecodeJava

A comprehensive Java learning repository demonstrating modern Java features including lambda expressions, functional interfaces, streams, optionals, method references, exception handling, and multithreading.

πŸ“‹ Table of Contents

πŸ” Overview

DecodeJava is an educational project that showcases various Java 8+ features with practical examples and demonstrations. This repository serves as a comprehensive learning resource for developers looking to understand functional programming concepts, exception handling mechanisms, and multithreading patterns in Java.

✨ Features

  • Lambda Expressions: Multiple implementation approaches (traditional classes, anonymous classes, and lambda expressions)
  • Functional Interfaces: Built-in Java functional interfaces (Predicate, Consumer, Function, Supplier)
  • Stream API: Data processing with streams, filtering, mapping, and collecting
  • Optional Class: Handling null values effectively
  • Method References: Constructor references and method references
  • Comparators: Custom sorting with Comparable and Comparator interfaces
  • Exception Handling: Try-catch, custom exceptions, try-with-resources, and exception hierarchy
  • Multithreading: Thread creation, synchronization, executors, locks, and producer-consumer patterns

πŸ“ Project Structure

DecodeJava/
β”œβ”€β”€ src/
β”‚ β”œβ”€β”€ Main.java # Entry point
β”‚ β”œβ”€β”€ compare/ # Comparison demonstrations
β”‚ β”‚ β”œβ”€β”€ comparable/ # Comparable interface examples
β”‚ β”‚ β”‚ β”œβ”€β”€ ComparableDemo.java
β”‚ β”‚ β”‚ └── Employee.java
β”‚ β”‚ └── comparator/ # Comparator interface examples
β”‚ β”‚ β”œβ”€β”€ ComparatorDemo.java
β”‚ β”‚ β”œβ”€β”€ Employee.java
β”‚ β”‚ β”œβ”€β”€ IdComparator.java
β”‚ β”‚ └── SalaryComparator.java
β”‚ β”œβ”€β”€ exceptionHandling/ # Exception handling examples
β”‚ β”‚ β”œβ”€β”€ TryCatch.java # Basic try-catch blocks
β”‚ β”‚ β”œβ”€β”€ TryCatchFinally.java # Finally block usage
β”‚ β”‚ β”œβ”€β”€ TryWithResource.java # Try-with-resources
β”‚ β”‚ β”œβ”€β”€ MultipleCatch.java # Multiple exception handling
β”‚ β”‚ β”œβ”€β”€ ThrowAndThrows.java # Throw and throws keywords
β”‚ β”‚ β”œβ”€β”€ CustomException.java # Creating custom exceptions
β”‚ β”‚ β”œβ”€β”€ MyCustomException.java # Custom exception class
β”‚ β”‚ └── hierarchy.md # Exception hierarchy diagram
β”‚ β”œβ”€β”€ lambda/ # Lambda expression demonstrations
β”‚ β”‚ β”œβ”€β”€ FuncInter.java
β”‚ β”‚ β”œβ”€β”€ FuncInterImp.java
β”‚ β”‚ β”œβ”€β”€ LambdaDemo.java
β”‚ β”‚ β”œβ”€β”€ SumInter.java
β”‚ β”‚ β”œβ”€β”€ ThreadCreationLambdaDemo.java
β”‚ β”‚ └── functionalInterfaces/ # Built-in functional interfaces
β”‚ β”‚ β”œβ”€β”€ ConsumerInterface.java # Consumer examples
β”‚ β”‚ β”œβ”€β”€ FunctionInterface.java # Function examples
β”‚ β”‚ β”œβ”€β”€ PredicateInterface.java # Predicate examples
β”‚ β”‚ └── SupplierInterface.java # Supplier examples
β”‚ β”œβ”€β”€ multithreading/ # Multithreading demonstrations
β”‚ β”‚ β”œβ”€β”€ MyThreadByClass.java # Thread creation by extending Thread
β”‚ β”‚ β”œβ”€β”€ MyThreadByInterface.java # Thread creation using Runnable
β”‚ β”‚ β”œβ”€β”€ MyThreadDemo.java # Thread usage examples
β”‚ β”‚ β”œβ”€β”€ ThreadByLambda.java # Creating threads with lambda
β”‚ β”‚ β”œβ”€β”€ LifeCycleOfThread.java # Thread lifecycle states
β”‚ β”‚ β”œβ”€β”€ ThreadPriorityMethod.java # Thread priority management
β”‚ β”‚ β”œβ”€β”€ JoinBetweenThreads.java # Thread joining
β”‚ β”‚ β”œβ”€β”€ DaemonThread.java # Daemon thread examples
β”‚ β”‚ β”œβ”€β”€ ThreadCommunication.java # Inter-thread communication
β”‚ β”‚ β”œβ”€β”€ VolatileDemo.java # Volatile keyword usage
β”‚ β”‚ β”œβ”€β”€ AtomicDemo.java # Atomic variables
β”‚ β”‚ β”œβ”€β”€ executors/ # Executor framework
β”‚ β”‚ β”‚ β”œβ”€β”€ ThreadPoolDemo.java # Thread pool basics
β”‚ β”‚ β”‚ β”œβ”€β”€ FixedCachedThreadPool.java # Fixed and cached pools
β”‚ β”‚ β”‚ β”œβ”€β”€ RunnableNCallable.java # Runnable vs Callable
β”‚ β”‚ β”‚ β”œβ”€β”€ FutureDemo.java # Future interface
β”‚ β”‚ β”‚ β”œβ”€β”€ CallableWithInvokeAll.java # InvokeAll method
β”‚ β”‚ β”‚ β”œβ”€β”€ ScheduledExecutorServiceDemo.java
β”‚ β”‚ β”‚ └── ScheduledExecutorServiceDemoFixed.java
β”‚ β”‚ β”œβ”€β”€ locks/ # Locking mechanisms
β”‚ β”‚ β”‚ β”œβ”€β”€ LockDemo.java # Basic lock usage
β”‚ β”‚ β”‚ β”œβ”€β”€ ReentrantLockDemo.java # ReentrantLock examples
β”‚ β”‚ β”‚ β”œβ”€β”€ ReadWriteCounter.java # ReadWriteLock
β”‚ β”‚ β”‚ └── BankAccount.java # Thread-safe bank account
β”‚ β”‚ └── producerConsumer/ # Producer-Consumer pattern
β”‚ β”‚ β”œβ”€β”€ Demo.java # Main demo
β”‚ β”‚ β”œβ”€β”€ Company.java # Shared resource
β”‚ β”‚ β”œβ”€β”€ Producer.java # Producer thread
β”‚ β”‚ └── Consumer.java # Consumer thread
β”‚ β”œβ”€β”€ optional/ # Optional class usage
β”‚ β”‚ └── OptionalDemo.java
β”‚ β”œβ”€β”€ ref/ # Method references
β”‚ β”‚ β”œβ”€β”€ RefDemo.java
β”‚ β”‚ β”œβ”€β”€ Task.java
β”‚ β”‚ β”œβ”€β”€ TaskInter.java
β”‚ β”‚ └── constructor/ # Constructor references
β”‚ β”‚ β”œβ”€β”€ ConsMain.java
β”‚ β”‚ β”œβ”€β”€ OptionalClass.java
β”‚ β”‚ β”œβ”€β”€ Streams.java
β”‚ β”‚ β”œβ”€β”€ Student.java
β”‚ β”‚ └── StudentInter.java
β”‚ └── streams/ # Stream API examples
β”‚ β”œβ”€β”€ StreamMethods.java
β”‚ └── StreamsDemo.java
└── README.md

πŸš€ Getting Started

Prerequisites

  • Java 8 or higher
  • IDE (IntelliJ IDEA, Eclipse, or VS Code)
  • Basic understanding of Java programming

How to Run

  1. Clone the repository:

    git clone <repository-url>
    cd DecodeJava
  2. Open in your IDE:

    • Import the project into your preferred Java IDE
    • Ensure the JDK version is set to Java 8+
  3. Run individual examples:

    • Navigate to any demo class (e.g., LambdaDemo.java)
    • Run the main method to see the output

πŸ“š Topics Covered

1. Lambda Expressions (lambda/)

  • Traditional class implementation vs anonymous classes vs lambda expressions
  • Functional interface implementation
  • Parameter inference in lambda expressions
  • Thread creation using lambda expressions

2. Functional Interfaces (lambda/functionalInterfaces/)

  • Predicate: Testing conditions with test(), and(), negate()
  • Consumer: Consuming data without returning values
  • Function: Transforming input to output
  • Supplier: Generating/supplying values

3. Stream API (streams/)

  • Filtering collections with predicates
  • Data transformation and collection
  • Working with arrays and collections
  • Method chaining for data processing
  • Common stream operations (map, filter, reduce, collect)

4. Optional Class (optional/)

  • Avoiding null pointer exceptions
  • Safe value handling
  • Optional chaining and transformations

5. Method References (ref/)

  • Constructor references
  • Static method references
  • Instance method references
  • Integration with streams and functional interfaces

6. Comparisons (compare/)

  • Comparable: Natural ordering implementation
  • Comparator: Custom sorting strategies
  • Multiple comparison criteria

7. Exception Handling (exceptionHandling/)

  • Try-Catch Blocks: Basic exception handling
  • Try-Catch-Finally: Resource cleanup with finally
  • Try-With-Resources: Automatic resource management
  • Multiple Catch: Handling multiple exception types
  • Throw and Throws: Exception propagation
  • Custom Exceptions: Creating and using custom exception classes
  • Exception Hierarchy: Complete Java exception hierarchy diagram

8. Multithreading (multithreading/)

Basic Threading

  • Thread creation by extending Thread class
  • Thread creation using Runnable interface
  • Thread creation with lambda expressions
  • Thread lifecycle and states
  • Thread priority management
  • Thread joining (wait for thread completion)
  • Daemon threads

Thread Communication & Synchronization

  • Inter-thread communication (wait, notify, notifyAll)
  • Volatile keyword for visibility
  • Atomic variables for lock-free operations
  • Producer-Consumer pattern implementation

Executor Framework (multithreading/executors/)

  • Thread pools (fixed, cached, scheduled)
  • Runnable vs Callable
  • Future interface for asynchronous results
  • InvokeAll for batch task execution
  • ScheduledExecutorService for scheduled tasks

Locks (multithreading/locks/)

  • Basic Lock interface
  • ReentrantLock for advanced locking
  • ReadWriteLock for concurrent reads
  • Thread-safe implementations (e.g., BankAccount)

πŸ’‘ Examples

Lambda Expression Example

// Traditional approach
FuncInter funcInter = new FuncInterImp();
funcInter.sayHello();
// Lambda expression
FuncInter funcInter2 = () -> System.out.println("Hello from Lambda Expression");
funcInter2.sayHello();

Stream API Example

List<Integer> numbers = List.of(1,2,3,4,5,6,7,8,9,10);
List<Integer> evenNumbers = numbers.stream()
 .filter(num -> num % 2 == 0)
 .collect(Collectors.toList());

Predicate Example

Predicate<Integer> isEven = num -> num % 2 == 0;
Predicate<Integer> isPositive = n -> n > 0;
System.out.println(isEven.and(isPositive).test(2)); // true

Exception Handling Example

// Try-with-resources
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
 String line = br.readLine();
} catch (IOException e) {
 e.printStackTrace();
}
// Custom exception
throw new MyCustomException("Custom error message");

Multithreading Example

// Creating thread with lambda
Thread t = new Thread(() -> {
 System.out.println("Thread running: " + Thread.currentThread().getName());
});
t.start();
// Using ExecutorService
ExecutorService executor = Executors.newFixedThreadPool(5);
executor.submit(() -> System.out.println("Task executed"));
executor.shutdown();
// Using ReentrantLock
ReentrantLock lock = new ReentrantLock();
lock.lock();
try {
 // Critical section
} finally {
 lock.unlock();
}

🀝 Contributing

Feel free to fork this repository and submit pull requests to improve the examples or add new demonstrations of Java features.

πŸ“„ License

This project is open source and available under the MIT License.

πŸ“ž Contact

For questions or suggestions, please feel free to reach out or create an issue in the repository.


Happy Learning! πŸŽ“

About

This repository demonstrates various ways to implement functional interfaces in Java using lambda expressions, anonymous classes, and regular classes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /