What are the use of constructor reference in java 8

I read about the Java 8 features that led me to this article and I was interested to know about the actual use of the constructor reference, I mean why not just use it new Obj?

PS, I tried googling, but I was not able to find something meaningful, if someone has an example code, link or tut, it will be great

+4
source share
2 answers

First of all, you should understand that references to constructors are just a special form of method references. The point in reference methods is that they do not call the reference method, but provide a way to determine the function that will call the method during evaluation.

Examples of related articles may not seem so useful, but this is a common problem with a short stand-alone code example. This is exactly the same as with the hello world program. This is no more useful than typing "hello world" directly into the console, but that doesn’t mean that anyway. Its purpose is to demonstrate a programming language.

As assylias has shown , there are options for using an existing functional interfaceusing the JFC API.


, , () interface : .

interface , - , new SomeType(…).

, Factory, interface , Factory - .

- , , - . , interface , .

+7

, . :

List<String> filtered = stringList.stream()
        .filter(s -> !s.isEmpty())
        .collect(Collectors.toCollection(ArrayList::new)); //() -> new ArrayList<> ()

Map<String, BigDecimal> numbersMap = new HashMap<>();
numbersMap.computeIfAbsent("2", BigDecimal::new); // s -> new BigDecimal(s)

someStream.toArray(Object[]::new); // i -> new Object[i]

.

+4

All Articles