当前位置: 首页>>代码示例>>Java>>正文


Java Function类代码示例

本文整理汇总了Java中com.annimon.stream.function.Function的典型用法代码示例。如果您正苦于以下问题:Java Function类的具体用法?Java Function怎么用?Java Function使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Function类属于com.annimon.stream.function包,在下文中一共展示了Function类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: ExposedMethod

import com.annimon.stream.function.Function; //导入依赖的package包/类
public ExposedMethod(Element element) {
    ExecutableType method = (ExecutableType) element.asType();
    TypeElement declaringClass = (TypeElement) element.getEnclosingElement();
    this.name = element.getSimpleName().toString();
    this.originalMethod = declaringClass.getQualifiedName().toString() + "." + element.getSimpleName();
    this.returnType = method.getReturnType().toString();
    this.params = new ArrayList<>();
    int count = 0;
    for (TypeMirror param : method.getParameterTypes()) {
        this.params.add(param.toString());
        String[] components = param.toString().toLowerCase().split("\\.");
        String paramName = components[components.length - 1];
        if (paramName.endsWith(">")) {
            paramName = paramName.substring(0, paramName.length() - 1);
        }
        this.params.add(paramName + count);
        count++;
    }
    this.thrown = Stream.of(method.getThrownTypes()).map(new Function<TypeMirror, String>() {
        @Override
        public String apply(TypeMirror typeMirror) {
            return typeMirror.toString();
        }
    }).toList();
}
 
开发者ID:globulus,项目名称:easyprefs,代码行数:26,代码来源:ExposedMethod.java

示例2: comparing

import com.annimon.stream.function.Function; //导入依赖的package包/类
/**
 * Returns a comparator that uses a function that extracts a sort key
 * to be compared with the specified comparator.
 *
 * @param <T> the type of the objects compared by the comparator
 * @param <U> the type of the sort key
 * @param keyExtractor  the function that extracts the sort key
 * @param keyComparator  the comparator used to compare the sort key
 * @return a comparator
 * @throws NullPointerException if {@code keyExtractor} or {@code keyComparator} is null
 */
public static <T, U> ComparatorCompat<T> comparing(
        final Function<? super T, ? extends U> keyExtractor,
        final Comparator<? super U> keyComparator) {
    Objects.requireNonNull(keyExtractor);
    Objects.requireNonNull(keyComparator);
    return new ComparatorCompat<T>(new Comparator<T>() {

        @Override
        public int compare(T t1, T t2) {
            final U u1 = keyExtractor.apply(t1);
            final U u2 = keyExtractor.apply(t2);
            return keyComparator.compare(u1, u2);
        }
    });
}
 
开发者ID:aNNiMON,项目名称:Lightweight-Stream-API,代码行数:27,代码来源:ComparatorCompat.java

示例3: testCustomIntermediateOperator_FlatMapAndCast

import com.annimon.stream.function.Function; //导入依赖的package包/类
@Test
public void testCustomIntermediateOperator_FlatMapAndCast() {
    List<List> lists = new ArrayList<List>();
    for (char ch = 'a'; ch <= 'f'; ch++) {
        lists.add( new ArrayList<Character>(Arrays.asList(ch)) );
    }
    Stream.of(lists)
            .custom(new CustomOperators.FlatMap<List, Object>(new Function<List, Stream<Object>>() {
                @SuppressWarnings("unchecked")
                @Override
                public Stream<Object> apply(List value) {
                    return Stream.of(value);
                }
            }))
            .custom(new CustomOperators.Cast<Object, Character>(Character.class))
            .custom(assertElements(contains('a', 'b', 'c', 'd', 'e', 'f')));
}
 
开发者ID:aNNiMON,项目名称:Lightweight-Stream-API,代码行数:18,代码来源:CustomTest.java

示例4: testMapStringToSquareInt

import com.annimon.stream.function.Function; //导入依赖的package包/类
@Test
public void testMapStringToSquareInt() {
    final Function<String, Integer> stringToSquareInt = new Function<String, Integer>() {
        @Override
        public Integer apply(String t) {
            final String str = t.substring(1, t.length() - 1);
            final int value = Integer.parseInt(str);
            return value * value;
        }
    };
    Stream.of("[2]", "[3]", "[4]", "[8]", "[25]")
            .map(stringToSquareInt)
            .custom(assertElements(contains(
                  4, 9, 16, 64, 625
            )));
}
 
开发者ID:aNNiMON,项目名称:Lightweight-Stream-API,代码行数:17,代码来源:MapTest.java

示例5: testMapWithComposedFunction

import com.annimon.stream.function.Function; //导入依赖的package包/类
@Test
public void testMapWithComposedFunction() {
    final Function<Integer, Integer> mapPlus1 = new Function<Integer, Integer>() {
        @Override
        public Integer apply(Integer x) {
            return x + 1;
        }
    };
    final Function<Integer, Integer> mapPlus2 = Function.Util
            .compose(mapPlus1, mapPlus1);
    Stream.range(-10, 0)
            .map(mapPlus2)
            .map(mapPlus2)
            .map(mapPlus2)
            .map(mapPlus2)
            .map(mapPlus2)
            .custom(assertElements(contains(
                  0, 1, 2, 3, 4, 5, 6, 7, 8, 9
            )));
}
 
开发者ID:aNNiMON,项目名称:Lightweight-Stream-API,代码行数:21,代码来源:MapTest.java

示例6: testOnCloseFlatMap

import com.annimon.stream.function.Function; //导入依赖的package包/类
@Test
public void testOnCloseFlatMap() {
    final int[] counter = new int[] { 0 };
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            counter[0]++;
        }
    };

    Stream<Integer> stream = Stream.rangeClosed(2, 4)
            .onClose(runnable)
            .onClose(runnable)
            .flatMap(new Function<Integer, Stream<Integer>>() {
                @Override
                public Stream<Integer> apply(final Integer i) {
                    return Stream.rangeClosed(2, 4)
                            .onClose(runnable);
                }
            });
    stream.count();
    assertThat(counter[0], is(3));
    stream.close();
    assertThat(counter[0], is(5));
}
 
开发者ID:aNNiMON,项目名称:Lightweight-Stream-API,代码行数:26,代码来源:OnCloseTest.java

示例7: testFlatMapToLong

import com.annimon.stream.function.Function; //导入依赖的package包/类
@Test
public void testFlatMapToLong() {
    Stream.rangeClosed(2L, 4L)
            .flatMapToLong(new Function<Long, LongStream>() {
                @Override
                public LongStream apply(Long t) {
                    return LongStream
                            .iterate(t, LongUnaryOperator.Util.identity())
                            .limit(t);
                }
            })
            .custom(assertElements(arrayContaining(
                    2L, 2L,
                    3L, 3L, 3L,
                    4L, 4L, 4L, 4L
            )));
}
 
开发者ID:aNNiMON,项目名称:Lightweight-Stream-API,代码行数:18,代码来源:FlatMapToLongTest.java

示例8: testFlatMapToInt

import com.annimon.stream.function.Function; //导入依赖的package包/类
@Test
public void testFlatMapToInt() {
    Stream.rangeClosed(2, 4)
            .flatMapToInt(new Function<Integer, IntStream>() {

                @Override
                public IntStream apply(Integer t) {
                    return IntStream
                            .iterate(t, IntUnaryOperator.Util.identity())
                            .limit(t);
                }
            })
            .custom(assertElements(arrayContaining(
                    2, 2,
                    3, 3, 3,
                    4, 4, 4, 4
            )));
}
 
开发者ID:aNNiMON,项目名称:Lightweight-Stream-API,代码行数:19,代码来源:FlatMapToIntTest.java

示例9: testSortByStudentName

import com.annimon.stream.function.Function; //导入依赖的package包/类
@Test
public void testSortByStudentName() {
    final List<Student> students = Arrays.asList(
            Students.STEVE_CS_4,
            Students.MARIA_ECONOMICS_1,
            Students.VICTORIA_CS_3,
            Students.JOHN_CS_2
    );
    Stream.of(students)
            .sortBy(new Function<Student, String>() {
                @Override
                public String apply(Student student) {
                    return student.getName();
                }
            })
            .custom(assertElements(contains(
                    Students.JOHN_CS_2,
                    Students.MARIA_ECONOMICS_1,
                    Students.STEVE_CS_4,
                    Students.VICTORIA_CS_3
            )));
}
 
开发者ID:aNNiMON,项目名称:Lightweight-Stream-API,代码行数:23,代码来源:SortByTest.java

示例10: testSortByStudentCourseDescending

import com.annimon.stream.function.Function; //导入依赖的package包/类
@Test
public void testSortByStudentCourseDescending() {
    final List<Student> students = Arrays.asList(
            Students.STEVE_CS_4,
            Students.MARIA_ECONOMICS_1,
            Students.VICTORIA_CS_3,
            Students.JOHN_CS_2
    );
    Stream.of(students)
            .sortBy(new Function<Student, Integer>() {
                @Override
                public Integer apply(Student student) {
                    return -student.getCourse();
                }
            })
            .custom(assertElements(contains(
                    Students.STEVE_CS_4,
                    Students.VICTORIA_CS_3,
                    Students.JOHN_CS_2,
                    Students.MARIA_ECONOMICS_1
            )));
}
 
开发者ID:aNNiMON,项目名称:Lightweight-Stream-API,代码行数:23,代码来源:SortByTest.java

示例11: testFlatMapToDouble

import com.annimon.stream.function.Function; //导入依赖的package包/类
@Test
@SuppressWarnings("unchecked")
public void testFlatMapToDouble() {
    Stream.of(2, 4)
            .flatMapToDouble(new Function<Integer, DoubleStream>() {
                @Override
                public DoubleStream apply(Integer t) {
                    return DoubleStream.of(t / 10d, t / 20d);
                }
            })
            .custom(assertElements(array(
                    closeTo(0.2, 0.0001),
                    closeTo(0.1, 0.0001),
                    closeTo(0.4, 0.0001),
                    closeTo(0.2, 0.0001)
            )));
}
 
开发者ID:aNNiMON,项目名称:Lightweight-Stream-API,代码行数:18,代码来源:FlatMapToDoubleTest.java

示例12: testStreamOfNullableMap

import com.annimon.stream.function.Function; //导入依赖的package包/类
@Test
public void testStreamOfNullableMap() {
    Map<Integer, String> t = null;
    assertThat(Stream.ofNullable(t), isEmpty());

    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    map.put(1, 2);
    map.put(3, 4);
    Stream.ofNullable(map)
            .flatMap(new Function<Map.Entry<Integer, Integer>, Stream<Integer>>() {
                @Override
                public Stream<Integer> apply(Map.Entry<Integer, Integer> e) {
                    return Stream.of(e.getKey(), e.getValue());
                }
            })
            .custom(assertElements(contains(1, 2, 3, 4)));
}
 
开发者ID:aNNiMON,项目名称:Lightweight-Stream-API,代码行数:18,代码来源:OfNullableTest.java

示例13: testCustomTerminal

import com.annimon.stream.function.Function; //导入依赖的package包/类
@Test
public void testCustomTerminal() {
    Function<Exceptional<Integer>, Integer> incrementer = new Function<Exceptional<Integer>, Integer>() {
        @Override
        public Integer apply(Exceptional<Integer> exceptional) {
            return exceptional.map(new ThrowableFunction<Integer, Integer, Throwable>() {
                @Override
                public Integer apply(Integer integer) throws Throwable {
                    return integer + 1;
                }
            }).getOrElse(0);
        }
    };
    int value;
    value = Exceptional.of(ioExceptionSupplier)
            .custom(incrementer);
    assertEquals(0, value);

    value = Exceptional.of(tenSupplier)
            .custom(incrementer);
    assertEquals(11, value);
}
 
开发者ID:aNNiMON,项目名称:Lightweight-Stream-API,代码行数:23,代码来源:ExceptionalTest.java

示例14: testToMapWithCustomValueMapper

import com.annimon.stream.function.Function; //导入依赖的package包/类
@Test
public void testToMapWithCustomValueMapper() {
    final Function<String, Character> keyMapper = Functions.firstCharacterExtractor();
    Map<Character, String> chars = Stream.of("a0", "b0", "c0", "d0")
            .collect(Collectors.toMap(keyMapper, new UnaryOperator<String>() {

        @Override
        public String apply(String value) {
            if ("c0".equals(value)) return null;
            return String.valueOf(Character.toUpperCase(value.charAt(0)));
        }
    }));

    assertThat(chars.size(), is(3));
    assertThat(chars, allOf(
            hasEntry('a', "A"),
            hasEntry('b', "B"),
            hasEntry('d', "D")
    ));
}
 
开发者ID:aNNiMON,项目名称:Lightweight-Stream-API,代码行数:21,代码来源:CollectorsTest.java

示例15: testAveraging

import com.annimon.stream.function.Function; //导入依赖的package包/类
@Test
@SuppressWarnings("deprecation")
public void testAveraging() {
    double avg;

    avg = Stream.<Integer>empty()
            .collect(Collectors.averaging(new Function<Integer, Double>() {
                @Override
                public Double apply(Integer t) {
                    return t.doubleValue();
                }
            }));
    assertThat(avg, closeTo(0, 0.001));

    avg = Stream.of(10, 20, 30, 40)
            .collect(Collectors.averaging(new Function<Integer, Double>() {
                @Override
                public Double apply(Integer value) {
                    return value / 10d;
                }
            }));
    assertThat(avg, closeTo(2.5, 0.001));
}
 
开发者ID:aNNiMON,项目名称:Lightweight-Stream-API,代码行数:24,代码来源:CollectorsTest.java


注:本文中的com.annimon.stream.function.Function类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。