Welcome to java boilerplate!
If any solution has code attached to it, feel free to use it - it's placed in the public domain.
See this article from Graham Hamilton for some history on why this blog exists.
Collections.sort(names, new Comparator<String>() {
public int compare(String a, String b) {
return 0; //Some sort of comparison algorithm.
}
}); Collections.sort(names, Comparator<String>(String a, String b) {
return 0; //Some sort of comparison algorithm.
}); public @Function interface Comparator<T> {
int compare(T o1, T o2);
}private String field;
public String getField() {
return field;
}
protected void setField(String field) {
this.field = field;
}
private @Get @Set(Access.PROTECTED) String field;
Iteratori = //Something
Iteratori2 = Iterators.unmodifiableIteratorAdapter(i,
Adapter<String, Integer>(String e) {return Integer.valueOf(e);});
public @Function interface Adapter<I, O> {
O filter(I input);
}return "Usage: \nmyapp \"filename\" -options\n";
return """Usage:
myapp "filename" -options
""";
//This would throw a runtime exception.
Pattern p = Pattern.compile("*");
//This will produce a compile-time error.
Pattern p = R"*";
//This is fine
Pattern k = R"(foo|bar)?\"(.*)\"\\s+";
//This uses the proposed string literal notation.
Pattern r = R"""(foo|bar)?"(.*)"\s+""";
if ( map == null ) {
return null;
} else {
return map.get(param);
}return map.?get(param);
UTF-8 and ISO-8859-1. However, when explicitly specifying character encoding, even with one of these guaranteed character sets, you still have to deal with an UnsupportedEncodingException which can't actually happen. For example: the String constructor with parameters (byte[] data, String encoding) or for example the InputStreamReader and its (InputStream stream, String encoding) constructor.String encoding parameter, where this parameter is replaced with the BasicCharset enum, which contains an option for each guaranteed encoding.String string;
try {
//data is a byte array.
string = new String(data, "UTF-8");
} catch ( UnsupportedEncodingException e ) {
throw new InternalError("Can't happen.");
}
String string = new String(data, BasicCharset.UTF8);
public Reader foobar() {
try {
return new InputStreamReader(System.in, "UTF-8");
} catch ( UnsupportedEncodingException e ) {
throw new Error();
}
}@IgnoreExceptions(UnsupportedEncodingException.class)
public Reader foobar() {
return new InputStreamReader(System.in, "UTF-8");
}
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD)
public @interface IgnoreExceptions {
Class[] value() default {Throwable.class};
}
public Reader foobar() ignores UnsupportedEncodingException {
return new InputStreamReader(System.in, "UTF-8");
}int length = someObject.size();
if ( length == 0 ) {
throw new EmptyCollectionException();
}
for ( int i = 0 ; i < length ; i++ ) {
// ... whatever
}
for ( int i = 0 ; i < someObject.size() ; i++ ) {
// ... whatever
} empty {
throw new EmptyCollectionException();
}@Tag(namespace = "http://foo/bar")
public class RootElement {
@Attribute String id;
@Map String author;
@Map(tagName="published", dateFormat="yyyy-MM-dd HH:mm")
long publishedInMillis;
@Map(mapType=MapType.MULTIPLE_TAGS,
targetClass = IconRef.class, tagName="icon")
Listicons;
}
@Tag
class IconRef {
@Attribute int id;
@SimpleContent String url;
}
<?xml version="1.0" encoding="UTF-8"?>
<rootelement id="something">
<author>Reinier Zwitserloot</author>
<published>2005-11-30 08:01</published>
<icon id="icon1">http://example/icon1.jpg</icon>
<icon id="icon2">http://example/icon2.jpg</icon>
</rootelement>
@Attribute(defaultValue = "something") int something; which will be used if the attribute does not exist in the XML data. When writing, the attribute is omitted if the data in the object structure matches the default.