Java POI read excel example

It's easy to use Apache POI read excel file. Let's say you have an excel file. The path of it is D:\test.xlsx. The content is like following.

Java8 stream collect list to map in order

We can convert list to map easily in Java8 by using stream. The sample code is like following.

Java Executors newFixedThreadPool example

Executors.newFixedThreadPool(3) method will create a thread pool with only 3 threads. When we put Runnable or Callable object into the thread pool. The thread pool will create new thread for running task. When the number of threads equals the pool size the new task will be queued. The code below may help you have a better understanding of the the method newFixedThreadPool().

MySQL show indexes for table

There are several ways to show indexes for a table in MySQL. Let's say you have create the table in your database. The schema of table is here.

Java Executors newCachedThreadPool example

Executors.newCachedThreadPool() method will create a thread pool which core pool size is 0 and max pool size is Integer.MAX_VALUE. The detail of constructor function is here:

Java core tutorial

This series of blog describes mainly how to use common classes of JDK.

Java8 stream map example

map function in Stream class means you can pass Function and get another Stream result. It is very useful when you want convert collection to another collection. In this page I'll give you a demonstration how to convert list of User to a list of user's name and age.

Understand ThreadPoolExecutor core and maximum

When we create ThreadPoolExecutor we need to specify the size of core and maximun. Let's say you create a executor, the corePoolSize is 2, the maximumPoolSize is 4. In general, there are 2 threads working. If the third task created it will be added in the queue. When the queue is full the executor will create a new thread for handling the new task. New tasks will be rejected when the size of pool is equal with maximumPoolSize and the queue is full.

Java ThreadPoolExecutor example

ThreadPoolExecutor is a useful class to manage threads in JDK. Create a thread task and put it to thread pool. This task will be executed in the feature. The simple thread pool code is here.

MongoDB Authentication Mechanisms

When you connect MongoDB with auth you need to specify the "Authentication Mechanisms". If your MongoDB version is 3.0+ the default "Authentication Mechanisms" is SCRAM-SHA-1, for other versions MONGODB-CR as the default.

MongoDB delete user

If MongoDB does not enable auth you can delete user without authorisation. If MongoDB enables auth you need to make sure you are userAdmin or userAdminAnyDatabase role before deleting the user. User following command to delete the user.

MongoDB enable auth

I assume you have installed MongoDB in your environment. The version of MongoDB in my computer is 3.4.6. There are two ways to enable auth in MongoDB, start mongod service with --auth or change the configuration file. In this page I will show you the second one.

Java execute command get output example

The getInputStream() method of Process return the result of what you executed. If there is an error when you invoke the command with Process, using getErrorStream() to get the information. The example code is here.

Java delete file example

Deleting file is a common daily operations. I will show you how to delete file in java. The example code is here.

Java Semaphore example

Semaphore is used to control the number of available threads. Let's say you have a lot of workers and only 5 computers. When workers finish his work he has to release the computer for next worker. Semaphore is good at handle this case. The example of Semaphore is here.