Linked Questions

Popular Questions

We are iterating the LinkedList using Streams in java8, and creating another list. But due the race condition the result List size got alatered.

List<DesInfo> InfoList = new LinkedList<>();
documentList.stream()
            .parallel()
            .forEach(document -> {
                Info descriptiveInfo = objectFactory.createDescriptiveInfo();
                List<Document> documents = new ArrayList<>();
                documents.add(document);
                descriptiveInfo.setHotelInfo(getHotelInfo(document.get(CODE), documents));
                InfoList.add(Info);
            });

If we runt this code everytime we are seeing different size of InfoList got generated for the same documentList input. I gone through some forums and they mention like LinkedList is not thread-safe and to go for any thread-safe collection instead. But my requirement not allows me to change apart from LinkedList. I need to use parallel streams functionality with the same LinkedList.

Thanks, Raghavan

Related Questions