** When checking if a string matches a string in Java, it is strictly forbidden to use the == operator! Use the equals method! ** **
・ Exception occurs when hoge is null in hoge.equals (“Hello”) ・ *** Exceptions can be prevented by bringing the value first, such as “Hello” .equals (hoge), or by determining hoge! = Null ***
//comment
/*
*comment
*/
/**
*Description (Summary explanation + Detailed explanation)
*
* block tags...(Block tag)
*/
A description of the @param parameter (argument). @return A description of the return value. @author A description of the author of the program. @throw A description of the exception that is thrown.
About comment types Basic knowledge of Javadoc How to write Javadoc documentation comments
Map
There is a "key" for each value, and the "key" and "value" are paired.
Must be initialized with HashMap.
To declare a Map object using the HashMap class, write as follows.
Java
Map<Key model name,Value type name>Object name= new HashMap<>();
Describe as follows.
Java
Map type object name.put(Key,value);
Java
Map type object name.get(Key);
When handling the data registered in Map in a batch, it is common to process it in a loop.
Java does not have a foreach instruction, but it has the same processing method as foreach in other languages.
It is called an extended for statement.
Describe as follows.
Java
for(Type variable name:List name){
System.out.println(Variable name);
}
You can also write loop processing using a mechanism called Iterator.
Describe as follows.
Java
for(Iterator<Map.Entry<Key model name,Value type name>> iterator = map.entrySet().iterator() ; iterator.hasNext() ;){
Map.Entry<Key model name,Value type name> entry = iterator.next();
System.out.println(entry.getKey() + " : " + entry.getValue());
}
Unlike arrays and Lists, Maps have a mechanism called "keys".
To get only the name of the "key" in the Map, use the keySet method.
Describe as follows.
Java
Map type object name.keySet()
Use the containsKey method to determine if a particular "key" is included in the Map.
Describe as follows.
Java
Map type object name.containsKey(Key to search)
Conditional expression?Equation 1:Equation 2
Evaluates the conditional expression and returns expression 1 if it is true and expression 2 if it is false. One thing to keep in mind when writing the conditional operator is to make the data types of Expression 1 and Expression 2 the same. This is because it is inconvenient if the data types returned by TRUE (true) and FALSE (false) in the conditional expression are different, because some numerical value or character is returned as a result of the conditional expression.
The specific program is as follows.
ConditionalTest.java
/* ConditionalTest */
public class ConditionalTest {
public static void main(String[] args) {
int i = 2;
int j = 3;
int k = (i > j) ? 4 : 0;
System.out.println("k = " + k);
System.out.println("i = " + i);
System.out.println((i >= j) ? "i is 3 or more" : "i is 3 or less");
}
}
C:\java>java ConditionalTest
k = 0
i = 2
i is 3 or less
Use the format function. Reference: http://write-remember.com/program/java/format/
Use | Description example | Remarks |
---|---|---|
Fill the beginning of the string with spaces | String.format("%6s", "abc") | Adds a blank character to the left of the specified character string up to 6 digits. |
Fill in the blanks after the string | String.format("%-6s", "abc") | Adds an empty string to the right of the specified character string up to 6 digits. |
Fill the beginning of the number with 0 | String.format("%06d", 123) | Zero-pads the specified number to the left of the string up to 6 digits. |
Separate numbers by commas every 3 digits | String.format("%,d", 123456789) | Separates the specified numbers into commas every 3 digits |
** Set the number of digits (6 digits ("% 6s") in the example below) in the first argument, Set the target character string in the second argument. ** **
test.java
public void formatTest2() {
//Expected value
String expected = "123";
//000123 is assigned to result
String result = String.format("%6s", "123").replace(" ", "0");
filter It is an intermediate operation. The argument is a lambda expression that is T-> boolean. Collect only elements whose expression is true.
forEach For the argument of forEach, pass a lambda expression such as (T)-> void. This is a termination operation that takes out elements one by one and performs some processing. Please refer to the following for how to use.
Only if the list has a start time and an end time, do end time-start time, The example is a method that sets a value for the required time.
calculateDuration.java
protected List<String> calculateDuration(
List<String> list) {
list.stream()
//Narrow down to those with both start time and end time set
.filter(i -> !Strings.isEmpty(i.getTimeStart()) && !Strings.isEmpty(i.getTimeFin()))
//End time-Subtract the start time and set it to the required time
.forEach(i -> {
String duration = Integer.toString(Integer.parseInt(i.getTimeFin()) - Integer.parseInt(i.getTimeStart()));
i.setDuration(duration);
});
return list;
}
Recommended Posts