A comprehensive Java learning repository demonstrating modern Java features including lambda expressions, functional interfaces, streams, optionals, method references, exception handling, and multithreading.
- Overview
- Features
- Project Structure
- Getting Started
- Topics Covered
- Prerequisites
- How to Run
- Examples
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.
- 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
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
- Java 8 or higher
- IDE (IntelliJ IDEA, Eclipse, or VS Code)
- Basic understanding of Java programming
-
Clone the repository:
git clone <repository-url> cd DecodeJava
-
Open in your IDE:
- Import the project into your preferred Java IDE
- Ensure the JDK version is set to Java 8+
-
Run individual examples:
- Navigate to any demo class (e.g.,
LambdaDemo.java) - Run the
mainmethod to see the output
- Navigate to any demo class (e.g.,
- Traditional class implementation vs anonymous classes vs lambda expressions
- Functional interface implementation
- Parameter inference in lambda expressions
- Thread creation using lambda expressions
- Predicate: Testing conditions with
test(),and(),negate() - Consumer: Consuming data without returning values
- Function: Transforming input to output
- Supplier: Generating/supplying values
- 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)
- Avoiding null pointer exceptions
- Safe value handling
- Optional chaining and transformations
- Constructor references
- Static method references
- Instance method references
- Integration with streams and functional interfaces
- Comparable: Natural ordering implementation
- Comparator: Custom sorting strategies
- Multiple comparison criteria
- 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
- 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
- Inter-thread communication (wait, notify, notifyAll)
- Volatile keyword for visibility
- Atomic variables for lock-free operations
- Producer-Consumer pattern implementation
- Thread pools (fixed, cached, scheduled)
- Runnable vs Callable
- Future interface for asynchronous results
- InvokeAll for batch task execution
- ScheduledExecutorService for scheduled tasks
- Basic Lock interface
- ReentrantLock for advanced locking
- ReadWriteLock for concurrent reads
- Thread-safe implementations (e.g., BankAccount)
// Traditional approach FuncInter funcInter = new FuncInterImp(); funcInter.sayHello(); // Lambda expression FuncInter funcInter2 = () -> System.out.println("Hello from Lambda Expression"); funcInter2.sayHello();
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<Integer> isEven = num -> num % 2 == 0; Predicate<Integer> isPositive = n -> n > 0; System.out.println(isEven.and(isPositive).test(2)); // true
// 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");
// 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(); }
Feel free to fork this repository and submit pull requests to improve the examples or add new demonstrations of Java features.
This project is open source and available under the MIT License.
For questions or suggestions, please feel free to reach out or create an issue in the repository.
Happy Learning! π