Skip to main content

Posts

Java Priority Queue sort using lambda expression

Priority Queue :     In Java  Priority Queue  is a  queue  which keeps its elements sorted as per their natural order( example in ascending orders for Integer, alphabetical a-z for alphabets) or using a custom Comparator at the time of creation you can custom sort it. It has most method  similar to a queue  add, clear, poll, peek As priority queue have to compare elements to keep in order so elements must be comparable otherwise, it will throw ClassCastException .  As null can not be compared so you are not allowed to insert null too A program to sort priority queue using Lambda function in Java           c lass   PriorityQueueLambdaJava { public List<Integer> KFrequent( int [] nums, int k) { Map<Integer, Integer> map = new HashMap<>(); for ( int j: nums) { map .put(j, map .getOrDefault(j, 0 ) + 1 ); } PriorityQueue<Map.Entry<Integ...
Recent posts

Java InputOutput Types

Java InputOutput : Most of the application needs to process some input and then produce output to a network file or local file etc. There is a java package java.io which provides input and output through data streams , serialization and the file system. 
The package covers most of the input and output operations required but not the GUI I/O operations which are covered in Swing, JSP etc in Java enterprise. 
this package is primarily focused on input and output to files, network streams, internal memory buffers etc. However, the Java IO package does not contain classes to open network sockets which are necessary for network communication. For opening network sockets there is another networking package in java. 
Typical Java IO sources : FILE, Network IO, Streams Streams : Conceptually, a stream is a endless flow of data from where you can either read  or write . Streams in Java IO can be either byte based (reading and writing bytes) or character based (reading and wri...

Java System Class --- a Utility often used

Java System Class The System class  is a final class and  contains many useful fields and methods which are quite useful utilities like: standard input, output and error, for loading files and libraries. It helps to provide access to externally defined properties and variables. It also contain utility methods to quickly copy an array To get the environment variables. Fields in the System Class: static PrintStream err : The standard error output stream static InputStream in : The standard input stream static PrintStream out: The standard output stream The class has many useful methods , lets see few important methods: static void arraycopy (Object src, int srcPos, Object dest, int destPos, int length): It is used to copy array from src array to destination array starting at srcPosition and starts copying at destPosition and copies the length specified from source to destination. System Properties Methods : It contains method to get , set and clear sys...

Thread -- A Java Class which runs nearly every platform

Thread: Multithreading, Runnable, Thread Class, Callable, DeadLock, Synchronization   Thread is a part of program execution which executes with different set of  variable values . In JVM multiple threads can be running concurrently to server a purpose. When multiple thread executes in program that is called Multi-Threading . Life Cycle Of Thread: Thread could be in 5 states : 1) New 2) Runnable 3) Running 4) Waiting/Blocked 5) Dead/Terminated New : The thread is in new state if you create an instance of Thread class but before the invocation of start() method.     Runnable: The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread. Running: The thread is in running state if the thread scheduler has selected it. Waiting/Blocked: This is the state when the thread is still alive, but is currently not eligible to run. Terminated: A thread is in terminated ...

Refrences Type Java

To understand Garbage collection mechanism we must know about the various reference types for object because on that basis the GC reclaim the memory from the object There are following 4 kinds of reference types in Java:     Strong references.     Weak references.     Soft references.     Phantom references. Strong reference: It is most simple as we use it in day to day programming. if not differently specified then an object is a strong Reference Object Ex String language="English"; A new string object will be created and  a strong reference to it would be stored in the variable 'language'; If any object has a strong reference or it is reachable by any chain of strong reference then JVM will not garbage collect it to avoid any situation where it destroy the object which is currently being worked upon. Weak reference: These are created using WeakReference class which is present in the java.lang.ref . Weak...

Garbage Collector

Garbage Collector : finalize  In C/C++ or all the languages before java we used to manually free the unused memory by deleting the objects/reference created so that system can use that memory for another objects or for other purpose. In java the objects which system can identify as "no longer in use" can be marked as garbage and later recycled. It is also called memory recycling. Most of the object are not used after sometime only few has references further in the program. So java identify those objects which are no longer in use and recycles the memory. So it helps the programmer to relieve the burden of memory management and hence increase the productivity. Garbage collection process mainly handles memory management process, it does not take care about network connection or database connection or any user windows. Those has to be manually managed by programmer. If any of the object is associated with other memory space and before the object is garbage collected an...

Java Exception Handling

Java Exception Handling : try, catch, final An exception is an event which that interrupts the normal flow of program. The exception could occur due to any resource required is not available, user enters invalid data , network connection lost etc. When there is an anomaly during execution of the method, then that method creates an object which contains a lot of debugging information such as method hierarchy, line number where the exception occurred, type of exception etc. The method than pass this object to runtime system which is called throwing an exception. When the exception is handover to runtime system then it tries to find some catch block which can handle this exception.  It tries to find the exception handler block from the method where the exception occurred and proceeds through the call stack in the reverse order in which the methods were called. When it find the block which can handle the type of exception then runtime system passes the exception object to the c...