Java7ã«ãã£ããã¢ãã
TopCoderãAtCoderã§ãJava7ã使ããããã«ãªã£ããã¨ã ããæ¢ã¾ã£ã¦ããç¥èãã¢ãããã¼ãããã
Java7ã®æ°æ©è½ã«ã¤ãã¦ãæ°ã«ãªããã®ããã®ãããããæ¾ã£ã¦ãã¦è©¦ããã
http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html
Binary Literals
2é²æ°ãªãã©ã«ãå é ã«0bï¼ã¾ãã¯0Bï¼ãä»ããã
int one = 0b00000001; int two = 0b00000010; int four = 0b00000100; int FF = 0b11111111; System.out.println(Integer.toBinaryString(FF ^ (one | two | four)));
åºå
11111000
Underscores in Numeric Literals
æ°å¤ãªãã©ã«ã®ä¸ã«ã¢ã³ãã¼ã¹ã³ã¢ãå«ã¾ãããã¨ãã§ããã
System.out.println(123_456_789);
System.out.println(3.141_592_653_589_793);
Strings in switch Statements
switchæã§Stringã使ããããã«ãªã£ãã
switch (args[0]) { case "-n": int n = Integer.parseInt(args[1]); break; case "-h": System.out.println("usage: hogehoge"); break; case "-v": System.out.println("version 3.14"); break; default: break; }
if-then-elseããéãããã
try-with-resources, AutoCloseable
try-catch-finallyã使ããã«ãªã½ã¼ã¹èªå解æ¾
try (Scanner sc = new Scanner(System.in)) { System.out.println(sc.nextInt()); }
tryã®ä¸ã«ã¯AutoCloseableインターフェースãå®è£
ããã¯ã©ã¹ãæå®ããã
Type Inference for Generic Instance Creation
ã¸ã§ããªãã¯ãªã¯ã©ã¹ã®åæåã§ã³ã³ã¹ãã©ã¯ã¿ã«åãã©ã¡ã¼ã¿ãæå®ããªãã¦è¯ããªã
ArrayList<Integer> list = new ArrayList<>(); HashMap<String, Integer> map = new HashMap<>();
java.nio.fileããã±ã¼ã¸
http://docs.oracle.com/javase/7/docs/api/java/nio/file/package-summary.html
Filesã¯ã©ã¹ã¨ãPathã¯ã©ã¹ã¨ãããã¡ã¤ã«æä½ã¾ããã«ä¾¿å©ãªã©ã¤ãã©ãªã追å ããã¦ããã
FileSystem fileSystem = FileSystems.getDefault(); Path src = fileSystem.getPath("./tmp.txt"); Path dst = fileSystem.getPath("./copyTo.txt"); try { Files.copy(src, dst); Files.delete(src); for (String line : Files.readAllLines(dst, Charset.forName("UTF-8"))) { System.out.println(line); } } catch (IOException e) { System.err.println(e); }
Objectsã¯ã©ã¹
http://docs.oracle.com/javase/7/docs/api/java/util/Objects.html
å°ç©é¢æ°ç¾¤ã
int hash = Objects.hash("string", 42, 3.14); String str = "hoge"; String nullStr = null; Objects.equals(str, nullStr); System.out.println(Objects.toString(nullStr, "this object is NULL!!!!"));
BitSet
http://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html
å¾®å¦ã«ã¡ã½ããã追å ããã¦ããã
BitSet bitset = BitSet.valueOf(new long[] { 0xFFFF_FFFF_FFFF_FFFFL, 0x1234_5678_9ABC_DEF0L }); for (int i = bitset.previousSetBit(bitset.size()); i >= 0; i = bitset.previousSetBit(i - 1)) { System.out.println(i); } byte[] bytes = bitset.toByteArray();
Integer#compare
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#compare%28int,%20int%29
èªåã§æ¯è¼ã³ã¼ããæ¸ããªãã¦è¯ããªã£ãã
Longãªã©ã§ãåããDoubleã«ã¯åãããã£ã
Integer.compare(0, 0); Long.compare(Long.MAX_VALUE, Long.MIN_VALUE);