Example code to demonstrate various features of Java 8 and above.
It also contains my solution to various coding problems that I was given on assorted job interviews.
- Java 17 or later.
- Apache Maven, version 3.6.0 or later.
- A Postgres database can be running. Edit the application.properties to set the correct parameters.
- Execute the schema/create.sql file to create the appropriate database tables and users. If you change the database password, you'll have to generate a new encrypted password, so it's simplest to use the existing user and password.
- The pom.xml file needs to be edited to uncomment the database sections.
- cd to the directory with this README file.
- Type: mvn clean compile test.
- Type: mvn spring-boot:run
- REST calls are at localhost:8888, or whatever port is set in application.properties.
- Type: mvn package to create the executable jar file.
- For the openapi3 (swagger) interface, in the browser, go to: http://localhost:8888/swagger-ui/index.html
The /vi/mockEndpoints calls illustrate the security scheme, using JWT tokens. The remaining calls are open to make it simpler to use.
Support was added for Postgres set up with a certain schema and user/password. The relevant parts are commented out in the pom file. Once I have the test part automated, I will uncomment that.
The primary purpose of this project is to learn and demonstrate new features in Java from version 8 on.
A secondary purpose is to have a complete Spring Boot application which can run either from the command line, or a docker/kubernetes environment.
It is intended that the code on the main branch is always in a good state, while work in progress might be saved to the develop branch.
Many of these problems are implemented both with and without streams. The time to run are compared as well as the correctness of the solution.
Below is a list of new features by release. Features are put in the first release where they are production features, not preview or incubating.
This was a major release. Some of the main features are:
- Functional Programming
- Optional Class
- Default And Static Methods In Interfaces
- Method References
- Stream API
- New java.time package
- New java.util.Base64 class.
- Collection API Improvements
- Concurrency API Changes/Enhancements
- Java IO Improvements
- Miscellaneous Core API Improvements
- Module System (Project Jigsaw)
- JShell command line tool (repl)
- New ProcessHandle class for information about external processes.
- Jcomd command line tool for analyzing running programs.
- java.util.concurrent.flow class for reactive programming.
- Unified JVM logging.
- Optional.stream() method.
- Assorted new stream methods.
- List, Set, Map have copyOf(Collection c) methods.
- Collectors has to UnmodifiableList(), Set(), or Map().
- Optional classes have a new orElseThrow() method.
- Start of 6 month relase cycle
- java.net.http package with new Http client.
- New methods on String class:
- isBlank()
- lines()
- strip()
- stripLeading()
- stripTrailing()
- repeat()
- Files class contains readString() and writeString() methods.
- Collection has a new toArray() method which returns an array of the correct type, not Object[].
- Predicate has a new not() method.
- Epsilon no-op garbage collector, useful under special conditions.
- JFR (Java Flight Recorder) now open sourced.
- JMC (Java Mission Control) now needs to be downloaded separately.
- TLS upgraded to 1.3 for greater security.
- Unicode 10 support.
No major changes in this release.
- String has new indent() and transform() methods.
- Files class has new mismatch() method.
- Collectors has a teeing() method for stream processing.
Various minor changes.
- Unicode 12.1 support
- Switch expressions
- More helpful NPE exceptions.
- JFR Event Streaming
- Text Blocks
- Records for immutable classes.
- Sealed classes.
- Virtual Threads
- Sequenced Collections
- String Templates
- Pattern Matching
- Native and Memory API
The primary purpose of these is to support I/O bound operations. They are not meant for cpu intensive operations. They can be created like this:
- Thread.ofVirtual()
- Executors.newVirtualThreadPerTaskExecutor()
Tool support is provided by Java Flight Recorder, as well as the jcmd command.
Implementing classes such as ArrayList and TreeSet support these operations:
- addFirst()
- addLast()
- getFirst()
- getLast()
- removeFirst()
- removeLast()
- reversed()
There are various templates that can be used to format strings with parameters. This is a preview feature, so should probably be avoided for now.
Nothing too exciting, this allows you to do less casting in a type safe way.
This simplifies switch statements when using derived classes of the input parameter. You can have a switch based on the object type, rather than using instanceof
This is a simpler and more efficient way of dealing with native applications and memory than JNI.
I could put a lot more time into this project, but it serves my purposes for now, so I will be moving on to another, more useful project instead.
There are lots of limitations in this project, including, but not limited to:
- Controllers need to be added for all the services.
- The tests need to be enhanced.
- Tests need to be divided up into short and longer running for convenience.
- The pom file should be cleaned up.
- The docker functionality is not yet implemented.
- Spring security should be implemented.
- Absurd amount of logging on load tests, fix in test config.
Determine which of a sequence of integers are prime. This allows for comparison among using stream(), parallelstream(), or Java 7 type multi-threading.
This contains various coding challenges that I wrote during live coding interviews. I have made some of the problems more challenging, as well as implementing them in multiple ways to show successively better implementations, or allowing comparisions in the efficiency of the solution.
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
So, 9 (1001) has a binary gap of 2, while 8 (100) is 0.
Find the number of unique integers in an array.
This is obviously a pretty simple problem.
For an array of integers, find the minimum one in the specified range NOT in the array.
Also fairly simple, but some interesting results in the unit tests for sorted vs unsorted data, and streaming vs Java 7 implementations.
Categorize integers by their first and last digits, and find the pair in the same category which are the largest sum.
This is a little more complex than the previous problems, and allows for increasingly sophisticated implementations.
An array of integers should contain either 0 entries of a number, or the same number of entries as the number itself.
A move consists of either adding or removing an entry to the array. The problem is to determine the minimum number of moves to modify the array so that it meets the criterion above.
The docs directory contains a number of markdown files which document various more recent java features.
The src/main/deployment directory contains the Ansible/Terraform files for automated deployment. A separate README file in that directory will explain it in more detail.