Example usage for java.util.stream Collectors toSet

List of usage examples for java.util.stream Collectors toSet

Introduction

In this page you can find the example usage for java.util.stream Collectors toSet.

Prototype

public static <T> Collector<T, ?, Set<T>> toSet() 

Source Link

Document

Returns a Collector that accumulates the input elements into a new Set .

Usage

From source file:Main.java

public static void main(String[] args) {
    Stream<String> s = Stream.of("a", "b", "c");

    Set<String> names = s.collect(Collectors.toSet());

    System.out.println(names);/*w w  w  .  j av  a  2  s .  c o m*/
}

From source file:Main.java

public static void main(String[] args) {
    // collect as typed array
    Stream<String> words = Stream.of("All", "men", "are", "created", "equal");
    Set<String> wordsSet = words.collect(Collectors.toSet());
    wordsSet.forEach(n -> System.out.println(n));
}

From source file:Main.java

public static void main(String... args) {
    Map<Type, Set<CaloricLevel>> o = Food.menu.stream()
            .collect(Collectors.groupingBy(Food::getType, Collectors.mapping(dish -> {
                if (dish.getCalories() <= 400)
                    return CaloricLevel.DIET;
                else if (dish.getCalories() <= 700)
                    return CaloricLevel.NORMAL;
                else
                    return CaloricLevel.FAT;
            }, Collectors.toSet())));

    System.out.println(o);//from   w  w w.  java  2  s. c o  m

}

From source file:com.khartec.waltz.jobs.sample.UnknownFlowGenerator.java

public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);

    ApplicationService applicationDao = ctx.getBean(ApplicationService.class);
    DSLContext dsl = ctx.getBean(DSLContext.class);

    List<Application> allApps = applicationDao.findAll();

    Set<DataFlowRecord> records = IntStream.range(0, 2000)
            .mapToObj(i -> Tuple.tuple(randomPick(allApps).id().get(), randomPick(allApps).id().get()))
            .map(t -> new DataFlowRecord("APPLICATION", t.v1(), "APPLICATION", t.v2(), "UNKNOWN", "UNK_TEST"))
            .collect(Collectors.toSet());

    dsl.deleteFrom(DATA_FLOW).where(DATA_FLOW.PROVENANCE.eq("UNK_TEST")).execute();

    dsl.batchInsert(records).execute();/*from w  w w .  j a  va 2 s  .c o  m*/

    System.out.println("Done");
}

From source file:com.khartec.waltz.jobs.sample.AppCapabilityGenerator.java

public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);
    CapabilityService capabilityDao = ctx.getBean(CapabilityService.class);
    ApplicationService applicationDao = ctx.getBean(ApplicationService.class);
    AppCapabilityService appCapabilityDao = ctx.getBean(AppCapabilityService.class);

    DSLContext dsl = ctx.getBean(DSLContext.class);

    dsl.delete(APP_CAPABILITY).execute();

    List<Capability> capabilities = capabilityDao.findAll();

    applicationDao.findAll().forEach(app -> {
        int count = rnd.nextInt(4) + 1;

        Set<Long> ids = IntStream.range(0, count).mapToObj(i -> randomPick(capabilities)).map(c -> c.id().get())
                .collect(Collectors.toSet());

        appCapabilityDao.addCapabilitiesToApp(app.id().get(), new ArrayList<>(ids));

    });//  w  ww  . j  av a 2 s  .com

}

From source file:com.khartec.waltz.jobs.sample.FlowGenerator.java

public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);

    AuthoritativeSourceDao authSourceDao = ctx.getBean(AuthoritativeSourceDao.class);
    ApplicationService applicationDao = ctx.getBean(ApplicationService.class);
    DataTypeService dataTypesDao = ctx.getBean(DataTypeService.class);
    DataFlowService dataFlowDao = ctx.getBean(DataFlowService.class);
    OrganisationalUnitService orgUnitDao = ctx.getBean(OrganisationalUnitService.class);
    DataSource dataSource = ctx.getBean(DataSource.class);
    DSLContext dsl = ctx.getBean(DSLContext.class);

    List<AuthoritativeSource> authSources = authSourceDao.findByEntityKind(EntityKind.ORG_UNIT);
    List<String> dataTypes = dataTypesDao.getAll().stream().map(dt -> dt.code()).collect(toList());
    List<Application> apps = applicationDao.findAll();
    List<OrganisationalUnit> orgUnits = orgUnitDao.findAll();

    Set<DataFlow> expectedFlows = authSources.stream().flatMap(a -> {
        long orgUnitId = a.parentReference().id();

        return IntStream.range(0, rnd.nextInt(40))
                .mapToObj(i -> ImmutableDataFlow.builder().dataType(a.dataType())
                        .source(a.applicationReference()).target(randomAppPick(apps, orgUnitId)).build());
    }).collect(Collectors.toSet());

    Set<DataFlow> probableFlows = authSources.stream().flatMap(a -> IntStream.range(0, rnd.nextInt(30))
            .mapToObj(i -> ImmutableDataFlow.builder().dataType(a.dataType()).source(a.applicationReference())
                    .target(randomAppPick(apps, randomPick(orgUnits).id().get())).build()))
            .collect(Collectors.toSet());

    Set<DataFlow> randomFlows = apps.stream()
            .map(a -> ImmutableDataFlow.builder().source(a.toEntityReference()))
            .map(b -> b.target(randomAppPick(apps, randomPick(orgUnits).id().get())))
            .map(b -> b.dataType(randomPick(dataTypes)).build()).collect(Collectors.toSet());

    dsl.deleteFrom(DATA_FLOW).execute();

    Set<DataFlow> all = new HashSet<>();
    all.addAll(randomFlows);//from   w  w w. j  av a 2 s  .  co m
    all.addAll(expectedFlows);
    all.addAll(probableFlows);

    dataFlowDao.addFlows(new ArrayList<>(all));

    System.out.println("Done");

}

From source file:Main.java

@SafeVarargs
public static <T> Set<T> setOf(T... values) {
    return unmodifiableSet(Stream.of(values).collect(Collectors.toSet()));
}

From source file:Main.java

@SafeVarargs
public static <T> Set<T> setOfNotNull(T... objects) {
    return Arrays.stream(objects).filter(obj -> obj != null).collect(Collectors.toSet());
}

From source file:Main.java

@SafeVarargs
public static <T> Set<T> unionArraysAsSet(T[]... arrays) {
    return Stream.of(arrays).map(Arrays::asList).flatMap(Collection::stream).collect(Collectors.toSet());
}

From source file:Main.java

public static <T> Set<String> toSet(List<T> list, Function<T, String> getId) {
    return stream(list).map(getId).collect(Collectors.toSet());
}