Example
See the Pen ReactExample119 by kenanhancer (@kenanhancer) on CodePen.
Continue readingSee the Pen ReactExample114 by kenanhancer (@kenanhancer) on CodePen.
Continue readingSee the Pen ReactExample103 by kenanhancer (@kenanhancer) on CodePen.
Continue readingI don't know how to emphasise how much closures are important 🙂 but if you are not aware of it, you may face many unpredictable and unexpected issues in your code. Even you are aware of it, you may still have some logical problem due to "Function Closure"
Continue readingJava String is a sequence of characters.
Java String variable contains a collection of characters surrounded by double quotes.
An array of characters works same as Java String.
Java Strings are used for storing text.
In Java, Strings are immutable or final or constant which means each time when we are creating or editing or performing any method to change string, one string created in "String Constant Pool"
Immutable strings eliminate race conditions in multi-threaded applications.
There are two ways to create a String object:
String str1 = "Hello World!";
String str2 = "Hello World!";
Each time you create a string literal, the JVM checks the "string constant pool" first. If the string already exists in the pool, a reference to the pooled instance is returned. If the string doesn't exist in the pool, a new string instance is created and placed in the pool.
In other words, we don't create any String object using new keyword above. The JVM does that task for us, it create a String object. If the String object exist in the memory, it doesn't create a new String object rather it assigns the same old object to the new instance, that means even though we have two String instances above(str1 and str2) compiler only created one String object and assigned the same to both the instances.
For example there are 5 String instances that have same value, it means that in memory there is only one String object having the value and all the 5 String instances would be pointing to the same String object.
What if we want to have two different String object with the same String. For that we would need to create Strings using new keyword
String str1 = new String("Hello World!");
String str2 = new String("Hello World!");
In such case, the JVM will create a new String object in normal (non-pool) heap memory.
Notice that JVM handles allocation of memory space for string variables efficiently, whenever we create a String variable and assign a String value with String Literal(double quotes) , JVM check value of String variable in String pool, if found, it returns reference of String object, if not found then it creates a new String object in String pool and return reference of it.
I found some images from google to represent. So, there are three images below.
Type | Ordered | Mutable | Unique | Indexable | Duplicates Allowed | Notable Use Case |
---|---|---|---|---|---|---|
list | ✅ | ✅ | ❌ | ✅ | ✅ | General-purpose sequence |
tuple | ✅ | ❌ | ❌ | ✅ | ✅ | Fixed-size data |
set | ❌ | ✅ | ✅ | ❌ | ❌ | Unique elements, set operations |
frozenset | ❌ | ❌ | ✅ | ❌ | ❌ | Immutable set |
dict | ✅ | ✅ | ✅ (keys) | ✅ (by key) | ✅ (values only) | Key-value pairs |
deque | ✅ | ✅ | ❌ | Limited (0, -1) | ✅ | Fast double-ended queue |
namedtuple | ✅ | ❌ | ❌ | ✅, attr access | ✅ | Tuple with field names |
defaultdict | ✅ | ✅ | ✅ (keys) | ✅ (by key) | ✅ (values only) | Dict with auto-init for missing keys |
Counter | ✅ | ✅ | ✅ (keys) | ✅ (by key) | ✅ (values count duplicates) | Counting/frequency of elements |
🔹 dict
keys must be unique
🔹 Tuples can be used as dict keys if they’re hashtable
(no lists inside)
Type | Description | Example |
int | Integer numbers | x = 10 |
float | Floating-point numbers | x = 3.14 |
bool | Boolean values | x = True |
str | String (sequence of characters) | x = "hello" |
tuple | Immutable ordered sequence | x = (1, 2, 3) |
frozenset | Immutable set | x = frozenset([1, 2, 3]) |
bytes | Immutable byte sequence | x = b'data' |
complex | Complex numbers | x = 2 + 3j |
NoneType | Represents 'no value' | x = None |
Collection | Operation | Time Complexity | Example |
list | Indexing | O(1) | x = lst[2] |
list | Append | O(1) amortized | lst.append(10) |
list | Insert (start/middle) | O(n) | lst.insert(1, 99) |
list | Pop (end) | O(1) | x = lst.pop() |
list | Pop (start) | O(n) | x = lst.pop(0) |
list | Remove by value | O(n) | lst.remove(10) |
list | Membership test | O(n) | if 5 in lst: pass |
list | Iteration | O(n) | for x in lst: pass |
list | Sort | O(n log n) | lst.sort() |
list | Slicing | O(k) | sub = lst[1:4] |
tuple | Indexing | O(1) | x = tpl[1] |
tuple | Membership test | O(n) | if 3 in tpl: pass |
tuple | Iteration | O(n) | for x in tpl: pass |
tuple | Concatenation | O(n + m) | tpl3 = tpl1 + tpl2 |
tuple | Slicing | O(k) | sub = tpl[1:3] |
set | Add | O(1) | s.add(5) |
set | Remove | O(1) | s.remove(5) |
set | Membership test | O(1) | if 5 in s: pass |
set | Iteration | O(n) | for x in s: pass |
frozenset | Membership test | O(1) | if 2 in fs: pass |
frozenset | Iteration | O(n) | for x in fs: pass |
dict | Get | O(1) | x = d['key'] |
dict | Set | O(1) | d['key'] = 10 |
dict | Delete | O(1) | del d['key'] |
dict | Membership test | O(1) | if 'key' in d: pass |
dict | Iteration | O(n) | for k, v in d.items(): pass |
list
Use case: Dynamic, ordered collections with duplicates.
my_list = [1, 2, 3, 4]
my_list.append(4)
my_list.insert(0, 0)
my_list[0] = 10
tuple
Use case: Fixed-size data, coordinates, function return values.
position = (10, 20)
# position[0] = 5 # ❌ Error
set
Use case: Unordered collections with no duplicates, set operations.
unique_items = {1, 2, 3}
unique_items.add(4)
unique_items.discard(2)
dict
Use case: Fast key-value lookup, structured data.
person = {"name": "Alice", "age": 30}
person["age"] = 31
frozenset
Use case: Hashtable
version of set, usable as dict
or set
key.
fset = frozenset([1, 2, 3])
# fset.add(4) # ❌ Error
collections.deque
Use case: Fast appends/pops from both ends (queue/stack).
from collections import deque
dq = deque([1, 2, 3])
dq.appendleft(0)
dq.pop()
collections.namedtuple
Use case: Tuple with named fields, lightweight class alternative.
from collections import namedtuple
Point = namedtuple("Point", "x y")
p = Point(10, 20)
print(p.x, p[1])
collections.defaultdict
Use case: Dict with default factory (no KeyError on missing keys).
from collections import defaultdict
d = defaultdict(int)
d["a"] += 1
collections.Counter
Use case: Count frequency of items (e.g., words, characters).
from collections import Counter
c = Counter("banana")
print(c["a"]) # 3
from typing import NamedTuple
class Point(NamedTuple):
x: float
y: float
p = Point(1.5, 2.5)
print(p.x, p.y) # 1.5 2.5
print(p[0], p[1]) # 1.5 2.5
class Color(NamedTuple):
red: int
green: int
blue: int
white = Color(255, 255, 255)
print(white.red) # 255
class ApiResponse(NamedTuple):
status_code: int
message: str
data: dict
res = ApiResponse(200, "OK", {"user": "kenan"})
print(res.status_code, res.data["user"]) # 200 kenan
A tuple is a collection which is ordered and unchangeable or immutable.
In order to find more details about this
usage, follow link below.
https://www.w3schools.com/Js/js_this.asp
I just took the following part from that page.