Skip to content

Instantly share code, notes, and snippets.

@adeleinr
Created December 4, 2015 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adeleinr/4995c0adeb6218b566c2 to your computer and use it in GitHub Desktop.
Save adeleinr/4995c0adeb6218b566c2 to your computer and use it in GitHub Desktop.
Try that autocloses of resources
=================================
try (
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest))
{
// code
}
Shorter and more concise Type deifnitions in generics
=====================================================
Before:
Map<String, List<String>> anagrams =
new HashMap<String, List<String>>();
Now:
Map<String, List<String>> anagrams = new HashMap<>();
String in case statement
========================
String s = "foo";
switch(s) {
case "quux":
processQuux(s);
// fall-through
case "foo":
case "bar":
processFooOrBar(s);
break;
}
Multiple exception catching
============================
Before:
} catch (FirstException ex) {
logger.error(ex);
throw ex;
} catch (SecondException ex) {
logger.error(ex);
throw ex;
}
Now:
try{
// Some code
} catch (FirstException | SecondException ex) {
logger.error(ex);
throw ex;
}
SafeVarargs
===========
Before:
@SuppressWarnings({"unchecked", "varargs"})
public static void printAll(List<String>... lists){
for(List<String> list : lists){
System.out.println(list);
}
}
Now:
@SafeVarargs
public static void printAll(List<String>... lists){
for(List<String> list : lists){
System.out.println(list);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment