Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

Java 8 Supplier & Consumer explanation for the layperson

As a non-Java programmer learning Java, I am reading about Supplier and Consumer interfaces at the moment. And I can't wrap my head around their usage and meaning. When and why you would use these interfaces? Can someone give me a simple layperson example of this… I'm finding the Doc examples not succinct enough for my understanding.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The reason you're having difficulty grasping the meaning of functional interfaces such as those in java.util.function is that the interfaces defined here do not have any meaning! They are present primarily to represent structure, not semantics.

This is atypical for most Java APIs. The typical Java API, such as a class or interface, has meaning, and you can develop a mental model for what it represents and use that to understand the operations on it. Consider java.util.List for example. A List is a container of other objects. They have a sequence and an index. The number of objects contained in the list is returned by size(). Each object has an index in the range 0..size-1 (inclusive). The object at index i can be retrieved by calling list.get(i). And so forth.

The functional interfaces in java.util.function don't have any such meaning. Instead, they're interfaces that merely represent the structure of a function, such as the number of arguments, the number of return values, and (sometimes) whether an argument or return value is a primitive. Thus we have something like Function<T,R> which represents a function that takes a single argument of type T and returns a value of type R. That's it. What does that function do? Well, it can do anything ... as long as it takes a single argument and returns a single value. That's why the specification for Function<T,R> is little more than "Represents a function that accepts one argument and produces a result."

Clearly, when we're writing code, it has meaning, and that meaning has to come from somewhere. In the case of the functional interfaces, the meaning comes from the context in which they're used. The interface Function<T,R> has no meaning in isolation. However, in the java.util.Map<K,V> API, there is the following:

V computeIfAbsent(K key, Function<K,V> mappingFunction)

(wildcards elided for brevity)

Ah, this use of Function is as a "mapping function". What does that do? In this context, if key is not already present in the map, the mapping function is called and is handed the key and is expected to produce a value, and the resulting key-value pair is inserted into the map.

So you can't look at the specification for Function (or any of the other functional interfaces, for that matter) and attempt to discern what they mean. You have to look at where they're used in other APIs to understand what they mean, and that meaning applies only to that context.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...