本文整理汇总了Java中java.util.function.Function.apply方法的典型用法代码示例。如果您正苦于以下问题:Java Function.apply方法的具体用法?Java Function.apply怎么用?Java Function.apply使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.function.Function
的用法示例。
在下文中一共展示了Function.apply方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: zipper
import java.util.function.Function; //导入方法依赖的package包/类
/**
* Zipper
*
* @param object
* @param <K>
* @param <V>
* @param <E>
* @return
*/
static <K, V, E> ConcurrentMap<K, V> zipper(
final Collection<E> object,
final Function<E, K> keyFn,
final Function<E, V> valueFn
) {
final ConcurrentMap<K, V> ret = new ConcurrentHashMap<>();
if (0 < object.size()) {
for (final E item : object) {
if (null != item) {
final K key = keyFn.apply(item);
final V value = valueFn.apply(item);
if (null != key && null != value) {
ret.put(key, value);
}
}
}
}
return ret;
}
示例2: createDownloadFile
import java.util.function.Function; //导入方法依赖的package包/类
private byte[] createDownloadFile(Function<File, File> action) {
Path tmpDir = null;
try {
tmpDir = createTempDir();
if (logger.isDebugEnabled()) {
logger.debug("Generating project in temporary directory " + tmpDir.toFile());
}
File download = action.apply(tmpDir.toFile());
return fileToByteArray(download);
} catch(Exception e) {
throw new RuntimeException(e);
} finally {
if (tmpDir != null) {
if (!FileSystemUtils.deleteRecursively(tmpDir.toFile())) {
logger.warn("Unable to delete temporary directory " + tmpDir.toFile());
}
}
}
}
示例3: fetchAll
import java.util.function.Function; //导入方法依赖的package包/类
@SafeVarargs
public final <T extends WithId<T>> ListResult<T> fetchAll(Class<T> model, Function<ListResult<T>, ListResult<T>>... operators) {
ListResult<T> result;
if (getDataAccessObject(model) != null) {
result = doWithDataAccessObject(model, d -> d.fetchAll());
} else {
Kind kind = Kind.from(model);
Map<String, T> cache = caches.getCache(kind.getModelName());
result = ListResult.of(cache.values());
}
if (operators == null) {
return result;
}
for (Function<ListResult<T>, ListResult<T>> operator : operators) {
result = operator.apply(result);
}
return result;
}
示例4: travel
import java.util.function.Function; //导入方法依赖的package包/类
public static <T, R> void travel(Function<T, R> func, List<Iterator<T>> iterators) {
for (Iterator<T> it : iterators) {
while(it.hasNext()) {
func.apply(it.next());
}
}
}
示例5: walkContext
import java.util.function.Function; //导入方法依赖的package包/类
private <T> Collection<T> walkContext(Function<ApplicationContext, T> contextProcessor) {
final List<T> result = new LinkedList<>();
ApplicationContext currentContext = applicationContext;
while (currentContext != null) {
T processingResult = contextProcessor.apply(currentContext);
currentContext = currentContext.getParent();
result.add(processingResult);
}
return result;
}
示例6: toLabeledReference
import java.util.function.Function; //导入方法依赖的package包/类
public static <T extends Enum> LabeledReference toLabeledReference(T obj, Function<T, String> nameMapper) {
if (obj != null) {
return new LabeledReference(obj.toString(), nameMapper.apply(obj));
} else {
return null;
}
}
示例7: leave
import java.util.function.Function; //导入方法依赖的package包/类
static Map<Object,Object> leave(IInterceptor interceptor, Map<Object,Object> context) {
Function<Map<Object,Object>,Map<Object,Object>> lFn = (interceptor != null) ? interceptor.getLeave() : null;
if (lFn != null) {
return lFn.apply(context);
} else {
return context;
}
}
示例8: applyCompareAndSay
import java.util.function.Function; //导入方法依赖的package包/类
private static Supplier<String> applyCompareAndSay(int i, Function<Integer, Double> func, Predicate<Double> compare, String message){
Supplier<String> supplier = new Supplier<String>() {
public String get() {
double v = func.apply(i);
return (compare.test(v)? v + " is " : v + " is not ") + message;
}
};
return supplier;
}
示例9: computeModuleIfAbsent
import java.util.function.Function; //导入方法依赖的package包/类
@CheckForNull
private String computeModuleIfAbsent(
@NonNull FileObject root,
@NonNull final Function<FileObject,String> provider) {
final Pair<Pair<Reference<FileObject>,File>,String> entry = modNameCache.get();
FileObject owner;
String modName;
if (entry == null ||
(owner = entry.first().first().get()) == null ||
!owner.equals(root)) {
modName = provider.apply(root);
final File modInfo = Optional.ofNullable(FileUtil.toFile(root))
.map((rf) -> new File(rf, String.format("%s.%s", FileObjects.MODULE_INFO, FileObjects.JAVA))) //NOI18N
.orElse(null);
final Pair<Pair<Reference<FileObject>,File>,String> newEntry = Pair.of(
Pair.of(new WeakReference<>(root), modInfo),
modName);
if (modNameCache.compareAndSet(entry, newEntry)) {
if (entry != null && entry.first().second() != null) {
FileUtil.removeFileChangeListener(this, entry.first().second());
}
if (newEntry.first().second() != null) {
FileUtil.addFileChangeListener(this, newEntry.first().second());
}
}
LOG.log(Level.FINE, "modNameCache updated: {0}", modName); //NOI18N
} else {
modName = entry.second();
}
return modName;
}
示例10: tryGetValue
import java.util.function.Function; //导入方法依赖的package包/类
private<T> T tryGetValue(Function<Double, T> cast) {
if (excelCell != null) {
if (excelCell.getCellTypeEnum() == CellType.FORMULA) {
excelCell.setCellType(CellType.NUMERIC);
}
if(excelCell.getCellTypeEnum() == CellType.NUMERIC) {
return cast.apply(excelCell.getNumericCellValue());
}
}
return null;
}
示例11: filterNumbersWithFunction
import java.util.function.Function; //导入方法依赖的package包/类
public List<Integer> filterNumbersWithFunction(final List<Integer> numbers,
Function<Integer, Boolean> function) {
List<Integer> filteredNumbers = new ArrayList<>();
for (Integer number : numbers) {
if (function.apply(number)) {
filteredNumbers.add(number);
}
}
return filteredNumbers;
}
示例12: convertBday
import java.util.function.Function; //导入方法依赖的package包/类
public Date convertBday(long bdayLong){
Function<Long, Date> bday = Date::new;
return bday.apply(bdayLong);
}
示例13: map
import java.util.function.Function; //导入方法依赖的package包/类
public <V1> Tuple1<V1> map(Function<Tuple1<T1>, Tuple1<V1>> function) {
return function.apply(this);
}
示例14: generateEvents
import java.util.function.Function; //导入方法依赖的package包/类
private static void generateEvents(String brokers, String topicName, String eventType, long numEvents)
throws UnsupportedEncodingException, InterruptedException {
Properties props = new Properties();
props.put("bootstrap.servers", brokers);
props.put("retries", 100);
props.put("batch.size", 16384);
props.put("key.serializer", ByteArraySerializer.class.getCanonicalName());
props.put("value.serializer", ByteArraySerializer.class.getCanonicalName());
Function<Integer, Pair<String, byte[]>> eventGenerator;
if (eventType.toLowerCase().contains(PAGEVIEW_EVENTTYPE)) {
eventGenerator = GenerateKafkaEvents::generatePageViewEvent;
} else {
eventGenerator = GenerateKafkaEvents::generateProfileChangeEvent;
}
boolean doSleep = false;
// sleep only when the events have to be produced continuously.
if (numEvents == Long.MAX_VALUE) {
doSleep = true;
}
try (Producer<byte[], byte[]> producer = new KafkaProducer<>(props)) {
for (int index = 0; index < numEvents; index++) {
final int finalIndex = 0;
Pair<String, byte[]> record = eventGenerator.apply(index);
producer.send(new ProducerRecord<>(topicName, record.getLeft().getBytes("UTF-8"), record.getRight()),
(metadata, exception) -> {
if (exception == null) {
LOG.info("send completed for event {} at offset {}", finalIndex, metadata.offset());
} else {
throw new RuntimeException("Failed to send message.", exception);
}
});
System.out.println(String.format("Published event %d to topic %s", index, topicName));
if (doSleep) {
Thread.sleep(1000);
}
}
producer.flush();
}
}
示例15: andThen
import java.util.function.Function; //导入方法依赖的package包/类
default <V> TripleFunction<A, B, C, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (a, b, c) -> after.apply(apply(a, b, c));
}