16
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JAVA備忘録

Last updated at Posted at 2017-12-07

Javaのよく使うけど覚えられないことを書いていく。

Mapのループ
for (Map.Entry<String, String> entry : map.entrySet())
配列の初期化
String[] str = new String[3];
String[] str = { "aaa", "bbb", "ccc" };
配列の結合
org.apache.commons.lang3.ArrayUtils.addAll(配列1, 配列2);
複数の文字列と比較
if (Arrays.asList("aaa", "bbb").contains(比較する文字列)) {}
ファイルの読み込み
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
    }
}
正規表現のグループ
String str = "aaa-bbb-ccc-ddd";
Pattern p = Pattern.compile("^aaa-(.*)-(.*)-ddd$");
Matcher m = p.matcher(str);
if (m.find()) {
    System.out.println(m.group(1)); // bbb
    System.out.println(m.group(2)); // ccc
}
日時フォーマット
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
Date date = sdf.parse("2018/01/01 10:00:00.000");
String dateStr = sdf.format(date);
String.format
// ゼロ埋め
String.format("%05d", 1); // 00005
String.format("%02d", 1); // 01
BeanのtoStringの簡単実装
@Override
public String toString() {
    return org.apache.commons.lang3.builder.ReflectionToStringBuilder.toString(this);
}
Mapの使い分け

HashMap k=vの並びが適当。高速
Hashtable keyが降順になる
TreeMap keyが昇順になる
LinkedHashMap k=vを入れた順番に並ぶ。MapをList化するのであればこれで作るべき

VM引数

メモリを増やす。-Xmx3072m -Xms512m

Javaコードを試せるWebサービス

例外の再スロー

catch句でthrow e とするとスタックトレース情報が失われるため、throw new XXXException("", e)など、別の例外でラップしてthrowする

16
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?