Tuesday, January 28, 2014

Java - static

1.

public class Test {

public static int staticInt;

public static void main(String[] args) {
Test test1 = new Test();

test1.staticInt++;

Test test2 = new Test();

test2.staticInt++;

System.out.println(test1.staticInt);
System.out.println(test2.staticInt);
System.out.println(Test.staticInt);
}
}

2.

public class Test {

static String staticStr;

static {
staticStr = "static 1";
}

public static void main(String[] args) {
System.out.println(staticStr);
}

static {
staticStr = "static 2";
}
}

Java - for

import java.util.ArrayList;
import java.util.List;

public class Test {

static int current;

public static void main(String[] args) {
final int LOOP_COUNT = 10000;
List<Integer> list = new ArrayList<Integer>(LOOP_COUNT);

for (int i = 0; i < LOOP_COUNT; i++) {
list.add(i);
}

long startTime = System.nanoTime();

int listSize = list.size();

for (int i = 0; i < listSize; i++) {
resultProcess(list.get(i));
}

long endTime = System.nanoTime();
double elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println(elaspedTime);

startTime = System.nanoTime();

for (int i = 0; i < list.size(); i++) {
resultProcess(list.get(i));
}

endTime = System.nanoTime();
elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println(elaspedTime);

startTime = System.nanoTime();

for (Integer i : list) {
resultProcess(i);
}

endTime = System.nanoTime();
elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println(elaspedTime);
}

public static void resultProcess(int result) {
current = result;
}
}

MongoDB 2.4 - Java (Hinting)

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Set;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class MongoDB {

private static MongoClient client;
private static DBCollection collection;

public static void main(String[] args) throws IOException {
createCollection();

collection.ensureIndex(new BasicDBObject("a", 1).append("b", -1)
.append("c", 1));

BasicDBObject query = new BasicDBObject("a", 40000).append("b", 40000)
.append("c", 40000);

DBObject document = collection.find(query).hint("a_1_b_-1_c_1")
.explain();

printExplain(document);

// or

BasicDBObject hint = new BasicDBObject("a", 1).append("b", -1).append(
"c", 1);

DBObject document2 = collection.find(query).hint(hint).explain();

printExplain(document2);

client.close();
}

private static void printExplain(DBObject document) {
Set<String> keySet = document.keySet();

for (String str : keySet) {
System.out.printf("%25s:%s\n", str, document.get(str));
}
}

private static void createCollection() throws UnknownHostException {
client = new MongoClient(new ServerAddress("localhost", 27017));

DB database = client.getDB("databaseName");

collection = database.getCollection("collectionName");
}
}

Java - if and if-else

import java.util.Random;

public class Test {
final static int LOOP_COUNT = 10000;
static String dummyData;

public static void main(String[] args) {

long startTime = System.nanoTime();

ifWithElse();

long endTime = System.nanoTime();
double elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println(elaspedTime);

startTime = System.nanoTime();

onlyIf();

endTime = System.nanoTime();
elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println(elaspedTime);
}

static void onlyIf() {
Random random = new Random();
String result = null;
int data = 1000 + random.nextInt();

for (int i = 0; i < LOOP_COUNT; i++) {
if (data < 50) {
result = "50";
}
if (data < 150) {
result = "150";
}
if (data < 250) {
result = "250";
}
if (data < 350) {
result = "350";
}
if (data < 450) {
result = "450";
}
if (data < 550) {
result = "550";
}
if (data < 650) {
result = "650";
}
if (data < 750) {
result = "750";
}
if (data < 850) {
result = "850";
}
if (data < 950) {
result = "950";
}
if (data >= 950) {
result = "over";
}

resultProcess(result);
}
}

static void ifWithElse() {
Random random = new Random();
String result = null;
int data = 1000 + random.nextInt();

for (int i = 0; i < LOOP_COUNT; i++) {
if (data < 50) {
result = "50";
} else if (data < 150) {
result = "150";
} else if (data < 250) {
result = "250";
} else if (data < 350) {
result = "350";
} else if (data < 450) {
result = "450";
} else if (data < 550) {
result = "550";
} else if (data < 650) {
result = "650";
} else if (data < 750) {
result = "750";
} else if (data < 850) {
result = "850";
} else if (data < 950) {
result = "950";
} else {
result = "over";
}

resultProcess(result);
}
}

public static void resultProcess(String result) {
dummyData = result;
}
}

Java - Collection

Note: Collections that are suggested to use if you do not want to think too much

- set: HashSet
- List: ArrayList
- Map: HashMap
- Queue: LinkedList

Monday, January 27, 2014

Linux - Commands

JAR file extraction

- cd /where/you/want/to/extract/it
- jar xvf /path/to/jarfile.jar

text search

grep -r "search" .

file search

find /path/to/search -name 'fileName'

directory search

find httpdocs -type d

Find and kill a process that is using a particular port

- sudo netstat -lpn | grep :9083

tcp        0      0 0.0.0.0:9083            0.0.0.0:*               LISTEN      8950/java

- sudo kill 8950

delete all files which were modified 3 days ago

find /path/to/destination -mtime +3 -exec rm -f {} \;

download file by using curl

curl -O http://www.example.com/example.json

change default java version at CentOS

- alternatives --install /usr/bin/java java /usr/java/latest/bin/java 1
- alternatives --config java

set default editor

export VISUAL="nano"  / export EDITOR="nano"

the longest line of a file

awk '{ if (length($0) > max) {max = length($0); maxline = $0} } END { print maxline }' YOURFILE

length of the longest line of a file

awk '{ if ( length > L ) { L=length} }END{ print L}' YOURFILE

count number of matches per line

grep -o -n 'pattern' <filename> | cut -d : -f 1 | uniq -c

counting files in the current directory

ls -l | grep -v ^l | wc -l
ls *.log | wc -l

display a specific line from a file

- sed -n '20,+0p' file_name
- awk 'FNR==20' file_name

display specific lines from a file

- sed -n '20,+20p' file_name
- awk 'FNR>=20 && FNR<=40' file_name

Calculate size of files in shell

find . -name "*pattern*" -ls | awk '{total += $7} END {print total}'

add a string after each line in a file?

sed -e 's/$/string after each line/' -i filename

add a string to the beginning of each line in a file?

sed -e 's/^/prefix/' file

Turning multiple lines into one line with comma separated

paste -d, -s filename

check permissions

stat -c "%a %n" file/dir

find object which permission is not 755

find /usr/bin /sbin /bin -type f ! -perm 755

use '-pl' to build specific module

- mvn package -pl module1,module2
- mvn clean install -pl module1,module2

skip test (maven)

mvn -Dmaven.test.skip install

print first word

awk '{print $1}' filename

delete lines below current line in vim

dG

delete lines above current line in vim

dgg

remove ^M

sed 's/\r$//g' old_file > new_file

split a large file into small parts

split --bytes=100M big-file new-file

split a large file into small parts by lines

split -l 200000 filename

check the number of CPU cores

nproc

check the total size of the memory in GB

free -g

check the version of the CentOS

cat /etc/redhat-release

check the file system type

df -T

compare the contents of two files

xxd file1 > file1.hex
xxd file2 > file2.hex
diff file1.hex file2.hex

or

cat file1 | od -c > file1.od
cat file2 | od -c > file2.od
diff file1.od file2.od

lines between two line numbers

sed -n '3,6p' file-name

convert the charset of a text file

iconv -f latin1 -t UTF-8 file-name

show the first n charaters of each line

cut -c-50 file-name

how to remove first N characters in each line

:%s/^.\{0,N\}//

Check files/folders size including hidden ones

du -sch .[!.]* * |sort -h

Sunday, January 26, 2014

Ubuntu 12.04 - Find and kill a process that is using a particular port

- sudo netstat -lpn | grep :9083

tcp        0      0 0.0.0.0:9083            0.0.0.0:*               LISTEN      8950/java

- sudo kill 8950