JAVA memorandum

I write things that I often use Java but can't remember.

Map loop
for (Map.Entry<String, String> entry : map.entrySet())
Array initialization
String[] str = new String[3];
String[] str = { "aaa", "bbb", "ccc" };
Joining arrays
org.apache.commons.lang3.ArrayUtils.addAll(Array 1,Array 2);
Compare with multiple strings
if (Arrays.asList("aaa", "bbb").contains(String to compare)) {}
Read file
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
    }
}
Regular expression group
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
}
Date and time format
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

//Zero padding
String.format("%05d", 1); // 00005
String.format("%02d", 1); // 01
Easy implementation of bean toString
@Override
public String toString() {
    return org.apache.commons.lang3.builder.ReflectionToStringBuilder.toString(this);
}
Use of Map properly

The arrangement of HashMap k = v is appropriate. high speed Hashtable key is in descending order TreeMap key is in ascending order LinkedHashMap Arrange in the order in which k = v is entered. If you want to make a Map into a List, you should make it with this

VM argument

Increase memory. -Xmx3072m -Xms512m

Web service where you can try Java code

https://paiza.io/ja/projects/new

Rethrow exception

If you use throw e in the catch clause, the stack trace information will be lost, so wrap it in another exception such as throw new XXXException ("", e) and throw it.

Recommended Posts

Java memorandum
JAVA memorandum
Java memorandum (list)
Java study memorandum
[Java] Optional memorandum
memorandum
Java
Java8 Silver exam memorandum
memorandum
Java
Java8 Gold exam memorandum
Java 3 major elements (object-oriented) memorandum
Java learning (0)
Studying Java ―― 3
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
MyBatis memorandum
Studying Java -5
java (interface)
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
Rails memorandum
Error memorandum
java beginner 3
Java memo
java (encapsulation)
Java inheritance
Java basics
Decompile Java
[Java] Annotation