Skip to content

Commit d6cd880

Browse files
author
Rajeev Kumar Singh
committed
LinkedList and LinkedHashMap
1 parent d8dab44 commit d6cd880

20 files changed

+342
-19
lines changed

Readme.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
1. [Java ArrayList Tutorial with Examples](https://www.callicoder.com/java-arraylist/)
44

5-
2. [Java HashMap Tutorial with Examples](https://www.callicoder.com/java-hashmap/)
5+
2. [Java LinkedList Tutorial with Examples](https://www.callicoder.com/java-linkedlist/)
66

7-
3. [Java HashSet Tutorial with Examples](https://www.callicoder.com/java-hashset/)
7+
3. [Java HashMap Tutorial with Examples](https://www.callicoder.com/java-hashmap/)
88

9-
4. [Java TreeMap Tutorial with Examples](https://www.callicoder.com/java-treemap/)
9+
4. [Java LinkedHashMap Tutorial with Examples](https://www.callicoder.com/java-linkedhashmap/)
1010

11-
5. [Java TreeSet Tutorial with Examples](https://www.callicoder.com/java-treeset/)
11+
5. [Java TreeMap Tutorial with Examples](https://www.callicoder.com/java-treemap/)
12+
13+
6. [Java HashSet Tutorial with Examples](https://www.callicoder.com/java-hashset/)
14+
15+
7. [Java TreeSet Tutorial with Examples](https://www.callicoder.com/java-treeset/)

java-arraylist-examples/src/ArrayListSortExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public int compare(String name1, String name2) {
2020
}
2121
});
2222

23-
// The above `sort()` method call can also be written simply using lambda expressions
23+
// The above `sort()` method call can also be written simply using lambda expression
2424
names.sort((name1, name2) -> name1.compareTo(name2));
2525

2626
// Following is an even more concise solution

java-arraylist-examples/src/RemoveElementsFromArrayListExample.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ public static void main(String[] args) {
1919
programmingLanguages.remove(5);
2020
System.out.println("After remove(5): " + programmingLanguages);
2121

22-
// Remove the element "Kotlin" (The remove() method returns false if the element does not exist in the ArrayList)
22+
// Remove the first occurrence of the given element from the ArrayList
23+
// (The remove() method returns false if the element does not exist in the ArrayList)
2324
boolean isRemoved = programmingLanguages.remove("Kotlin");
2425
System.out.println("After remove(\"Kotlin\"): " + programmingLanguages);
2526

26-
// Remove all the elements belonging to the collection scriptingLanguages
27+
// Remove all the elements that exist in a given collection
2728
List<String> scriptingLanguages = new ArrayList<>();
2829
scriptingLanguages.add("Python");
2930
scriptingLanguages.add("Ruby");

java-arraylist-examples/src/SearchElementsInArrayListExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static void main(String[] args) {
1515
// Check if an ArrayList contains a given element
1616
System.out.println("Does names array contain \"Bob\"? : " + names.contains("Bob"));
1717

18-
// Find the index of first occurrence of an element in an ArrayList
18+
// Find the index of the first occurrence of an element in an ArrayList
1919
System.out.println("indexOf \"Steve\": " + names.indexOf("Steve"));
2020
System.out.println("indexOf \"Mark\": " + names.indexOf("Mark"));
2121

java-arraylist-examples/src/SynchronizedArrayListExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static void main(String[] args) throws InterruptedException {
1515
// Create a thread pool of size 10
1616
ExecutorService executorService = Executors.newFixedThreadPool(10);
1717

18-
// Create a Runnable task that increments the each element of the ArrayList by one
18+
// Create a Runnable task that increments each element of the ArrayList by one
1919
Runnable task = () -> {
2020
incrementArrayList(safeArrayList);
2121
};

java-arraylist-examples/src/UnsafeArrayListExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static void main(String[] args) throws InterruptedException {
1414
// Create a thread pool of size 10
1515
ExecutorService executorService = Executors.newFixedThreadPool(10);
1616

17-
// Create a Runnable task that increments the each element of the ArrayList by one
17+
// Create a Runnable task that increments each element of the ArrayList by one
1818
Runnable task = () -> {
1919
incrementArrayList(unsafeArrayList);
2020
};

java-hashmap-examples/src/AccessKeysFromHashMapExample.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ public class AccessKeysFromHashMapExample {
55
public static void main(String[] args) {
66
Map<String, String> userCityMapping = new HashMap<>();
77

8-
// Check if HashMap is empty
8+
// Check if a HashMap is empty
99
System.out.println("is userCityMapping empty? : " + userCityMapping.isEmpty());
1010

1111
userCityMapping.put("John", "New York");
1212
userCityMapping.put("Rajeev", "Bengaluru");
1313
userCityMapping.put("Steve", "London");
1414

15+
System.out.println("userCityMapping HashMap : " + userCityMapping);
16+
1517
// Find the size of a HashMap
1618
System.out.println("We have the city information of " + userCityMapping.size() + " users");
1719

@@ -25,9 +27,17 @@ public static void main(String[] args) {
2527
System.out.println("City details not found for user " + userName);
2628
}
2729

30+
// Check if a value exists in a HashMap
31+
if(userCityMapping.containsValue("New York")) {
32+
System.out.println("There is a user in the userCityMapping who lives in New York");
33+
} else {
34+
System.out.println("There is not user in the userCityMapping who lives in New York");
35+
}
36+
37+
2838
// Modify the value assigned to an existing key
2939
userCityMapping.put(userName, "California");
30-
System.out.println(userName + " moved to a new city " + userCityMapping.get(userName));
40+
System.out.println(userName + " moved to a new city " + userCityMapping.get(userName) + ", New userCityMapping : " + userCityMapping);
3141

3242
// The get() method returns `null` if the specified key was not found in the HashMap
3343
System.out.println("Lisa's city : " + userCityMapping.get("Lisa"));

java-hashmap-examples/src/CreateHashMapExample.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public static void main(String[] args) {
1111
numberMapping.put("Two", 2);
1212
numberMapping.put("Three", 3);
1313

14+
// Add a new key-value pair only if the key does not exist in the HashMap, or is mapped to `null`
15+
numberMapping.putIfAbsent("Four", 4);
16+
1417
System.out.println(numberMapping);
1518
}
1619
}

java-hashmap-examples/src/HashMapEntryKeySetValuesExample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ public static void main(String[] args) {
1515

1616
// HashMap's entry set
1717
Set<Map.Entry<String, String>> countryISOCodeEntries = countryISOCodeMapping.entrySet();
18-
System.out.println(countryISOCodeEntries);
18+
System.out.println("countryISOCode entries : " + countryISOCodeEntries);
1919

2020
// HashMap's key set
2121
Set<String> countries = countryISOCodeMapping.keySet();
22-
System.out.println("Countries : " + countries);
22+
System.out.println("countries : " + countries);
2323

2424
// HashMap's values
2525
Collection<String> isoCodes = countryISOCodeMapping.values();
26-
System.out.println("ISO Codes : " + isoCodes);
26+
System.out.println("isoCodes : " + isoCodes);
2727
}
2828
}

java-hashmap-examples/src/RemoveKeysFromHashMapExample.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ public static void main(String[] args) {
1010

1111
System.out.println("Husband-Wife Mapping : " + husbandWifeMapping);
1212

13-
// Unfortunately, Chris got divorced. Let's remove him from the mapping
13+
// Remove a key from the HashMap
14+
// Ex - Unfortunately, Chris got divorced. Let's remove him from the mapping
1415
String husband = "Chris";
1516
String wife = husbandWifeMapping.remove(husband);
1617

1718
System.out.println("Couple (" + husband + " => " + wife + ") got divorced");
1819
System.out.println("New Mapping : " + husbandWifeMapping);
1920

20-
// Divorce "Jack" only if He is married to "Linda"
21+
// Remove a key from the HashMap only if it is mapped to the given value
22+
// Ex - Divorce "Jack" only if He is married to "Linda"
2123
boolean isRemoved = husbandWifeMapping.remove("Jack", "Linda");
22-
System.out.println("Did Jack got removed from the mapping? : " + isRemoved);
24+
System.out.println("Did Jack get removed from the mapping? : " + isRemoved);
2325

2426
// remove() returns null if the mapping was not found for the supplied key
2527
wife = husbandWifeMapping.remove("David");

0 commit comments

Comments
 (0)