Skip to content

Instantly share code, notes, and snippets.

@priyankshah217
Last active April 6, 2021 07:23
Show Gist options
  • Save priyankshah217/6350f57780bc55a60b04bd79cbcfb9e6 to your computer and use it in GitHub Desktop.
Save priyankshah217/6350f57780bc55a60b04bd79cbcfb9e6 to your computer and use it in GitHub Desktop.
Performance comparison of iteration vs steams
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Solution {
public static void main(String[] args) {
String str = "Basic Service Set (BSS): A set of stations controlled by a single coordination function.\n" +
"Distribution: The service that, by using association information,\n" +
"delivers medium access control (MAC) service data units (MSDUs)\n" +
"within the distribution system (DS).\n" +
"Distribution System Service (DSS): The set of services provided by\n" +
"(MAC) layer to transport MAC service data units (MSDUs) between\n" +
"stations that are not in direct communication with each other over a\n" +
"single instance of the wireless medium (WM). These services include\n" +
"the transport of MSDUs between the access points (APs) of basic\n" +
"service sets (BSSs) within an extended service set (ESS), transport\n" +
"of MSDUs between portals and BSSs within an ESS, and transport of\n" +
"MSDUs between stations in the same BSS in cases where the MSDU has a\n" +
"multicast or broadcast destination address, or where the destination\n" +
"is an individual address but the station sending the MSDU chooses to\n" +
"involve the DSS. DSSs are provided between pairs of IEEE 802.11\n" +
"MACs.\n" +
"Integration: The service that enables delivery of medium access\n" +
"control (MAC) service data units (MSDUs) between the distribution\n" +
"system (DS) and an existing, non-IEEE 802.11 local area network (via\n" +
"a portal).\n" +
"Station (STA): A device that contains an IEEE 802.11 conformant\n" +
"medium access control (MAC) and physical layer (PHY) interface to the\n" +
"wireless medium (WM).\n" +
"Portal: The logical point at which medium access control (MAC)\n" +
"service data units (MSDUs) from a non-IEEE 802.11 local area network\n" +
"(LAN) enter the distribution system (DS) of an extended service set\n" +
"(ESS).\n" +
"WLAN: In this document, WLAN refers to a logical component\n" +
"instantiated on a WTP device. A single physical WTP may operate a\n" +
"number of WLANs. Each Basic Service Set Identifier (BSSID) and its\n" +
"constituent wireless terminal radios is denoted as a distinct WLAN on\n" +
"a physical WTP.\n" +
"Wireless Termination Point (WTP): The physical or network entity that\n" +
"contains an IEEE 802.11 RF antenna and wireless PHY to transmit and\n" +
"receive station traffic for wireless access networks.";
long start = System.nanoTime();
Set<Character> characterList = getDuplicateCharsSetIterative(str);
long end = System.nanoTime();
long execution = end - start;
System.out.println("Execution time is " + execution + " nanoseconds");
System.out.println(characterList);
/*
Execution time is 1011943 nanoseconds
[
, , (, ), ,, -, ., 0, 1, 2, 8, :, A, B, C, D, E, H, I, L, M, N, P, S, T, U, W, Y, a, b, c, d, e, f, g, h, i, k, l, m, n, o, p, r, s, t, u, v, w, x, y]
*/
start = System.nanoTime();
Set<Character> duplicateCharsSet = getDuplicateCharsSetStreams(str);
end = System.nanoTime();
execution = end - start;
System.out.println("Execution time is " + execution + " nanoseconds");
System.out.println(duplicateCharsSet);
/*
Execution time is 9840996 nanoseconds
[
, , (, ), ,, -, ., 0, 1, 2, 8, :, A, B, C, D, E, H, I, L, M, N, P, S, T, U, W, Y, a, b, c, d, e, f, g, h, i, k, l, m, n, o, p, r, s, t, u, v, w, x, y]
*/
}
private static Set<Character> getDuplicateCharsSetIterative(String str) {
int[] intArray = new int[256];
Set<Character> characterSet = new HashSet<>();
for (char ch : str.toCharArray()) {
if (intArray[ch] > 0) {
characterSet.add(ch);
} else {
intArray[ch] = 1;
}
}
return characterSet;
}
@NotNull
private static Set<Character> getDuplicateCharsSetStreams(String str) {
return str
.chars()
.mapToObj(value -> (char) value)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream()
.filter(characterLongEntry -> characterLongEntry.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment