URL sınıfı Http bağlantısı açabilir. URI sınıfı ise bu yeteneğe sahip değil.
getHost metodu
Örnek ver
If you connected your database to Intellij IDEA, and installed JPA buddy plugin then you can easily create an entity class with the help of plugin without writing a single line of code.
If you use JPA Hibernate in your application, this will come in very handy. It is very common to use the entity class to create the object directly at the place of generation of data, but sometimes that is not suitable. In such cases we need a DTO to transform the data into persistable form. Here, JPA Buddy comes in clutch where you can create a DTO directly from the entity and edit it the way you want. This is especially useful when you need DTOs for multiple entities. Within a couple of clicks you’ll have all the DTOs ready.
This method returns the value if present. Otherwise, it invokes other and returns the result of the invocation.Optional boş değilse taşıdığı değeri, boşsa belirtilen supplier çağrısının sonucunu döner.
public T orElseGet(Supplier<? extends T> supplier)//If a value is present, returns the value, otherwise returns the result produced by the
//supplying function.//Parameters://supplier - the supplying function that produces a value to be returned//Returns://the value, if present, otherwise the result produced by the supplying function//Throws://NullPointerException - if no value is present and the supplying function is null
public Optional<Table> retrieveTable() {return Optional.ofNullable(constructTableFromCache()).orElse(fetchTableFromRemote());}
public Optional<Table> retrieveTable() {return Optional.ofNullable(constructTableFromCache()) .orElseGet(this::fetchTableFromRemote); }
return stuff().that().returns().optional().orElseGet(() -> {
...
return alternateResult;
});
Optional<User> userOptional = userDao.findByCellNumber(cellNumber);if (!userOptional.isPresent()) {throw new UserNotFoundException("...");} else {User user = userOptional.get();//...}
User user = userDao.findByCellNumber(cellNumber).orElseThrow(UserNotFoundException::new);
If the value is present, this method returns the contained value. Otherwise, it throws an exception to be created by the provided supplier.
Optional<Integer> anyOddInStream = Stream.of(2, 4, 6, 8)
.filter(x -> x % 2 == 1)
.findAny();
var current = anyOddInStream.orElseThrow();
orElseThrow metodu - SupplierT orElseThrow(Supplier<? super T> exceptionSupplier)
// exception could be replaced with any other
Integer opt = anyOddInStream.orElseThrow(
() -> new NoSuchElementException("No value present"));
The CDI framework provides us with a built-in stereotype @javax.enterprise.inject.Model which is intended for use with beans that define the model layer of an MVC web application architecture such as Jakarta Server Faces.
The opening delimiter is defined by a sequence of three double quotes, followed by zero or more spaces and a line terminator. The content of the text block starts from the first character after the line terminator. Therefore, any white spaces between the three quotation marks and the line terminator are not taken into consideration.The closing delimiter, on the other hand, is defined only by three double quotes sequence. The content of the text block ends with the character preceding the first double quotes in the sequence, of the closing delimiter.
There are three phases that are performed during compile-time:- Normalization of line terminators.- Removal of white spaces that were introduced to align the text block with the Java code.- Interpretation of escape characters.
Normalization for text blocks always transforms all line terminators into LF, regardless of the platform on which it runs.
After the normalization process, a text block will clearly consist of one or more lines. The algorithm for removing superfluous white spaces, (i.e., the spaces introduced to align the text block code with the Java code) includes:- The removal of all of the white spaces that are at the end of each line.- The removal of all of the white spaces that are at the beginning of each line, common to all lines.
The algorithm described in this section is implemented through the use of the static method of the String class introduced with Java 13 stripIndent.
3. Interpretation of escape charactersString textBlock = """This is a text block!""";
Technically it is also possible to use the escape characters, and \", but it is useless and therefore not recommended.
Returns the value if present otherwise, returns other.
public T orElse(T other)//If a value is present, returns the value, otherwise returns other.//Parameters://other - the value to be returned, if no value is present. May be null.//Returns://the value, if present, otherwise other
Optional<Foo> opt = ...
opt.orElse(myFunc(...));
orElseGet() lamda aldığı için lazy. Dolayısıyla çözüm olarak şöyle yapmak gerekir.opt.orElseGet(() -> myFunc(...));
public String getPersonName() {Optional<String> name = getName();if (name.isPresent()) {return name.get();}return "DefaultName";}
public String getPersonName() {Optional<String> name = getName();return name.orElse("DefautName");}
List<String> strings = Arrays.asList("alpha","beta","gamma");strings .stream() .collect(Collectors.maxBy(Comparator.naturalOrder())) .get(); // output: gamma
List<Integer> integers = Arrays.asList(1,2,3,4,5,6,6);List<String> strings = Arrays.asList("alpha","beta","gamma");integers.stream().collect(Collectors.minBy(Comparator.naturalOrder())).get();// output: 1strings.stream().collect(Collectors.minBy(Comparator.naturalOrder())).get();// output: alpha
System.out.println(Stream.of(1, 2, 3, 4, 5)
.map(i -> {
System.out.println("processing " + i);
return i * 2;
}).count());
System.out.println(Stream.of(1, 2, 3, 4, 5)
.map(i -> {
System.out.println("processing " + i);
return i * 2;
}).collect(Collectors.counting()));
An implementation may choose to not execute the stream pipeline (either sequentially or in parallel) if it is capable of computing the count directly from the stream source. In such cases no source elements will be traversed and no intermediate operations will be evaluated...
Map<String, Long> collect =
wordsList.stream().collect(groupingBy(Function.identity(), counting()));
ÖrnekList<Integer> integers = Arrays.asList(1,2,3,4,5,6,6);Long collect = integers.stream().filter(x -> x < 4).collect(Collectors.counting());// output: 3
int countThreshold = 2;
long sum =
words.stream()
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()))
.values()
.stream()
.filter(x -> x >= countThreshold)
.reduce(0L, Long::sum);
Set<String> sOne = ...; Set<String> sTwo = ...; long numFound = sOne.parallelStream() .filter(segment -> sTwo.contains(segment)) .collect(Collectors.counting());
The Collectors.toUnmodifiableList would return a Collector that disallows null values and will throw NullPointerException if it is presented with a null value.
List<Integer> result = Arrays.asList(1, 2, 3, 4)
.stream()
.collect(Collectors.toUnmodifiableList());