x Java Java 8 JUnit JSON
  • XML
  • JDBC Spring Boot Microservices React Contact Us

    Java Stream flatmap examples

    1) Java 8 Stream flatmap example to convert two dimensional array of String to stream of Strings.

    It converts {{"Mike", "Micky"}, {"Pearl", "John"}, {"James", "Emma"}} to {"Mike", "Micky", "Pearl", "John","James", "Emma"}

    // Convert two dimensional array of String to stream of Strings using flatmap
    import java.util.Arrays;
    import java.util.stream.Stream;
        
    public class StreamFlatMap { 
    
        public static void main(String[] args) {
            String[][] strs = new String[][]{{"Mike", "Micky"}, {"Pearl", "John"},
                {"James", "Emma"}};
    
            Stream<String[]> stream1 = Arrays.stream(strs);
            Stream<String[]> stream = stream1.filter(x -> "Micky".equals(x.toString()));
            stream.forEach(System.out::println);  // prints nothing 
            
            Stream<String[]> stream2 = Arrays.stream(strs);
            Stream<String> stringStream = stream2.flatMap(x -> Arrays.stream(x));
            stringStream.forEach(System.out::println);
            
            Stream<String[]> stream3 = Arrays.stream(strs);
            stringStream = stream3.flatMap(x -> Arrays.stream(x));
            stringStream = stringStream.filter(x -> "Micky".equals(x.toString()));  
            
            stringStream.forEach(System.out::println);    // prints Micky
        }
    }	
    	

    Output :

    Mike
    Micky
    Pearl
    John
    James
    Emma
    Micky
    

    2) Java 8 Stream flatmap example to get list of all states for two countries

    Suppose you have two Country objects and each one has list of states, now you want to get list of all the states that belong to all the countries, you can do it using Stream flatmap as shown below:

    import java.util.Set;    
    public class Country {
    
        private String name;
        private Set<String> states;
    
        public Country(String name, Set<String> states) {
            this.states = states;
            this.name = name;
        }
    
        public Set<String> getStates() {
            return this.states;
        }
    }
    
        
    // Stream flatmap example
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
        
    public class StreamFlatMap { 
    
        public static void main(String[] args) {
            
            Set<String> russianStates = new HashSet<String>();
            russianStates.add("rs1");
            russianStates.add("rs2");
            
            Country russia = new Country("Russia", russianStates);
            
            Set<String> germanStates = new HashSet<String>();
            germanStates.add("gs1");
            germanStates.add("gs2");
            
            Country germany = new Country("Germany", germanStates);
            
            List<Country> countries = new ArrayList<Country>();
            countries.add(russia);
            countries.add(germany);
            
            Stream<Country> collect =   countries.stream();
            Stream<Set<String>> streamSet = collect.map(c -> c.getStates());
            Stream<Object> streamObject = streamSet.flatMap(x -> x.stream());
            List<Object> allStates = streamObject.collect(Collectors.toList());
            
            // prints all the states for all the countries
            allStates.forEach(System.out::println);  
        }
    }	
    	

    Output :

    rs2
    rs1
    gs1
    gs2
    

    3) Java 8 Stream flatMapToInt example to convert list of String to stream of int.

    flatMapToInt() method is defined in Stream interface. It has following signature

    IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper)

    Below example converts {"1", "2", "3", "4", "5", "6"} to {1, 2, 3, 4, 5, 6}

    // Convert two dimensional array of int to stream of int using flatMapToInt
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.IntStream;
    import java.util.stream.Stream;
        
    public class StreamFlatMapToInt { 
    
        public static void main(String[] args) {
            List<String> list = Arrays.asList("1","2","3","4","5","6");
            Stream<String> stream = list.stream();
            IntStream intStream =  stream.flatMapToInt(n-> IntStream.of(Integer.parseInt(n)) );
            intStream.forEach(System.out::println);
         
        }
    }	
    	

    Output :

    1
    2
    3
    4
    5
    6
    

    References :

    Oracle Docs Stream flatmap

    Oracle Docs Function

    Oracle Docs flatMapToInt