Posts

Showing posts with the label java

Spring Sample on Docker (for M1 Chip)

In this post, I am going to run a sample Spring application, which was created at:  REST with Spring: Quick Start  . Assuming that you have the jar file at: ./build/libs/rest-service-0.0.1-SNAPSHOT.jar Prepare Docker Files % vim Dockerfile FROM arm64v8/openjdk ADD ./build/libs/rest-service-0.0.1-SNAPSHOT.jar greeting.jar CMD java -jar greeting.jar Note: arm64v8/openjdk is for M1 Chip. This solved the following build error: "The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8)" % vim docker-compose.yml version: "3.9" services: web: build: . ports: - "80:8080" Build & Run % docker-compose up --build % curl http://localhost/greeting {"id":1,"content":"Hello, World!"}

Java Data Structure Cheat Sheet - Hash Table

HashMap is the hash table implementation in Java. Check  this post  for time complexity. https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html The methods include: V put(K key, V value) boolean containsKey(Object key) V get(Object key) V remove(Object key) Set<K> keySet() etc Example import java.util.HashMap; import java.util.Map; public class HashMapExample {   public static void main(String[] args ) {     Map<String, Integer> map = new HashMap<String, Integer>();     map .put( "a" , 1);     map .put( "b" , 2);     map .put( "c" , 3);     System. out .println( map .get( "a" )); // 1     System. out .println( map .get( "z" )); // null     if ( map .containsKey( "b" )) {       System. out .println( map .get( "b" )); // 2     }     for (String key : map .keySet()) {       int val = map .get( key ); ...

Java Data Structure Cheat Sheet

Average time complexity of data structures: Data structure Access (top) Access (key) Search Insertion Deletion Array O(1) O(1) O(N) O(N) O(N) Stack O(1) O(N) O(N) O(1) O(1) Queue O(1) O(N) O(N) O(1) O(1) Linked List O(1) O(N) O(N) O(1) O(1) Hash Table n/a O(1) O(1) O(1) O(1) Binary Search Tree O(1) O(log N) O(log N) O(log N) O(log N) Heap O(1) O(N) O(N) O(log N) O(log N) Each link is for a Java example.

Java Data Structure Cheat Sheet - Queue

Queue is an interface in Java. There are multiple implementation choices, but using LinkedList is common and performs as the usual queue as expected. https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html Main methods are: boolean add(E e) E element() E remove() and there exists the alternative ones as: boolean offer(E e) E peek() E poll() The first group throws an Exception whereas the second one returns "special value". They are all O(1). Example import java.util.LinkedList; import java.util.Queue; public class QueueExample {   public static void main(String[] args ) {     Queue<Integer> q = new LinkedList<Integer>();     q .add(1); // or q.offer(1)     q .add(2);     q .add(3);     System. out .println( q .element()); // 1. you can also use q.peek()     while (! q .isEmpty()) { // isEmpty() is from Collection       int n = q .remove(); // or q.poll()   ...

Java Data Structure Cheat Sheet - Stack

Stack exists as a class (not as an interface or abstract class) in Java. https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html Here are the three main methods: E peek() E pop() E push(E item) They are all O(1). Example: import java.util.Stack; public class StackExample {   public static void main(String[] args ) {     Stack<Integer> s = new Stack<Integer>();     s .push(1); // add is also available as Vector     s .push(2);     s .push(3);     System. out .println( s .peek()); // 3     while (! s .empty()) { // isEmpty() is also available as Vector       int n = s .pop();       System. out .println( n ); // 3, 2, 1     }     // System.out.println(s.peek()); // java.util.EmptyStackException is called   } }

Kotlin: Quick Start on command line

This post describes how to start Kotlin on command line. https://kotlinlang.org/docs/command-line.html What is Kotlin? Kotlin is a programming language running on JVM. It is becoming popular to implement Android apps. https://kotlinlang.org/ Install If you have not installed SDKMAN, see  https://web-quickstart.blogspot.com/2021/04/sdkman-quick-start.html % sdk install kotlin ... % kotlin -version  Kotlin version 1.4.31-release-344 (JRE 11.0.10+9) hello world % cat hello.kt  fun main() {   println("Hello, World!") } % kotlinc hello.kt HelloKt.class is created % java HelloKt Hello, World! hello world (mix Java code) The following code shows the compatibility and difference between Kotlin vs Java. Note that you probably should not mix them on production code. % cat hello.kt  fun main() {   println("Hello, World!")   System.out.println("Java Way"); } % kotlinc hello.kt % java HelloKt Hello, World! Java Way

SDKMAN: Quick Start

"SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on most Unix based systems" ( https://sdkman.io/ ). In this post, I am going to use it to switch Java version from 15 to 11. background I had java version 15, which was not supported by sonarqube. So, I need to use version 8 or 11. % java --version openjdk 15.0.1 2020-10-20 install sdkman As mentioned in https://sdkman.io/install , run: % curl -s "https://get.sdkman.io" | bash then, open a new terminal to reflect the change. install java 11 (and switch) $ sdk install java Downloading: java 11.0.10.hs-adpt ... Setting java 11.0.10.hs-adpt as default. % java --version openjdk 11.0.10 2021-01-19 See https://sdkman.io/usage for more detail list You can list available versions. % sdk list java ================================================================================   Vendor         | Use | Version       | Dist     | Status     | Identif...

REST with Spring: Quick Start

I used Gradle to run  “Hello, World” RESTful web service with Spring . Requirement JDK 1.8 or later Gradle 4+ 1. git clone & cd git clone https://github.com/spring-guides/gs-rest-service.git cd gs-rest-service/initial 2. add POJO src/main/java/com/example/restservice/Greeting.java package com.example.restservice; public class Greeting { private final long id; private final String content; public Greeting(long id, String content) { this.id = id; this.content = content; } public long getId() { return id; } public String getContent() { return content; } } 3. add controller src/main/java/com/example/restservice/GreetingController.java package com.example.restservice; import java.util.concurrent.atomic.AtomicLong; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class GreetingController { private static...

Selenide: Quick Start

Selenide is a Selenium wrapper in Java for testing.   This post describes how you can start using Selenide (using Gradle). 1. Setup Java Application # keep pressing enters to choose default values gradle init --type java-application # confirm that test succeeds gradle test 2. Add Selenide Dependency open build.gradle and edit as: dependencies {   ...   testImplementation 'com.codeborne:selenide:5.13.0' } 3. Write Test (replace the existing test with selenide code) src/test/java/selenide/AppTest.java package selenide; import static com.codeborne.selenide.Condition.text; import static com.codeborne.selenide.Selenide.$; import static com.codeborne.selenide.Selenide.open; import org.junit.Test;   public class AppTest { @Test public void test() { open("https://www.google.ca/"); $("input[type=text]").val("test").pressEnter(); $("body").shouldHave(text("test")); } } 4. Run Selenide gradle test This will open a browser t...