Skip to content

Instantly share code, notes, and snippets.

View asela38's full-sized avatar

Asela Illayapparachchi asela38

  • Rakuten
  • Sri Lanka
View GitHub Profile
@asela38
asela38 / gist:94505f908630c06c7eb9b691457097e6
Created June 5, 2019 12:16
Compile and Run Hello World Java Appliation
➜ Test docker pull java
Using default tag: latest
latest: Pulling from library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete

Take all properties from a pojo and enclose them inside a tag

$ grep private myPojo.java  | cut -d' ' -f7 | sed -e 's$\(.*\);$<value>\1</value>$g' 

Kill all processors which mactch a perticular user

$ ps ef | grep user123 | cut -d' ' -f2- | sed 's/^\s*//g' | cut -d' ' -f1 | grep "[0-9]" | xargs -n1 kill -9 
@asela38
asela38 / pom.xml
Created September 25, 2018 04:46
My Basic Default POM
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.asela.test</groupId>
<artifactId>application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Application</name>

curl -s "wttr.in"

Weather report: Singapore, Singapore

    \  /       Partly cloudy
  _ /"".-.     30-35 °C
    \_(   ).   ↑ 17 km/h
    /(___(__)  10 km
 4.0 mm
@asela38
asela38 / java8-one-liners-for-statistical-properties.md
Last active May 11, 2023 23:54
Java 8 One liners for statistical properties : Mean, Mode, Median(Q2), Q1, Q2, Variance, Standard Deviation

While working on a Hacker Rank Problem I wrote few one lines using java stream to find mean, median, mode, Q1, Q3, variance and standard deviation. Thought to share since it's quite interesting.

        // mean
        double mean = list.stream().mapToInt(Integer::intValue).average().getAsDouble();
        System.out.println(mean);
        
        // mode - create count map using group by and sorted with custom comparator to give minimum from competing probable mode values
        Integer mode = list.stream()
                .collect(Collectors.groupingBy(i -> i, () -> new TreeMap<Integer, Long>(), Collectors.counting()))

Most used Terminal Line Commands

when working on the terminal line

Use command
Toggle Between begining of line and End of Line CTRL+XX
Delete Last Word
Before:
>test one two |
After:
>test one |
ALT + BACKSPACE
Cut last Word
Before:
>test one two |
After:
>test one |
CTRL+W
Paste last cut CTRL+Y

Creating a Shell Script to Create New Shell Scripts Based on a Template

For this example, I am creating a shell script to create another shell script that prints a specific message based on a template.

Template File Name : script-XXX.template

Content of the Template:

#!/bin/sh
@asela38
asela38 / HashMapStringVsIntegerStoring.md
Last active August 10, 2018 10:09
Why it's good to keep Strings as key of a hash map over Integers?

First to explore the hash collisions i used following utility methods which access the field 'table' array which stores all the key-value pairs in the hash table.

    private void analyzeBuckets(HashMap<?, ?> hashMap) throws NoSuchFieldException, IllegalAccessException {
        Class<?> class1 = hashMap.getClass();
        
        Field field = class1.getDeclaredField("table");
        field.setAccessible(true);
 Object[] objects = (Object[]) field.get(hashMap);

To Find Highest Frequency number with the frequency following code can be use quite nicely

    @Test
    public void findHighestFrequency() throws Exception {

        Stream.of(1,2,2,1,2,1,2,2,1,2,1,2,2,1,2,1,2,2,1,2,1,2,2,1,2)
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                .entrySet().stream()
 .max((a,b) -&gt; a.getValue().compareTo(b.getValue()))