स्निपेट·मुफ़्त
Java स्निपेट
20 Java स्निपेट: वैलिडेशन वाले record, switch एक्सप्रेशन और पैटर्न मैचिंग, stream और Collectors, Optional, try-with-resources, वर्चुअल थ्रेड, java.net.http और JUnit 5 टेस्ट। कुछ के लिए Java 17 या 21 चाहिए।
मुफ़्त में इंस्टॉल करें।
अगर आपके Android डिवाइस पर PocketCode इंस्टॉल है, तो यह पेज ऐप में खुलेगा।
प्रीव्यू
20 में से 5
Record with validationrecv
Record whose compact constructor rejects invalid values
public record ${1:Point}(${2:int x, int y}) {
public ${1:Point} {
if (${3:x < 0 || y < 0}) {
throw new IllegalArgumentException("${4:coordinates must be positive}");
}
}
}Switch expressionswexpr
Switch that returns a value, with arrow cases
var ${1:label} = switch (${2:day}) {
case ${3:SATURDAY, SUNDAY} -> ${4:"weekend"};
default -> ${5:"weekday"};
};Try-with-resources readertrywr
Read a file line by line and close it automatically
try (var ${1:reader} = Files.newBufferedReader(Path.of("${2:data.txt}"))) {
String line;
while ((line = ${1:reader}.readLine()) != null) {
$0
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}Stream filter and mapstreamf
Filter and transform a list into a new unmodifiable list (Java 16+)
List<${1:String}> ${2:cleaned} = ${3:items}.stream()
.filter(${4:s -> !s.isBlank()})
.map(${5:String::trim})
.toList();Group by a keygroupby
Collect a stream into a map of lists keyed by a property
Map<${1:String}, List<${2:Person}>> ${3:byCity} = ${4:people}.stream()
.collect(Collectors.groupingBy(${5:Person::city}));1.0.0 में बदलाव
पहला संस्करण।