Skip to content

Instantly share code, notes, and snippets.

@SansWord
Last active July 3, 2016 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SansWord/91b0a39c451df3c40ee01776d964e88e to your computer and use it in GitHub Desktop.
Save SansWord/91b0a39c451df3c40ee01776d964e88e to your computer and use it in GitHub Desktop.
Compares methods to literate over a list
Compares methods to literate over a list:
The following codes all do the same things:
Given a list of ticket,
1. put ticket into a map.
2. count the number of ticket which status is BOUGNT, initial an AtomicInteger with the count of bought tickets.
1. for-loop iteration:
39 106 94 85 39 88 94 40 90 95 86 38 93 95 86 40 93 93 86 39 92 93 86 39 94 94 39 91 94 39 94 92 83 38 90 93 40 94 93 84 39 93 90 87 39 93 92 84 39 93 94 38 89 93 85 39 92 92 40 93 94 39 92 94 83 39 91 93 85 39 92 90
Num 72
Mean 76.611111
Standard deviation 23.554049
Minimum 38
Maximum 106
2.a stream-filter-count iteration:
88 103 106 93 101 107 91 86 98 96 90 83 94 96 89 83 97 97 89 84 96 100 92 80 95 96 86 96 98 82 95 100 91 83 109 96 82 95 96 85 97 96 84 96 96 86 96 97 88 82 96 97 90 81 99 99 85 95 97 92 84 96 98 82 97 97 89 83 98 99
Num 70
Mean 92.8
Standard deviation 6.751931
Minimum 80
Maximum 109
2.b stream-atomic-add iteration:
81 95 93 87 79 93 93 87 79 92 96 87 77 93 97 87 79 92 97 87 80 93 93 88 79 92 95 87 78 96 95 86 81 92 95 87 78 92 93 88 82 92 96 86 79 92 94 85 80 95 93 87 80 93 93 87 78 92 95 86 79 91 95 85 79 92 96 88 79 91 94 85 81 93 96 86 80 92 96 87
Num 80
Mean 88.3625
Standard deviation 6.087372
Minimum 77
Maximum 97
3.a stream-parallel-filter-count iteration:
85 100 107 89 99 110 94 88 101 103 95 85 98 100 92 84 100 101 85 100 102 86 100 102 86 100 102 90 84 99 100 93 86 99 101 85 101 105 91 84 100 103 86 100 102 85 100 102 88 101 101 94 85 98 101 85 101 101 85 101 102 88 101 102 91 87 106 102
Num 68
Mean 95.735294
Standard deviation 7.267206
Minimum 84
Maximum 110
3.b stream-parallel-atomic-add iteration:
80 98 103 90 82 100 98 89 85 97 99 87 83 95 98 89 81 96 99 89 83 95 97 86 82 95 98 89 84 95 98 90 81 96 98 88 89 93 97 87 83 97 97 87 82 95 97 88 80 98 97 89 80 94 100 88 81 97 99 89 80 96 98 89 82 97 99 88 83 98 97 86 84 96 100 88 80 96 101 87
Num 80
Mean 91.275
Standard deviation 6.678276
Minimum 80
Maximum 103
List<Ticket> tickets = fetchTickets();
int count = 0;
for (Ticket ticket : tickets) {
String ticketId = ticket.getId();
ticketMap.put(ticketId, ticket);
if (BOUGHT.equals(ticket.getStatus())) {
count++;
}
}
AtomicInteger boughtCount = new AtomicInteger(count);
List<Ticket> tickets = fetchTickets();
Stream<Ticket> ticketsListStream = tickets.stream().parallel();
1. for-loop iteration:
Num 72, Mean 76.611111, Standard deviation 23.554049, Minimum 38, Maximum 106
2.a stream-filter-count iteration:
Num 70, Mean 92.8, Standard deviation 6.751931, Minimum 80, Maximum 109
2.b stream-atomic-add iteration:
Num 80, Mean 88.3625, Standard deviation 6.087372, Minimum 77, Maximum 97
3.a stream-parallel-filter-count iteration:
Num 68, Mean 95.735294, Standard deviation 7.267206, Minimum 84, Maximum 110
3.b stream-parallel-atomic-add iteration:
Num 80, Mean 91.275, Standard deviation 6.678276, Minimum 80, Maximum 103
List<Ticket> tickets = fetchTickets();
AtomicInteger boughtCount = new AtomicInteger(0);
Stream<Ticket> ticketsListStream = tickets.stream();
ticketsListStream.forEach(ticket -> {
String ticketId = ticket.getId();
ticketMap.put(ticketId, ticket);
boughtCount.addAndGet(1);
});
List<Ticket> tickets = fetchTickets();
Stream<Ticket> ticketsListStream = tickets.stream();
ticketsListStream.forEach(ticket -> {
String ticketId = ticket.getId();
ticketMap.put(ticketId, ticket);
});
ticketsListStream = tickets.stream();
long boughtCountNum = ticketsListStream.filter(ticket -> BOUGHT.equals(ticket.getStatus())).count();
AtomicInteger boughtCount = new AtomicInteger((int) boughtCountNum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment