Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java_DSA_added #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions data_structures/Arrays/Java/Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

import java.util.Arrays;

/*
*
/$$$$$$
/$$__ $$
| $$ \ $$ /$$$$$$ /$$$$$$ /$$$$$$ /$$ /$$ /$$$$$$$
| $$$$$$$$ /$$__ $$ /$$__ $$|____ $$| $$ | $$ /$$_____/
| $$__ $$| $$ \__/| $$ \__/ /$$$$$$$| $$ | $$| $$$$$$
| $$ | $$| $$ | $$ /$$__ $$| $$ | $$ \____ $$
| $$ | $$| $$ | $$ | $$$$$$$| $$$$$$$ /$$$$$$$/
|__/ |__/|__/ |__/ \_______/ \____ $$|_______/
/$$ | $$|
| $$$$$$$$/
\_______/
*/
/*
*

- EXPLANATIONS:
- An array is a data structure that stores a collection of elements of the same data type in a contiguous block of memory.
It provides a way to store and access a group of related values using a single variable name.

- Each element in an array is identified by an index, which represents its position in the array.
The index starts at 0 for the first element, and increases by 1 for each subsequent element.
This means that if an array has n elements, the indices range from 0 to n-1.

- Arrays can be used to store and manipulate different types of data, such as numbers, characters, strings, and objects.
They are commonly used in programming for a variety of tasks, such as sorting, searching, and processing large amounts of data.

- MOST COMMONLY USED BUILT-IN FUNCTIONS:

- array.append(x) - This function is used to add an element x to the end of the array.
- array.extend(iterable) - This function is used to add the elements of an iterable (such as a list or tuple) to the end of the array.
- array.insert(i, x) - This function is used to insert an element x at a specific index i in the array.
- array.remove(x) - This function is used to remove the first occurrence of element x from the array.
- array.pop(i) - This function is used to remove and return the element at a specific index i in the array. If no index is specified, the last element of the array is removed and returned.
- array.index(x) - This function is used to find the first occurrence of element x in the array and return its index.
- array.count(x) - This function is used to count the number of occurrences of element x in the array.
- array.sort() - This function is used to sort the elements of the array in ascending order.
- array.reverse() - This function is used to reverse the order of the elements in the array.
- len(array) - This function is used to return the number of elements in the array.

- TIME AND SPACE COMPLEXITY:

- array.append(x) - Time complexity: O(1), Space complexity: O(1)
- array.extend(iterable) - Time complexity: O(k), where k is the length of the iterable being added, Space complexity: O(k)
- array.insert(i, x) - Time complexity: O(n), where n is the number of elements in the array, Space complexity: O(1)
- array.remove(x) - Time complexity: O(n), where n is the number of elements in the array, Space complexity: O(1)
- array.pop(i) - Time complexity: O(n), where n is the number of elements in the array, Space complexity: O(1)
- array.index(x) - Time complexity: O(n), where n is the number of elements in the array, Space complexity: O(1)
- array.count(x) - Time complexity: O(n), where n is the number of elements in the array, Space complexity: O(1)
- array.sort() - Time complexity: O(n log n), where n is the number of elements in the array, Space complexity: O(1)
- array.reverse() - Time complexity: O(n), where n is the number of elements in the array, Space complexity: O(1)
- len(array) - Time complexity: O(1), Space complexity: O(1)

*/

public class Array{
public static void main(String[] args) {

// One Dimensional Array
int[] array1D = {10,22,13,24,1};

// Two Dimensional Array
int[][] array2D = {{1, 20, 32, 4},
{5, 6, 70, 81},
{9, 10, 7, 12},
{3, 14, 5, 16}};

// Two Dimensional (n x n) Array
int[][] arraynD = {{1,2,3},{4,5,6},{7,8,9}};

// initialize new array
int[] newArray = new int[3]; //length = 3 {0,1,2} indexing


//Print all the elements of an 1D Array using for each loop
for(int i:array1D){
System.out.println(i);
}

System.out.println();

//Print all the elements of an 2D Array using for each loop
for(int[] i:array2D){
for(int j:i){
System.out.print(j+" ");
}
System.out.println();
}

//Array before sorting
System.out.println(Arrays.toString(array1D));
//To sort an array
Arrays.sort(array1D);
//Array after sorting
System.out.println(Arrays.toString(array1D));

}
}
149 changes: 149 additions & 0 deletions data_structures/HashMap/Java/HashmapJava.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package data_structures.HashMap.Java;

/*
/$$ /$$ /$$ /$$ /$$
| $$ | $$ | $$ | $$$ /$$$
| $$ | $$ /$$$$$$ /$$$$$$$| $$$$$$$ | $$$$ /$$$$ /$$$$$$ /$$$$$$
| $$$$$$$$ |____ $$ /$$_____/| $$__ $$| $$ $$/$$ $$ |____ $$ /$$__ $$
| $$__ $$ /$$$$$$$| $$$$$$ | $$ \ $$| $$ $$$| $$ /$$$$$$$| $$ \ $$
| $$ | $$ /$$__ $$ \____ $$| $$ | $$| $$\ $ | $$ /$$__ $$| $$ | $$
| $$ | $$| $$$$$$$ /$$$$$$$/| $$ | $$| $$ \/ | $$| $$$$$$$| $$$$$$$/
|__/ |__/ \_______/|_______/ |__/ |__/|__/ |__/ \_______/| $$____/
| $$
| $$
|__/
*/

/*
### HashMap in Java

# Definition:
`HashMap` is part of the `java.util` package and implements the `Map<K, V>` interface. It is used to store key-value pairs, where each key is unique. The `HashMap` class is based on a hash table, providing efficient operations for inserting, deleting, and retrieving elements.

# Key Characteristics:

1. Unique Keys :
- Each key in a `HashMap` must be unique. If you try to insert a duplicate key, the new value will replace the old value.

2. Null Values :
- `HashMap` allows one null key and multiple null values.

3. Non-Synchronized :
- `HashMap` is not synchronized, meaning it is not thread-safe. If multiple threads access a `HashMap` concurrently, and at least one of the threads modifies the map, it must be synchronized externally.

4. Ordering :
- `HashMap` does not guarantee any specific order of the elements. The order of keys and values can change over time.

# Common Operations:

1. Creating a HashMap :
```java
HashMap<Integer, String> map = new HashMap<>();
```

2. Adding Elements :
- Use `put(key, value)` to add key-value pairs.
```java
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
```

3. Accessing Elements :
- Use `get(key)` to retrieve the value associated with a specific key.
```java
String value = map.get(2); // Returns "Two"
```

4. Removing Elements :
- Use `remove(key)` to remove a key-value pair.
```java
map.remove(2); // Removes the key-value pair for key 2
```

5. Checking if Key or Value Exists :
- Use `containsKey(key)` to check if a key exists.
- Use `containsValue(value)` to check if a value exists.
```java
boolean hasKey = map.containsKey(1); // Returns true
boolean hasValue = map.containsValue("One"); // Returns true
```

6. Iterating through a HashMap :
- Use `keySet()`, `values()`, or `entrySet()` to iterate through keys, values, or key-value pairs, respectively.
```java
for (Integer key : map.keySet()) {
System.out.println("Key: " + key);
}

for (String val : map.values()) {
System.out.println("Value: " + val);
}

for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
```

7. Getting the Size :
- Use `size()` to get the number of key-value pairs.
```java
int size = map.size(); // Returns the number of key-value pairs
```

8. Clearing the HashMap :
- Use `clear()` to remove all key-value pairs.
```java
map.clear(); // Removes all key-value pairs
```

*/
//java
import java.util.HashMap;
import java.util.Map;


public class HashmapJava {
public static void main(String[] args) {
// Creating a HashMap
HashMap<Integer, String> map = new HashMap<>();

// Adding elements to the HashMap
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");

// Accessing elements
System.out.println("Value for key 2: " + map.get(2)); // Outputs "Two"

// Removing an element
map.remove(2);

// Checking if a key or value exists
System.out.println("Contains key 1: " + map.containsKey(1)); // Outputs "true"
System.out.println("Contains value 'One': " + map.containsValue("One")); // Outputs "true"

// Iterating through the HashMap
System.out.println("Keys:");
for (Integer key : map.keySet()) {
System.out.println(key);
}

System.out.println("Values:");
for (String val : map.values()) {
System.out.println(val);
}

System.out.println("Key-Value Pairs:");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}

// Getting the size of the HashMap
System.out.println("Size of the map: " + map.size());

// Clearing the HashMap
map.clear();
System.out.println("Size of the map after clear: " + map.size());
}
}
130 changes: 130 additions & 0 deletions data_structures/HashSet/Java/HashSetJava.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package data_structures.HashSet.Java;

/*
/$$ /$$ /$$ /$$$$$$ /$$
| $$ | $$ | $$ /$$__ $$ | $$
| $$ | $$ /$$$$$$ /$$$$$$$| $$$$$$$ | $$ \__/ /$$$$$$ /$$$$$$
| $$$$$$$$ |____ $$ /$$_____/| $$__ $$| $$$$$$ /$$__ $$|_ $$_/
| $$__ $$ /$$$$$$$| $$$$$$ | $$ \ $$ \____ $$| $$$$$$$$ | $$
| $$ | $$ /$$__ $$ \____ $$| $$ | $$ /$$ \ $$| $$_____/ | $$ /$$
| $$ | $$| $$$$$$$ /$$$$$$$/| $$ | $$| $$$$$$/| $$$$$$$ | $$$$/
|__/ |__/ \_______/|_______/ |__/ |__/ \______/ \_______/ \___/
*/

/*
### HashSet in Java

# Definition:
`HashSet` is part of the `java.util` package and implements the `Set<E>` interface. It is used to store a collection of unique elements. `HashSet` is backed by a hash table (actually a `HashMap` instance) and does not guarantee any specific order of the elements.

# Key Characteristics:

1. Unique Elements :
- `HashSet` only stores unique elements. If you try to add a duplicate element, the `HashSet` will ignore it.

2. Null Values :
- `HashSet` allows null values.

3. Non-Synchronized :
- `HashSet` is not synchronized, meaning it is not thread-safe. If multiple threads access a `HashSet` concurrently, and at least one of the threads modifies the set, it must be synchronized externally.

4. Ordering :
- `HashSet` does not guarantee any specific order of the elements. The order of elements can change over time.

# Common Operations:

1. Creating a HashSet :
```java
HashSet<String> set = new HashSet<>();
```

2. Adding Elements :
- Use `add(element)` to add elements to the set.
```java
set.add("One");
set.add("Two");
set.add("Three");
```

3. Removing Elements :
- Use `remove(element)` to remove a specific element from the set.
```java
set.remove("Two");
```

4. Checking if Element Exists :
- Use `contains(element)` to check if an element exists in the set.
```java
boolean exists = set.contains("One"); // Returns true
```

5. Iterating through a HashSet :
- Use an enhanced for loop or an iterator to iterate through the elements of the set.
```java
for (String element : set) {
System.out.println(element);
}

Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
```

6. Getting the Size :
- Use `size()` to get the number of elements in the set.
```java
int size = set.size(); // Returns the number of elements in the set
```

7. Clearing the HashSet :
- Use `clear()` to remove all elements from the set.
```java
set.clear(); // Removes all elements from the set
```

*/
//java
import java.util.HashSet;
import java.util.Iterator;

public class HashSetJava {
public static void main(String[] args) {
// Creating a HashSet
HashSet<String> set = new HashSet<>();

// Adding elements to the HashSet
set.add("One");
set.add("Two");
set.add("Three");

// Adding a duplicate element
set.add("One"); // This will not add a new element

// Checking if an element exists
System.out.println("Contains 'One': " + set.contains("One")); // Outputs "true"

// Removing an element
set.remove("Two");

// Iterating through the HashSet using an enhanced for loop
System.out.println("Elements in the set:");
for (String element : set) {
System.out.println(element);
}

// Iterating through the HashSet using an iterator
System.out.println("Elements in the set (using iterator):");
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}

// Getting the size of the HashSet
System.out.println("Size of the set: " + set.size());

// Clearing the HashSet
set.clear();
System.out.println("Size of the set after clear: " + set.size());
}
}
Loading