for (String item : items) {
...
}
for (int i = 0; i < items.size(); i++) {
String item = items.get(i);
...
}
Iterator<String> iterator = items.getIterator();
while (iterator.hasNext()) {
String item = iterator.next();
...
}
for (String value : mapOfValues.values()) {
...
}
for (String key : mapOfValues.keySet()) {
...
}
for (Map.Entry<String, String> entry : mapOfValues.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
...
}
for (String key : mapOfValues.keySet()) {
String value = mapOfValues.get(key);
...
}
String nameDescribingTheContent = service.getContent();
String s = service.getContent();
String nameDescribingTheContent = service.getContent(); ... String nameDescribingSomethingElse = service.getSomethingElse();
String s = null; ... s = service.getContent(); ... s = service.getSomethingElse();
List<Something> listOfSomething = new ArrayList<Something>;
for (...) {
...
listOfSomething.add(something);
}
for (...) {
List<Something> onlyInsideLoop = new ArrayList<Something>;
...
}
List<Something> listOfSomething = null;
...
for (...) {
...
if (listOfSomething == null) {
listOfSomething = new ArrayList<Something>;
}
listOfSomething.add(something);
}
List<Something> onlyInsideLoop = null;
...
for (...) {
onlyInsideLoop = new ArrayList<Something>;
...
}
try {
// A few lines doing something specific
...
} catch (SpecificException e) {
// Handling the one or two things that you actually
// can do something about
...
}
try {
... lots of lines ...
} catch (Exception e) {
// Probably something exceptionally complex
...
}
try {
...
} catch (Exception e) {
// Nothing
}
try {
...
} catch (Throwable t) {
// What the fuck do you think you are doing?
...
}
try (PreparedStatement statment = connection.prepareStatment(sql)) {
...
} catch (SQLException e) {
// Either retry or just log the error or don't catch at all
...
}
PreparedStatement statment = null;
try {
statment = connection.prepareStatment(sql);
...
} catch (SQLException e) {
...
} finally {
if (statment != null) {
statment.close();
}
}
Path filePath = Paths.get("SomeFile.txt");
try (InputStream inputStream = Files.newInputStream(filePath)) {
...
}
InputStream inputStream = ...
...
if (inputStream != null) {
inputStream.close();
}
private boolean checkForSomeCondition(...) {
for (...) {
...
if (someCondition) {
return true;
}
}
return false;
}
try {
for (...) {
...
if (someCondition) {
throw new SomeException();
}
}
} catch (SomeException exception) {
// Maybe do something with the exception, maybe not
}
public class AnObject implements Comparable{ @Override public int compareTo(AnObject other) { return Integer.compare(id, other.getId()); } }
@SuppressWarnings("rawtypes")
public class AnObject implements Comparable {
@Override
public int compareTo(Object arg0) {
AnObject anObject = (AnObject) arg0;
...
}
}
public int compareTo(AnObject other) {
return Integer.valueOf(this.id).compareTo(Integer.valueOf(other.getId()));
}