Hash set

import java.util.*;

public class MyApp {

	public static void main(String[] args){
		// HashSet
		HashSet<Integer> sales = new HashSet<>();

		sales.add(10);
		sales.add(20);
		sales.add(30);
		sales.add(10);

		System.out.println(sales.size());

		for(Integer sale: sales){
			System.out.println(sale);
		}

		sales.remove(30);

		for(Integer sale: sales){
			System.out.println(sale);
		}
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
3
20
10
30
20
10

import java.util.*;

public class MyApp {

	public static void main(String[] args){
		
		// HashMap<String, Integer> sales = new HashMap<>();
		Map<String, Integer> sales = new HashMap<>();

		sales.put("tom", 10);
		sales.put("bob", 20);
		sales.put("steve", 30);

		System.out.println(sales.get("tom"));
		System.out.println(sales.size());

		for (Map.Entry<String, Integer> sale: sales.entrySet()){
			System.out.println(sale.getKey() + ":" + sale.getValue());
		}

		sales.put("tom", 100);
		sales.remove("steve");

		for (Map.Entry<String, Integer> sale : sales.entrySet()){
			System.out.println(sale.getKey() + ":" + sale.getValue());
		}
	}
}
import java.util.*;

public class MyApp {

	public static void main(String[] args){
		
		List<Integer> sales = new ArrayList<>(Arrays.asList(12, 30, 22, 4, 9));
		sales
			.stream()
			.filter(e -> e % 3 == 0)
			.map(e -> "(" + e + ")")
			.forEach(System.out::println);

		System.out.println();
	}
}
import java.time.*;
import java.time.format.DateTimeFormatter;

public class MyApp {

	public static void main(String[] args){
		LocalDateTime d = LocalDateTime.now();
		System.out.println(d.getYear());
		System.out.println(d.getMonth());
		System.out.println(d.getMonth().getValue());
		System.out.println(d.plusMonths(2).minusDays(3));
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy!MM!dd!");
		System.out.println(d.format(dtf));
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
2018
SEPTEMBER
9
2018-11-13T23:12:54.812
2018!09!16!

スレッド

class MyRunnable implements Runnable{
@Override
public void run(){
for (int i = 0; i < 500; i++){ System.out.print('*'); } } } public class MyApp { public static void main(String[] args){ MyRunnable r = new MyRunnable(); Thread t = new Thread(r); t.start(); for (int i = 0; i < 500; i++){ System.out.print('.'); } } } [/java] [vagrant@localhost java]$ java MyApp ..............................................................................................................................................******************************************************************************...................................................................................................................................................**********************************************************************************************************************************************************************************************************************************************...................................................................................................................................................................................................................**************************************************************************************************************************************************************************************** String class [java] public class MyApp { public static void main(String[] args){ String s = "abcdef"; System.out.println(s.length()); System.out.println(s.substring(2, 5)); System.out.println(s.replaceAll("ab", "AB")); String s1 = "ab"; String s2 = "ab"; if (s1.equals(s2)){ System.out.println("same!"); } String ss1 = new String("ab"); String ss2 = new String("ab"); if(ss1 == ss2){ System.out.println("same!same!same!"); } } } [/java] [java] public class MyApp { public static void main(String[] args){ int score = 50; double height = 165.8; String name = "yokoi"; System.out.printf("name: %s, score: %d, height: %f\n", name, score, height); System.out.printf("name: %-10s, score: %10d, height: %5.2f\n", name, score, height); } } [/java] [java] public class MyApp { public static void main(String[] args){ double d = 53.234; System.out.println(Math.ceil(d)); System.out.println(Math.floor(d)); System.out.println(Math.round(d)); System.out.println(Math.PI); } } [/java] [vagrant@localhost java]$ javac MyApp.java [vagrant@localhost java]$ java MyApp 54.0 53.0 53 3.141592653589793 Random処理 [java] Random r = new Random(); System.out.println(r.nextDouble()); System.out.println(r.nextInt(100)); System.out.println(r.nextBoolean()); [/java] [java] import java.util.*; public class MyApp { public static void main(String[] args){ // ArrayList sales = new ArrayList<>();
List sales = new ArrayList<>();

sales.add(10);
sales.add(20);
sales.add(30);

for(int i = 0; i < sales.size(); i++){ System.out.println(sales.get(i)); } sales.set(0, 100); sales.remove(2); for(Integer sale : sales){ System.out.println(sale); } } } [/java] [vagrant@localhost java]$ javac MyApp.java [vagrant@localhost java]$ java MyApp 10 20 30 100 20

イニシャライザー

class User {
	private String name;
	private static int count;

	static {
		User.count = 0;
		System.out.println("Static initializer");
	}

	public User(String name){
		this.name = name;
		User.count++;
		System.out.println("Constructor initializer");
	}

	{
		System.out.println("Instance initializer");
	}

	public static void getInfo(){
		System.out.println("# of instances: " + User.count);
	}
}

public class MyApp {

	public static void main(String[] args){
		User.getInfo();
		User tom = new User("Tom");
		User.getInfo();
		User john = new User("John");
		User.getInfo();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Static initializer
# of instances: 0
Instance initializer
Constructor initializer
# of instances: 1
Instance initializer
Constructor initializer
# of instances: 2

final は継承できない。

final class User {
	protected String name;
	private static final double VERSION = 1.1;

	static {
		this.name = name;
		User.VERSION = 1.2;
	}

	public final void sayHi{
		System.out.println("Hi! " + this.name);
	}

	public static void getInfo(){
		System.out.println("# of instances: " + User.count);
	}
}

抽象クラス

// 抽象クラス -> 具象クラス

abstract class User {
	public abstract void sayHi(); // 抽象メソッド
}

class JapaneseUser extends User {
	@Override
	public void sayHi(){
		System.out.println("こんにちは!");
	}
}

class AmericanUser extends User {
	@Override
	public void sayHi(){
		System.out.println("Hi");
	}
}

public class MyApp {

	public static void main(String[] args){
		AmericanUser tom = new AmericanUser();
		JapaneseUser aki = new JapaneseUser();
		tom.sayHi();
		aki.sayHi();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Hi
こんにちは!

interface

interface Printable{
	// 定数
	double VERSION = 1.2;
	// 抽象メソッド
	void print();
	// default メソッド
	public default void getInfo(){
		System.out.println("I/F ver. " + Printable.VERSION);
	} 
	// static メソッド

} 

class User implements Printable {
	@Override
	public void print(){
		System.out.println("Now printing user profile...");
	}
}

public class MyApp {

	public static void main(String[] args){
		User tom = new User();
		tom.print();
		tom.getInfo();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Now printing user profile…
I/F ver. 1.2

列挙型

enum Result {
	SUCCESS,
	ERROR,

}


public class MyApp {

	public static void main(String[] args){
		Result res;

		res = Result.ERROR;

		switch (res){
			case SUCCESS:
				System.out.println("OK!");
				System.out.println(res.ordinal());
			break;
			case ERROR:
				System.out.println("NG!");
				System.out.println(res.ordinal());
				break;
		}

		
	}
}

[vagrant@localhost java]$ java MyApp
NG!
1

例外処理

public class MyApp {

	public static void div(int a, int b){
		System.out.println(a / b);
	}

	public static void main(String[] args){
		div(3, 0);
	}
}

[vagrant@localhost java]$ java MyApp
Exception in thread “main” java.lang.ArithmeticException: / by zero
at MyApp.div(MyApp.java:13)
at MyApp.main(MyApp.java:17)

例外処理

class MyException extends Exception {
	public MyException(String s){
		super(s);
	}
}

public class MyApp {

	public static void div(int a, int b){

		try {
			if (b < 0){
				throw new MyException("not minux!");
			}
			System.out.println(a / b);
		} catch (ArithmeticException e){
			System.err.println(e.getMessage());
		} catch(MyException e){
			System.err.println(e.getMessage());
		}		
	}
	public static void main(String&#91;&#93; args){
		div(3, 0);
		div(5, -2);
	}
}
&#91;/java&#93;

Wrapper Class
&#91;java&#93;
public class MyApp {

	public static void main(String&#91;&#93; args){
		// Integer i = new Integer(32);
		// int n = i.intValue();
		Integer i = 32; // auto boxing
		i = null;
		int n = i; // auto unboxing
		System.out.println();
	}
}
&#91;/java&#93;

&#91;vagrant@localhost java&#93;$ javac MyApp.java
&#91;vagrant@localhost java&#93;$ java MyApp
Exception in thread "main" java.lang.NullPointerException
        at MyApp.main(MyApp.java:14)

型を後程指定するジェネリック
&#91;java&#93;
class MyData<T> {
	public void getThree(T x){
		System.out.println(x);
		System.out.println(x);
		System.out.println(x);
	}
}

public class MyApp {

	public static void main(String[] args){
		MyData<Integer> i = new MyData<>();
		i.getThree(32);
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
32
32
32

パッケージをコンパイルしよう

インポートって、要するに違うファイルからの読み込みね。すっかり忘れてる or スルーしてた。

package jp.hpscript.myapp;
import jp.hpscript.myapp.model.User;
import jp.hpscript.myapp.model.AdminUser;

public class MyApp {

	public static void main(String[] args){
		User tom = new User("Tom");
		// System.out.println(tom.name);
		tom.sayHi();

		AdminUser bob = new AdminUser("bob");
		// System.out.println(bob.name);
		bob.sayHi();
		bob.sayHello();
	}
}
package jp.hpscript.myapp.model;

public class User {
	protected String name;

	public User(String name){
		this.name = name;
	}

	public void sayHi(){
		System.out.println("hi!" + this.name);
	}
}
package jp.hpscript.myapp.model;

public class AdminUser extends User {
	public AdminUser(String name){
		super(name);
	}

	public void sayHello(){
		System.out.println("hello!" + this.name);
	}
	@Override public void sayHi(){
		System.out.println("[admin]hi!" + this.name);
	}	
}

[vagrant@localhost java]$ javac jp/hpscript/myapp/MyApp.java
[vagrant@localhost java]$ java jp.hpscript.myapp.MyApp
hi!Tom
[admin]hi!bob
hello!bob

setter, getter

class User {
	private String name;
	private int score;

	public User(String name, int score){
		this.name = name;
		this.score = score;
	}

	public int getScore(){
		return this.score;
	}

	public void setScore(int score){
		if(score > 0){
			this.score = score;
		}	
	}
}

public class MyApp {

	public static void main(String[] args){
		User tom = new User("Tom", 65);
		tom.setScore(85);
		tom.setScore(-22);
		System.out.println(tom.getScore());
	}
}
// static

class User {
	private String name;
	private static int count = 0;

	public User(String name){
		this.name = name;
		User.count++;
	}

	public static void getInfo(){
		System.out.println("# of instances: " + User.count);
	}
}

public class MyApp {

	public static void main(String[] args){
		User.getInfo();
		User tom = new User("Tom");
		User.getInfo();
		User john = new User("John");
		User.getInfo();
	}
}

java method

public static void sayHi(){
		System.out.println("Hi!");
	}

	public static void main(String[] args){
		sayHi();
	}
public static void sayHi(String name){
		System.out.println("Hi!" + name);
	}

	public static void main(String[] args){
		sayHi("Tom");
		sayHi("Bob");
	}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Hi!Tom
Hi!Bob

public static String sayHi(String name){
		return "Hi!" + name;
	}

	public static void main(String[] args){
		String msg = sayHi("Steve");
		System.out.println(msg);
	}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Hi!Steve

classを使ってみよう

class User {
	String name = "Me!";

	void sayHi(){
		System.out.println("hi!");
	}

}

public class MyApp {

	public static void main(String[] args){
		User tom;
		tom = new User();

		System.out.println(tom.name);
		tom.sayHi();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Me!
hi!

なるほど。

コンストラクタ

class User {
	String name;

	User(String name){
		this.name = name;
	}

	void sayHi(){
		System.out.println("hi!" + this.name);
	}

}

public class MyApp {

	public static void main(String[] args){
		User tom;
		tom = new User("Tom");

		System.out.println(tom.name);
		tom.sayHi();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Tom
hi!Tom

なるほど、何故か急に分かってきた。
classの継承

class User {
	String name;

	User(String name){
		this.name = name;
	}

	void sayHi(){
		System.out.println("hi!" + this.name);
	}
}

class AdminUser extends User {
	AdminUser(String name){
		super(name);
	}

	void sayHello(){
		System.out.println("hello!" + this.name);
	}	
}

public class MyApp {

	public static void main(String[] args){
		User tom = new User("Tom");
		System.out.println(tom.name);
		tom.sayHi();

		AdminUser bob = new AdminUser("bob");
		System.out.println(bob.name);
		bob.sayHi();
		bob.sayHello();
	}
}

@override 継承した時に親クラスのメソッドを使う

class User {
	String name;

	User(String name){
		this.name = name;
	}

	void sayHi(){
		System.out.println("hi!" + this.name);
	}
}

class AdminUser extends User {
	AdminUser(String name){
		super(name);
	}

	void sayHello(){
		System.out.println("hello!" + this.name);
	}
	@Override void sayHi(){
		System.out.println("[admin]hi!" + this.name);
	}	
}

public class MyApp {

	public static void main(String[] args){
		User tom = new User("Tom");
		System.out.println(tom.name);
		tom.sayHi();

		AdminUser bob = new AdminUser("bob");
		System.out.println(bob.name);
		bob.sayHi();
		bob.sayHello();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Tom
hi!Tom
bob
[admin]hi!bob
hello!bob
ほうほう

java 基礎

public static void main(String[] args){
		int[] sales;
		sales = new int[3];

		sales[0] = 100;
		sales[1] = 200;
		sales[2] = 300;
		System.out.println(sales[1]);
		
	}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
200

こうも書ける

public static void main(String[] args){
		int[] sales;
		sales = new int[]{200, 240, 280};

		System.out.println(sales[1]);

	}

forと組み合わせる。

public static void main(String[] args){

int[] sales = {800, 540, 320};

for(int i = 0; i < 3; i++){ System.out.println(sales[i]); } } [/java] (int sale: sales)という書き方 [java] public static void main(String[] args){ int[] sales = {800, 540, 320}; for(int sale: sales){ System.out.println(sale); } } [/java] 基本データ型と参照型 [java] public static void main(String[] args){ int x = 10; int y = x; y = 5; System.out.println(x); System.out.println(y); } [/java] [java] int[] a = {3, 5, 7}; int[] b = a; b[1] = 8; System.out.println(a[1]); System.out.println(b[1]); [/java] 配列の場合はメモリ領域が入る。 [vagrant@localhost java]$ javac MyApp.java [vagrant@localhost java]$ java MyApp 8 8 文字列は別の領域に割り当てる。 [java] public static void main(String[] args){ String s = "hello"; String t = s; t = "world"; System.out.println(s); System.out.println(t); } [/java]

javaをもう一回やろう

キャストする

public static void main(String[] args){
		double d = 52343.231;
		int i = (int)d;
		System.out.println(i);
	}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java Myapp
エラー: メイン・クラスMyappが見つからなかったかロードできませんでした
[vagrant@localhost java]$ java MyApp
52343

if文

public static void main(String[] args){
		int score = 85;
		if(score > 80){
			System.out.println("great");	
		}			
	}
public static void main(String[] args){
		int score = 85;
		String msg = score > 80 ? "great" : "soso..!";
		System.out.println(msg);
	}

switch

public static void main(String[] args){
		String signal = "red";
		switch(signal) {
			case "red":
				System.out.println("stop!");
				break;
			case "blue":
				System.out.println("go");
				break;
			case "yello":
				System.out.println("caution");
				break;
			default:
				System.out.println("wrong signal!");
				break;
		}
	}

public static void main(String[] args){
int i = 0;
while (i < 10){ System.out.println(i); i++; } } [/java] [vagrant@localhost java]$ java MyApp 0 1 2 3 4 5 6 7 8 9 こうも行ける。 [java] public static void main(String[] args){ int i = 0; do { System.out.println(i); i++; } while (i < 10); } [/java] [java] public static void main(String[] args){ for (int i = 0; i < 10; i++){ System.out.println(i); } } [/java] break, continue [java] public static void main(String[] args){ for (int i = 0; i < 10; i++){ if (i==5){ continue; } System.out.println(i); } } [/java] うおおおおおおお、短期間で詰め込んでるな

JDK、JRE/JVM

MyApp.java -> Compile -> MyApp.class

Write once, run anywhere

ではjavaの基礎から行きます。

public class MyApp {

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

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ ls
AdminUser.class MyApp.class MyInteger.class User.class
AmericanUser.class MyApp.java MyRunnable.class com
JapaneseUser.class MyData.class Printable.class java1
MyApp$1.class MyException.class Result.class
[vagrant@localhost java]$ java MyApp
Hello world

変数

public class MyApp {

	public static void main(String[] args){
		// 変数
		String msg;
		msg = "hello world again";

		System.out.println(msg);
	}
}
public class MyApp {

	public static void main(String[] args){

		char a = 'a';
		// 整数 byte short int long
		int x = 10;
		long y = 55555555L;
		double d = 23423.344;
		float f = 32.33F;
		boolean flag = true; // flase
		// \n, \t

		String msg = "hello w\norld a\tgain";

		System.out.println(msg);
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
hello w
orld a gain

public static void main(String[] args){

		int i;
		i = 10 / 3;
		System.out.println(i);
		i = 10 % 3;
		System.out.println(i);
		int x = 5;
		x++;
		System.out.println(x);

	}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
3
1
6

ほ~

zabbix, zabbix20, zabbix22 違い

zabbixはoss総合監視ツール ラトビアzabbixSIA社開発

linux, windows, nw
zabbix agent, snmp, ipmi, エージェントレス監視
リソース監視、死活監視(ICMPPing, プロセス)、web監視、ログ監視、HW監視、SQL監視

2.0系、2.2系、3.0系と、どんどん新しくなっている。
主に性能改善、機能拡充

監視ロジックの改善、メモリの改善、監視の幅、自動化

なるほど、当たり前だけど、バージョンが高い方がいいのね。
では入れていきましょう。
sudo yum install -y zabbix22.x86_64 zabbix22-server.noarch zabbix22-server-mysql.x86_64

エラー: パッケージ: zabbix22-server-mysql-2.2.21-1.el6.x86_64 (epel)
要求: libiksemel.so.3()(64bit)
問題を回避するために –skip-broken を用いることができません
これらを試行できます: rpm -Va –nofiles –nodigest

zabbix3にします。
yum install http://repo.zabbix.com/zabbix/3.0/rhel/6/x86_64/zabbix-release-3.0-1.el6.noarch.rpm

インストール:
zabbix-release.noarch 0:3.0-1.el6

完了しました!

yum -yの[-y]とは?

オプション -y は 問い合わせにすべて「y」と返答するオプション

$ man yum でマニュアルを見ると、

-y, –assumeyes
Assume yes; assume that the answer to any question which would
be asked is yes.
Configuration Option: assumeyes

なるほどね~