DEV Community

Steve Mak
Steve Mak

Posted on

Stream

Convert n elements to an array list

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
Enter fullscreen mode Exit fullscreen mode

Stream

Stream map

List<String> listOfStrings = Arrays.asList("1", "2", "3", "4", "5");

List<Integer> listOfIntegers = listOfStrings.stream()
        .map(Integer::valueOf)
        .collect(Collectors.toList());

System.out.println(listOfIntegers);
Enter fullscreen mode Exit fullscreen mode

Stream reduce

//
// Example 1
//
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
int result = numbers
  .stream()
  .reduce(0, (subtotal, element) -> subtotal + element);
assertThat(result).isEqualTo(21);

//
// Example 2 (Use method reference)
//
int result = numbers.stream().reduce(0, Integer::sum);
assertThat(result).isEqualTo(21);

//
// Example 3
//
List<User> users = Arrays.asList(new User("John", 30), new User("Julie", 35));
int result = users.stream()
  .reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
assertThat(result).isEqualTo(65);
Enter fullscreen mode Exit fullscreen mode

Stream.flatMap

//
// Example 1
//
Merging Lists into a Single List
List<Integer> list1 = Arrays.asList(1,2,3);
List<Integer> list2 = Arrays.asList(4,5,6);
List<Integer> list3 = Arrays.asList(7,8,9);

List<List<Integer>> listOfLists = Arrays.asList(list1, list2, list3);

List<Integer> listOfAllIntegers = listOfLists.stream()
          .flatMap(x -> x.stream())
          .collect(Collectors.toList());

System.out.println(listOfAllIntegers);

//
// Example 2
//
String[][] dataArray = new String[][]{{"a", "b"}, 
        {"c", "d"}, {"e", "f"}, {"g", "h"}};

List<String> listOfAllChars = Arrays.stream(dataArray)
              .flatMap(x -> Arrays.stream(x))
              .collect(Collectors.toList());

System.out.println(listOfAllChars);
Enter fullscreen mode Exit fullscreen mode

IntStream

// Converting IntStream to Array
int[] intArray = IntStream.of(1, 2, 3, 4, 5).toArray();

// Converting IntStream to List
List<Integer> list = IntStream.of(1,2,3,4,5)
            .boxed()
            .collect(Collectors.toList());

// Returns stream containing a single specified element.
IntStream.of(10); // 10

// Returns stream containing specified all elements.
IntStream.of(1, 2, 3); // 1,2,3

// Returns a sequential ordered int stream excludes the last element.
IntStream.range(1, 5); // 1,2,3,4

// Returns a sequential ordered int stream includes the last element.
IntStream.rangeClosed(1, 5); // 1,2,3,4,5
Enter fullscreen mode Exit fullscreen mode

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay