Last active
October 24, 2022 13:42
-
-
Save MuffinTheMan/6c60d9190d596f8a5d3452ef10694a72 to your computer and use it in GitHub Desktop.
Snippets of Java code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
void standardForLoop() { | |
final List<String> fruit = new ArrayList<>( | |
Arrays.asList("apple", "banana", "orange", "mango") | |
); | |
int counter = 0; | |
for (int i = 0; i < fruit.size(); i++) { | |
if (counter++ < 10) { | |
fruit.add("pineapple"); | |
} | |
} | |
assertThat(counter).isEqualTo(fruit.size()); | |
assertThat(fruit).hasSize(14); | |
} | |
@Test | |
void forInLoop() { | |
assertThatThrownBy(() -> { | |
final List<String> fruit = new ArrayList<>( | |
Arrays.asList("apple", "banana", "orange", "mango") | |
); | |
for (final String item : fruit) { | |
fruit.add("pineapple"); | |
} | |
}).isInstanceOf(ConcurrentModificationException.class); | |
} | |
@Test | |
void forEachLoop() { | |
assertThatThrownBy(() -> { | |
final List<String> fruit = new ArrayList<>( | |
Arrays.asList("apple", "banana", "orange", "mango") | |
); | |
fruit.forEach(item -> fruit.add("pineapple")); | |
}).isInstanceOf(ConcurrentModificationException.class); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// STREAMS | |
// Convert List of numbers as Strings into a Set of Integers. | |
// This would remove duplicates by nature of how the Set works. | |
final List<String> numberStrings = Arrays.asList("1", "2", "2", "3", "4", "4"); | |
final Set<Integer> uniqueNumbers = numberStrings.stream().map(Integer::parseInt).collect(Collectors.toSet()); | |
// STRING FORMATTING | |
void example1() { | |
// https://docs.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html | |
final int MILES_BETWEEN_EARTH_AND_MOON = 226_000; | |
final int MILLISECONDS_PER_HOUR = 3_600_000; | |
// https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html | |
String.format( | |
"There are %s miles between the earth and moon at perigee.", | |
MILES_BETWEEN_EARTH_AND_MOON | |
); | |
// There are 226000 miles between the earth and moon at perigee. | |
String.format( | |
"There are %s milliseconds in one hour.", | |
MILLISECONDS_PER_HOUR | |
); | |
// There are 3600000 milliseconds in one hour. | |
String.format( | |
"There are %,d miles between the earth and moon at perigee.", | |
MILES_BETWEEN_EARTH_AND_MOON | |
); | |
// There are 226,000 miles between the earth and moon at perigee. | |
String.format( | |
"There are %,d milliseconds in one hour.", | |
MILLISECONDS_PER_HOUR | |
); | |
// There are 3,600,000 milliseconds in one hour. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment