List<Integer> list = new ArrayList(Arrays.asList(1, 2, 3));
Initialize Set variables
// Using Another Collection Instance Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3));
// Using Anonymous Class Set<Integer> set = new HashSet() {{ add(1); add(2); }};
// Using Stream Since Java 8 Set<String> set3 = Stream.of("a", "b", "c") .collect(Collectors.toCollection(HashSet::new));
Initialize Maps variables
// Using Anonymous Class (Not recommend, Double Brace Initialization should not be used. Replace of to use map.put(key, value).) Map<Integer, String> map = new HashMap<Integer, String>() {{ put(1, "one"); put(2, "two"); put(3, "three"); }};
// Using Stream Since Java 8 Map<String, Integer> map2 = Stream.of(new Object[][] { { "key1", 1 }, { "key2", 2 }, }).collect(Collectors.toMap(data -> (String) data[0], data -> (Integer) data[1]));
// Using a Stream of Map.Entry Map<String, Integer> map3 = Stream.of( new AbstractMap.SimpleEntry<>("key1", 1), new AbstractMap.SimpleEntry<>("key2", 2)) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
// JDK 8 (Don't expose internal_list reference of unmodifiableList(List internale_list)). Arrays.asList() can't increase size, but it can modify its elements. public static final List UNMODIFY_LIST = Collections.unmodifiableList(Arrays.asList(1, 2, 3));
// JDK 9 (Recommend, less space cost) public static final List stringList = List.of("a", "b", "c");
Initialize Immutable Set
// JDK 8 public static final Set<String> stringSet = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("a", "b", "c")));
// JDK 9 (Recommend) public static final Set<String> stringSet2 = Set.of("a", "b", "c");
Initialize Immutable Map
// Immutable Map, JDK 8 public static final Map<Integer, String> UNMODIFY_MAP = Collections.unmodifiableMap( new HashMap<Integer, String>() { { put(1, "one"); put(2, "two"); put(3, "three"); }; });
// java 9, return ImmutableCollections (Recommend) public static final Map<Integer, String> my_map2 = Map.of(1, "one", 2, "two");
// java 10, return ImmutableCollections (Recommend) public static final Map<Integer, String> my_map3 = Map.ofEntries( entry(1, "One"), entry(2, "two"), entry(3, "three"));
Enumeration Type vs Constants
If number of a set of constant is fixed, you should use enum type.
If number of a set of constant is increasing and variable, you should use constant variables.
public static final int MONDAY = 0; public static final int TUESDAY = 1; public static final int WEDNESDAY = 2; public static final int THURSDAY = 3; public static final int FRIDAY = 4; public static final int SATURDAY = 5; public static final int SUNDAY = 6;
Type Formatting
Integer formatting
// Specifying length. Stringstr2= String.format("|%10d|", 101); // | 101| // Left-justifying within the specified width. Stringstr3= String.format("|%-10d|", 101); // |101 | // Filling with zeroes. Stringstr4= String.format("|%010d|", 101); // |0000000101|
Strings= String.format("name is %s", name); // Specify Maximum Number of Characters String.format("|%.5s|", "Hello World"); // |Hello| // Fill with zeros String.format("%32s", Integer.toBinaryString(value)).replace(' ', '0')
If the template string contain %, you need replace with %%. For example,
String.format("%s increase 7%%", "Stock"); // Stock increase 7% String.format("name like '%%%s%%'", "Jack"); // name like '%Jack%'