modepaster.blogg.se

Streams in java
Streams in java









streams in java
  1. STREAMS IN JAVA HOW TO
  2. STREAMS IN JAVA CODE

Starting from the top, the first thing you may see and scratch your head over is the Optional keyword. Obviously, there's a LOT going on inside of this code, but that's why I'm here to try my best to explain what's going on. collect(oupingBy(Player::getState,Ĭollectors.maxBy(paring(Player::getHighScore)))) Private static Collection> getHighScoresWithStreams()

STREAMS IN JAVA CODE

This method can be called from the main method in our code example above. What I'm going to show you is the one method that will do the work we need to return a list of Players. Thankfully the alternative is stupidly simple.įirst let's have a look at what the code would be to get our desired output using Streams, then I'll explain it. Now that's a CRAP load of code just to get this output… but without the use of Streams and Lambdas in Java 8, this is more or less what you would have to do to get the correct output. Name=Bat Man, state=Washington, highScore=3205] Name=John Doe, state=Arizona, highScore=5048, Name=Sansa Stark, state=California, highScore=7220, [name=Daenerys Targaryen, state=Colorado, highScore=10000, The output of all that code above will be this list of Player high scores by state: Private static Player createPlayer (Long id, String name, Integer highScore, String state) Players.add(createPlayer(10L, "Tyrion Lannister", 4680, "California")) Players.add(createPlayer(9L, "Sansa Stark", 7220, "California")) Players.add(createPlayer(8L, "Arya Stark", 6050, "Colorado")) Players.add(createPlayer(7L, "John Snow", 9800, "Colorado")) Players.add(createPlayer(6L, "Daenerys Targaryen", 10000, "Colorado")) Players.add(createPlayer(5L, "Frodo Baggins", 100, "Washington"))

streams in java

Players.add(createPlayer(4L, "Bat Man", 3205, "Washington")) Players.add(createPlayer(3L, "Super Man", 1450, "Washington")) Players.add(createPlayer(2L, "Jane Doe", 2400, "Arizona")) Players.add(createPlayer(1L, "John Doe", 5048, "Arizona")) Private static void populatePlayerData () Return player2.getHighScore().compareTo(player1.getHighScore()) Step 2 & 3: Sort grouped Player data by high score and return the highest scoreįor (Map.Entry> entry : groupedPlayerDataByState.entrySet())Ĭollections.sort(entry.getValue(), new Comparator () int compare(Player player1, Player player2) GroupedPlayerDataByState.put(player.getState(), new ArrayList(Arrays.asList(player))) GroupedPlayerDataByState.get(player.getState()).add(player) Map> groupedPlayerDataByState = new HashMap() Step 1: Group the Player data by state some example data from the populatePlayerData method. we would have a database, but for our purposes, we'll just hard code Step 0: make sure our players List is populated with data. of the process for finding the high scores in each state without the use of streams, we'll need to break down each step Private static List getHighScoresWithoutStreams() Return "\nname=" + name + ", state=" + state+ ", highScore=" + highScore Public void setHighScore(Integer highScore) Let's see how we would get aggregate information without using streams: Now, if we wanted to get the top high scores for players in all 50 states in the USA, how would we do this with our Java 7 level coding skills (read: not using streams)? They also have location information (i.e. In this game there are Players, and Players have high scores. Okay, so if streams are great for data aggregation, then let's see some examples. If you're not at all familiar with the concept of aggregate functions and grouping, I'd highly suggest reading my articles / listening to my podcasts on the topics: SQL Aggregate Functions and SQL Group By Data Aggregation without Streams In SQL, the aggregates we have access to are operations that are performed on groups of data like:Īll of these operations can be performed on data that is grouped into buckets. What the heck do I mean by aggregating data?Ĭonsider a database query that uses aggregates.

streams in java

Streams in Java 8 are essentially the solution to aggregating data easily.

STREAMS IN JAVA HOW TO

First and foremost, let's talk about why you should learn how to use streams.











Streams in java