base64ToByte
1 | String string = "SmF2YWNvZGVnZWVrcw=="; |
TimeConvert
1 | //date format |
Lambda
1 | //stream read database |
thread
notify thread
1 | class Shared |
stop thread
1 | class MyThread extends Thread |
1 | class MyThread extends Thread |
try-catch-finally
- 不管有木有出现异常,finally块中代码都会执行**
- 当try和catch中有return时,finally仍然会执行
- finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,管finally中的代码怎么样,返回的值都不会改变,仍然是之前保存的值),所以函数返回值是在finally执行前确定的;
- finally中最好不要包含return,否则程序会提前退出,返回值不是try或catch中保存的返回值。
- 任何执行try 或者catch中的return语句之前,都会先执行finally语句,如果finally中有return语句,那么程序就return了,所以finally中的return是一定会被return的。
ReentrantLock
1 | import java.util.concurrent.locks.ReentrantLock; |
1 | Output: |
stream
Creating Java Streams
- We can use Stream.of() to create a stream from similar type of data. For example, we can create Java Stream of integers from a group of int or Integer objects.
1 | Stream<Integer> stream = Stream.of(1,2,3,4); |
- We can use Stream.of() with an array of Objects to return the stream. Note that it doesn’t support autoboxing, so we can’t pass primitive type array.
1 | Stream<Integer> stream = Stream.of(new Integer[]{1,2,3,4}); |
- We can use Collection stream() to create sequential stream and parallelStream() to create parallel stream.
1 | List<Integer> myList = new ArrayList<>(); |
- We can use Stream.generate() and Stream.iterate() methods to create Stream.
1 | Stream<String> stream1 = Stream.generate(() -> {return "abc";}); |
- Using Arrays.stream() and String.chars() methods.
1 | LongStream is = Arrays.stream(new long[]{1,2,3,4}); |
Converting Java Stream to Collection or Array
- We can use java Stream collect() method to get List, Map or Set from stream.
1 | Stream<Integer> intStream = Stream.of(1,2,3,4); |
- We can use stream toArray() method to create an array from the stream.
1 | Stream<Integer> intStream = Stream.of(1,2,3,4); |
Java Stream Intermediate Operations
- Stream filter() : We can use filter() method to test stream elements for a condition and generate filtered list.
1 | List<Integer> myList = new ArrayList<>(); |
- Stream map() : We can use map() to apply functions to an stream. Let’s see how we can use it to apply upper case function to a list of Strings.
1 | Stream<String> names = Stream.of("aBc", "d", "ef"); |
- Stream sorted() : We can use sorted() to sort the stream elements by passing Comparator argument.
1 | Stream<String> names2 = Stream.of("aBc", "d", "ef", "123456"); |
- Stream flatMap() : We can use flatMap() to create a stream from the stream of list. Let’s see a simple example to clear this doubt.
1 | Stream<List<String>> namesOriginalList = Stream.of( |
Java Stream Terminal Operations
- Stream reduce() example: We can use reduce() to perform a reduction on the elements of the stream, using an associative accumulation function, and return an Optional. Let’s see how we can use it multiply the integers in a stream.
1 | Stream<Integer> numbers = Stream.of(1,2,3,4,5); |
- Stream count() example: We can use this terminal operation to count the number of items in the stream.
1 | Stream<Integer> numbers1 = Stream.of(1,2,3,4,5); |
- Stream forEach() example: This can be used for iterating over the stream. We can use this in place of iterator. Let’s see how to use it for printing all the elements of the stream.
1 | Stream<Integer> numbers2 = Stream.of(1,2,3,4,5); |
- Stream match() examples: Let’s see some of the examples for matching methods in Stream API.
1 | Stream<Integer> numbers3 = Stream.of(1,2,3,4,5); |
- Stream findFirst() :This is a short circuiting terminal operation, let’s see how we can use it to find the first string from a stream starting with D.
1 | Stream<String> names4 = Stream.of("Pankaj","Amit","David", "Lisa"); |
some example
1 | //merge collection |