Example usage for java.util.stream Collectors toCollection

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

Introduction

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

Prototype

public static <T, C extends Collection<T>> Collector<T, ?, C> toCollection(Supplier<C> collectionFactory) 

Source Link

Document

Returns a Collector that accumulates the input elements into a new Collection , in encounter order.

Usage

From source file:info.rmarcus.birkhoffvonneumann.MatrixUtils.java

public static int[] randomPermutationSparse(Random r, int n) {
    List<Integer> s = IntStream.range(0, n).mapToObj(i -> i)
            .collect(Collectors.toCollection(() -> new ArrayList<Integer>(n)));
    Collections.shuffle(s, r);//  w  w  w. ja v a2 s  . c  o m
    return NullUtils.orThrow(s.stream().mapToInt(i -> i).toArray(),
            () -> new BVNRuntimeException("Could not convert ArrayList to array!"));
}

From source file:org.sonar.java.bytecode.cfg.BytecodeCFGBuilderTest.java

@Test
public void test_all_instructions_are_part_of_CFG() throws Exception {
    SquidClassLoader squidClassLoader = new SquidClassLoader(
            Lists.newArrayList(new File("target/test-classes"), new File("target/classes")));
    File file = new File("src/test/java/org/sonar/java/bytecode/cfg/testdata/CFGTestData.java");
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
    SemanticModel.createFor(tree, squidClassLoader);
    Symbol.TypeSymbol testClazz = ((ClassTree) tree.types().get(0)).symbol();
    ClassReader cr = new ClassReader(squidClassLoader
            .getResourceAsStream(Convert.bytecodeName(CFGTestData.class.getCanonicalName()) + ".class"));
    ClassNode classNode = new ClassNode(BytecodeCompleter.ASM_API_VERSION);
    cr.accept(classNode, 0);/*from   w  ww .  j  a  va 2 s.  c o  m*/
    for (MethodNode method : classNode.methods) {
        Multiset<String> opcodes = Arrays.stream(method.instructions.toArray()).map(AbstractInsnNode::getOpcode)
                .filter(opcode -> opcode != -1).map(opcode -> Printer.OPCODES[opcode])
                .collect(Collectors.toCollection(HashMultiset::create));

        Symbol methodSymbol = Iterables.getOnlyElement(testClazz.lookupSymbols(method.name));
        BytecodeCFG bytecodeCFG = SETestUtils.bytecodeCFG(((Symbol.MethodSymbol) methodSymbol).signature(),
                squidClassLoader);
        Multiset<String> cfgOpcodes = cfgOpcodes(bytecodeCFG);
        assertThat(cfgOpcodes).isEqualTo(opcodes);
    }
}

From source file:io.syndesis.rest.v1.state.ClientSideState.java

public <T> Set<T> restoreFrom(final Collection<Cookie> cookies, final Class<T> type) {
    return cookies.stream().flatMap(c -> {
        try {/*w  ww. ja va 2  s  .  c  o m*/
            return Stream.of(restoreWithTimestamp(c, type));
        } catch (final IllegalArgumentException e) {
            LOG.warn("Unable to restore client side state from cookie: {}", c, e);

            return Stream.empty();
        }
    }).sorted().map(t -> t.state).collect(Collectors.toCollection(LinkedHashSet::new));
}

From source file:com.evolveum.midpoint.common.refinery.RefinedObjectClassDefinitionImpl.java

@Override
public Collection<? extends QName> getNamesOfAttributesWithInboundExpressions() {
    return getAttributeDefinitions().stream()
            .filter(attrDef -> CollectionUtils.isNotEmpty(attrDef.getInboundMappingTypes()))
            .map(attrDef -> attrDef.getName()).collect(Collectors.toCollection(HashSet::new));
}

From source file:org.codice.alliance.catalog.transformer.mgmp.MgmpTransformer.java

@Override
public LinkedHashSet<Path> buildPaths() {
    LinkedHashSet<Path> paths = Stream.of(MgmpConstants.RESOURCE_ORIGINATOR_SECURITY_PATH,
            MgmpConstants.RESOURCE_SECURITY_RELEASABILITY_PATH, MgmpConstants.LANGUAGE_PATH,
            MgmpConstants.CLOUD_COVERAGE_PATH, MgmpConstants.FORMAT_PATH, GmdConstants.FORMAT_VERSION_PATH,

            MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_NUMBER_PATH,
            MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_TYPE_PATH,
            MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_DATUM_TYPE_PATH,
            MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_DATUM_NUMBER_TYPE_PATH,
            MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_ELLIPSOID_TYPE_PATH,
            MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_ELLIPSOID_NUMBER_TYPE_PATH,
            MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_PROJECTION_TYPE_PATH,
            MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_PROJECTION_NUMBER_TYPE_PATH,
            MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_GRID_TYPE_PATH,
            MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_GRID_NUMBER_TYPE_PATH,
            MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_GRID_WKT_TYPE_PATH,
            MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_PROJECTION_WKT_TYPE_PATH,
            MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_ELLIPSOID_WKT_TYPE_PATH,
            MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_DATUM_WKT_TYPE_PATH,

            MgmpConstants.ISR_COVERAGE_CATEGORY_PATH, MgmpConstants.ISR_COVERAGE_COMMENT_PATH,
            MgmpConstants.ISR_IMAGE_COMMENT_PATH, MgmpConstants.ISR_IMAGE_DESCRIPTION_PATH,
            MgmpConstants.ISR_MD_IMAGE_COMMENT_PATH, MgmpConstants.ISR_MD_IMAGE_DESCRIPTION_PATH,
            MgmpConstants.ISR_VIDEO_COMMENT_PATH, MgmpConstants.ISR_VIDEO_DESCRIPTION_PATH,

            MgmpConstants.METADATA_ORIGINATOR_SECURITY_PATH, MgmpConstants.METADATA_RELEASABILITY_PATH,
            MgmpConstants.RESOURCE_SECURITY_RELEASABILITY_PATH, MgmpConstants.NIIRS_PATH,
            MgmpConstants.NIIRS_RATING_PATH, MgmpConstants.RESOURCE_SECURITY_PATH,
            MgmpConstants.METADATA_SECURITY_PATH, MgmpConstants.MGMP_SPATIAL_REFERENCE_SYSTEM_CRS_WKT_TYPE_PATH)
            .map(this::toPath).collect(Collectors.toCollection(LinkedHashSet::new));

    paths.addAll(super.buildPaths());
    return paths;
}

From source file:com.olacabs.fabric.compute.builder.impl.JarScanner.java

private List<ScanResult> scanForSources(ClassLoader classLoader, URL[] downloadedUrls) throws Exception {
    Reflections reflections = new Reflections(new ConfigurationBuilder().addClassLoader(classLoader)
            .addScanners(new SubTypesScanner(), new TypeAnnotationsScanner()).addUrls(downloadedUrls));
    Set<Class<?>> sources = Sets.intersection(reflections.getTypesAnnotatedWith(Source.class),
            reflections.getSubTypesOf(PipelineSource.class));
    return sources.stream().map(source -> {
        Source sourceInfo = source.getAnnotation(Source.class);
        ComponentMetadata metadata = ComponentMetadata.builder().type(ComponentType.SOURCE)
                .namespace(sourceInfo.namespace()).name(sourceInfo.name()).version(sourceInfo.version())
                .description(sourceInfo.description()).cpu(sourceInfo.cpu()).memory(sourceInfo.memory())
                .requiredProperties(ImmutableList.copyOf(sourceInfo.requiredProperties()))
                .optionalProperties(ImmutableList.copyOf(sourceInfo.optionalProperties())).build();

        return ScanResult.builder().metadata(metadata).componentClass(source).build();

    }).collect(Collectors.toCollection(ArrayList::new));
}

From source file:mesclasses.handlers.ModelHandler.java

public ObservableList<Cours> getCoursForDayAndClasse(String day, Classe classe) {
    return data.getCours().stream()
            .filter(c -> c.getDay().equals(day) && c.getClasse().getName().equals(classe.getName()))
            .collect(Collectors.toCollection(FXCollections::observableArrayList));
}

From source file:nova.core.wrapper.mc.forge.v18.wrapper.block.forward.FWBlock.java

@Override
public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
    Block blockInstance;/*from  ww  w .  j  a v  a  2  s.com*/

    // see onBlockHarvested for why the harvestedBlocks hack exists
    // this method will be called exactly once after destroying the block
    BlockPosition position = new BlockPosition((World) world, pos.getX(), pos.getY(), pos.getZ());
    if (harvestedBlocks.containsKey(position)) {
        blockInstance = harvestedBlocks.remove(position);
    } else {
        blockInstance = getBlockInstance(world, new Vector3D(pos.getX(), pos.getY(), pos.getZ()));
    }

    Block.DropEvent event = new Block.DropEvent(blockInstance);
    blockInstance.events.publish(event);

    return new ArrayList<>(event.drops.stream().map(item -> (ItemStack) Game.natives().toNative(item))
            .collect(Collectors.toCollection(ArrayList::new)));
}

From source file:org.pentaho.hadoop.PropertiesConfigurationPropertiesTest.java

@Test
public void testPropertyNames() {
    HashSet<String> names = new HashSet<>(Arrays.asList("a", "b", "c"));
    mockKeys(names);//w ww . j  a va  2 s.c om

    HashSet<String> propertyNames = Collections.list(propertiesConfigurationProperties.propertyNames()).stream()
            .map(Object::toString).collect(Collectors.toCollection(HashSet::new));
    assertEquals(names, propertyNames);
}

From source file:org.obiba.mica.web.model.StudyDtos.java

@NotNull
BaseStudy fromDto(@NotNull Mica.StudyDtoOrBuilder dto) {
    Assert.isTrue(//from  ww  w  .j  a  va 2 s.c o  m
            dto.hasExtension(Mica.CollectionStudyDto.type) || dto.hasExtension(Mica.HarmonizationStudyDto.type),
            "StudyDto must of a type extension.");

    BaseStudy study = dto.hasExtension(Mica.CollectionStudyDto.type) ? new Study() : new HarmonizationStudy();

    if (dto.hasId())
        study.setId(dto.getId());
    if (dto.getNameCount() > 0)
        study.setName(localizedStringDtos.fromDto(dto.getNameList()));
    if (dto.getAcronymCount() > 0)
        study.setAcronym(localizedStringDtos.fromDto(dto.getAcronymList()));
    if (dto.hasLogo())
        study.setLogo(attachmentDtos.fromDto(dto.getLogo()));
    if (dto.hasTimestamps())
        TimestampsDtos.fromDto(dto.getTimestamps(), study);
    if (dto.getObjectivesCount() > 0)
        study.setObjectives(localizedStringDtos.fromDto(dto.getObjectivesList()));
    if (dto.hasOpal())
        study.setOpal(dto.getOpal());

    if (dto.getMembershipsCount() > 0) {
        Map<String, List<Membership>> memberships = Maps.newHashMap();
        dto.getMembershipsList().forEach(e -> memberships.put(e.getRole(), e.getMembersList().stream()
                .map(p -> new Membership(personDtos.fromDto(p), e.getRole())).collect(toList())));
        study.setMemberships(memberships);
    }

    if (dto.getPopulationsCount() > 0) {
        study.setPopulations(dto.getPopulationsList().stream().map(populationDtos::fromDto)
                .collect(Collectors.toCollection(TreeSet<org.obiba.mica.study.domain.Population>::new)));
    }

    if (dto.hasContent() && !Strings.isNullOrEmpty(dto.getContent()))
        study.setModel(JSONUtils.toMap(dto.getContent()));
    else
        study.setModel(new HashMap<>());

    return study;
}