Posts

Swing in Java ?

-  Swing is Adv version of AWT ### 1. `java.lang.Object` - **Role:** Root class for all Java classes. Every class in Java is a direct or indirect subclass of `Object`. - **Key Methods:** Provides fundamental methods like `equals()`, `hashCode()`, `toString()`, and `getClass()`. - **Usage:** Used as a base for all Java objects and provides basic functionalities. ### 2. `java.awt.Component` - **Role:** A subclass of `Object`, `Component` is an abstract class that serves as the base for all UI components in AWT (Abstract Window Toolkit). - **Key Methods:** Defines methods for rendering, event handling (`addMouseListener()`, `addKeyListener()`), and component lifecycle (`setVisible()`, `setEnabled()`). - **Usage:** Provides the foundation for UI components like buttons, text fields, and panels in Java GUI applications using AWT. ### 3. `java.awt.Container` - **Role:** Extends `Component` and serves as a base class for components that can contain other AWT components. - **Key Methods:**...

Stream in Java ?

 In Java, the Stream API, introduced in Java 8, is a powerful and flexible way to process sequences of elements. It provides a high-level abstraction for operations on collections of data. A stream is not a data structure; instead, it takes input from collections, arrays, or I/O channels. Streams can be used to perform operations like filtering, mapping, and reducing on collections of data in a more declarative and functional style. ### Key Features of Streams 1. **Declarative**: Streams allow you to write code that describes what you want to achieve, rather than how to achieve it. 2. **Composability**: Streams support operations that can be combined to form complex data processing pipelines. 3. **Parallelism**: Streams can be processed in parallel, making it easier to leverage multi-core processors. 4. **Laziness**: Streams are lazy and do not perform any computations until they are explicitly needed. This allows for more efficient processing. ### Stream Operations Stream operatio...

What is ForEach Method in java ?

// ForEach method is method of Iterable Interface // It takes parameter as Consumer Functional Interface // it is Functional Interface i.e. void accept(Type obj) has the only method // nums.forEach(Consume obj) forEach values provide krta hai // Consumer oos values ke sath kya krna hai woh krta hai   // n-> System.out.println(n) all automaticallt is inside the anonymous class of method void accept(Integer)         // automatically creates object of anonumous class who implements Consumer interface         // it automaticaly Detects n is type of Integer         // There we used Lambda Expression          // Simply forEach method gives values to Consumer object 'n' and it does operations like print of anything --------------------------------------------------------------------------------------------------------------------------- ### Detailed Explanation 1. **Lambda Expression**:    ``...

Comparator vs Comparable Interface in java ?

Comparator vs Comparable * Comparator    - Its Interface and Functional Interface   - Its an Functional Interface it means we can use anonymous Inner class and Lambda Expressions   - Comparator has non abstract method which is " int compare(int , int) "   - It take 2 Type class Objects (Ex: int compare(Integer i , Integer j))   - All Wrapper Classes has " .compare(type , type) " method which returns int value    - if returns positive integer it means swap if negative it means no swap if zero it means both are equal   - Comparator is as a third person who are comparing objects and generates logic   - But Collections.sort(Type , comparator obj ) ye sort krta hai according Comparator (compare(T,T)) * Comparable   - Comparable is Normal Interface   - It has method name " int compareTo(Type object) "    - There are 2 types of compareTo(Type obj) and compareTo(String obj)   - (1) - compareTo(Type obj) it just gives cla...

compareTo method of java ?

 `compareTo` method ke do alag-alag signatures hote hain, ek jo `Object` type ko leta hai aur ek jo `String` type ko leta hai. Yeh do alag methods hote hain jo alag context mein use hote hain: ### `compareTo` Method with `Object` Parameter 1. **Signature:**    - `int compareTo(Object o)`    - Yeh method `java.lang.Comparable` interface mein define hota hai. Iska use generally objects ke comparison ke liye hota hai, jab ek object ko dusre object ke saath compare karna hota hai. 2. **Example:**    - Example mein, agar aap `Comparable` interface implement karte hain ek custom class mein, to `compareTo` method ek `Object` parameter leta hai:    ```java    public class MyClass implements Comparable<MyClass> {        private int value;        public MyClass(int value) {            this.value = value;        }        @Overrid...

Collections class in java ?

 The Collections class in Java provides utility methods for working with collections, such as List , Set , and Map . It offers various static methods to perform common operations like sorting, searching, and synchronizing collections. Here are some key functionalities provided by the Collections class: Sorting Collections: sort(List<T> list) : Sorts the specified list into ascending order. sort(List<T> list, Comparator<? super T> c) : Sorts the specified list according to the order induced by the specified comparator. Searching Collections: binarySearch(List<? extends Comparable<? super T>> list, T key) : Searches the specified list for the specified object using the binary search algorithm. binarySearch(List<? extends T> list, T key, Comparator<? super T> c) : Searches the specified list for the specified object using the binary search algorithm. Synchronization of Collections: synchronizedList(List<T> list) : Returns a synchronize...

Thread in Java ?

jab `ref1.run()` aur `ref2.run()` ko directly call karte ho, tab wo methods main thread mein sequentially execute hote hain, alag-alag threads mein nahi. Iska matlab hai ki aap actual multithreading nahi kar rahe ho.  *  Thread class has only the way we can achieve multithreading *  We can Create Thread using Thread class and Runnable Interface * Thread class has many method :  * join() -> it will wait for completion of Thread * sleep(milisec) -> it will wait after statment for given miliseconds * Runnable is the FunctionalInterface it means we can use Anonymous inner class and Lambda                    Expressions (only one non - abstract method is there which is run())  ***Multithreading only possible using start() method of Thread class Java mein true multithreading achieve karne ke liye, aapko `Thread` class ka use karke `Runnable` instances ko alag threads mein start karna padta hai. Main differen...