Found 7442 Articles for Java

Difference between Iterator and Spilt Iterator in Java.

Himanshu shriv
Updated on 09-Sep-2020 11:27:40

1K+ Views

Iterator and split iterator both interface are used for iterating over the collection.Split iterator is introduced in Java 8 for achieving parallelism. It can split the given set of element and can perform operation parallely using different independent threads. It can traverse the elements parallely as well as sequentially manner. There are following important methods in the splitIterator interface −trySplit - It is used for split the given set of elements into multiple pieces.tryAdvance - It is equivalent to the hasNext/ next methods available in Iterator interfacegetExactSizeIfKnown  -It is used to get the size of the given set of elements.Sr. ... Read More

Difference between intermediate and terminal operations in Java 8

Himanshu shriv
Updated on 09-Sep-2020 11:25:15

16K+ Views

Stream is introduced in Java 8, it is only used for processing group of data not for the storting elements.. It does not modify the actual collection, they only provide the result as per the pipelined methods.Stream api supports multiple operations and operations are divided into two parts −Intermediate Operation- These operations are used to pipeline other methods and to transform into the other streams. They don’t produce results because these operation does not invoke until the terminal operation gets executed. Below are the examples −sorted(Comparator)peek(Consumer)distinct()Terminal operations - These operations are used to produce results. They can’t be used for ... Read More

Difference between Function and Predicate in Java 8

Himanshu shriv
Updated on 09-Sep-2020 11:22:33

8K+ Views

Function and Predicate both functional interface was introduced in Java 8 to implement functional programming in Java.Function interface is used to do the transformation.It can accepts one argument and produces a result. On the other side, Predicate can also accept only one argument but it can only return boolean value. It is used to test the condition.Sr. No.KeyFunctionPredicate1BasicIt can take 2 type parameters First one represents input type argument type and second one represents return type.It can take one type parameter which represents input type or argument type.2Return TypeIt can return any type of value.It can only return boolean value3MethodIt ... Read More

Difference between scheduledThread pool and Single Thread Executor.

Himanshu shriv
Updated on 09-Sep-2020 11:16:53

1K+ Views

Sr. No.KeyScheduled Thread PoolSingle Thread Executor1BasicCreates a thread pool that can schedule commands to run after a given delay, or to execute periodically. Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time2QueueIt uses Delay Queue to store tasks. Schedule the task based on time delay.It uses blocking queue.3Thread LifetimeT he number of threads to keep in the pool, even if they are idleRecreate thread if killed because of the task.4.Thread Pool SizeIt always has a single thread running.The thread pool can grow from zero threads to Integer.MAX_VALUE5.Use CaseWe should used fixedthreadpool, ... Read More

Difference between Fixed thread pool and cached thread pool.

Himanshu shriv
Updated on 09-Sep-2020 09:41:08

4K+ Views

Executor framework are designed using thread pool concept. Thread pool is the way to reuse the already created thread instead of creating a new thread every time to execute the current task.Executors class provides a factory method to create thread pools. The ThreadPoolExecutor class is the base implementation for the executors that are returned from many of the Executors  factory methods.Sr. No.KeyFixed Thread PoolCached Thread Pool1BasicAs per Java Doc −Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional ... Read More

Difference between Executor and ExecutorServices in Java

Himanshu shriv
Updated on 09-Sep-2020 09:36:46

2K+ Views

Executor and ExecutorServices both interfaces are part of the Executor framework. It is released with Java 5. In java, thread creation is very expensive operation so we should  reuse the available thread instead of starting a new thread every time and we can achieve the same using Executor framework.Executor framework use the thread pool to execute the task parallel which helps to optimize response time and resource utilization. It provides four types of built in thread pools −Fixed Thread PoolCached Thread PoolScheduled Thread PoolSingle Thread ExecutorSr. No.KeyExecutorExecutorServices1BasicIt is a parent interfaceIt extends Executor Interface2MethodIt has execute() methodIt has submit() method3Return TypeIt ... Read More

Difference between Runnable and Callable interface in java

Himanshu shriv
Updated on 09-Sep-2020 09:33:21

7K+ Views

Runnable and Callable both functional interface. Classes which are implementing these interfaces are designed to be executed by another thread.Thread can be started with Ruunable and they are two ways to start a new thread: one is by subclassing Thread class and another is implementing Runnable interface.Thread class does not have constructor for callable so we should use ExecutorService  class for executing thread.Sr. No.KeyRunnableCallable1PackageIt belongs to Java.langIt belongs to java.util.concurrent2Thread CreationWe can create thread by passing runnable as a parameter.We can’t create thread by passing callable as parameter  3Return TypeRuunable does not return anythingCallable can return results4.MethodIt has run() methodIt ... Read More

Difference between Streams and Collections in Java 8

Himanshu shriv
Updated on 09-Sep-2020 09:31:05

5K+ Views

Java Collections framework is used for storing and manipulating group of data. It is an in-memory data structure and every element in the collection should be computed before it can be added in the collections.Stream API is only used for processing group of data. It does not modify the actual collection, they only provide the result as per the pipelined methods.Sr. No.KeyCollectionsStreams1BasicIt is used for storing and manipulating group of dataStream API is only used for processing group of data2PackageAll the classes and interfaces of this API is in the Java.util packageAll the classes and interfaces of this API is ... Read More

File Objects in Java

AmitDiwan
Updated on 17-Aug-2020 09:48:42

1K+ Views

The File object represents the actual file/directory on the disk. Here are the list of constructors to create File Object in Java −Sr.No.Method & Description1File(File parent, String child)This constructor creates a new File instance from a parent abstract pathname and a child pathname string.2File(String pathname)This constructor creates a new File instance by converting the given pathname string into an abstract pathname.3File(String parent, String child)This constructor creates a new File instance from a parent pathname string and a child pathname string.4File(URI uri)This constructor creates a new File instance by converting the given file: URI into an abstract pathname.Assuming an object is ... Read More

Precision Handling in Java

AmitDiwan
Updated on 17-Aug-2020 09:46:45

550 Views

Let us see how precisions are handled in Java −Example Live Demoimport java.io.*; import java.lang.*; public class Demo{    public static void main(String[] args){       double my_val = 34.909;       System.out.println("The formatted value of 34.909 is ");       System.out.println(String.format("%.7f", my_val));       double my_val_2 = 12.56;       System.out.println("The formatted value of 12.56 is ");       System.out.println(String.format("%.9f", my_val_2));    } }OutputThe formatted value of 34.909 is 34.9090000 The formatted value of 12.56 is 12.560000000A class named Demo contains the main function, where a double valued integer is declared, and it is ... Read More

Advertisements