Skip to content

Commit 231d0e4

Browse files
committed
Changes in Basics
1 parent 6fcc920 commit 231d0e4

9 files changed

Lines changed: 36 additions & 28 deletions

File tree

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
<artifactId>maven-compiler-plugin</artifactId>
5454
<version>3.7.0</version>
5555
<configuration>
56-
<source>11</source>
57-
<target>11</target>
56+
<source>17</source>
57+
<target>17</target>
5858
</configuration>
5959
</plugin>
6060
</plugins>

src/main/java/com/ab/collection/arrays/ArrayDemo.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.ab.collection.arrays;
22

3-
import java.util.Arrays;
4-
53
/**
64
* @author Arpit Bhardwaj
75
*
@@ -19,9 +17,8 @@ public static void main(String[] args) {
1917
//int ids[] = new int[10];
2018
//int ids [] = new int[10];
2119

22-
//int[] ids; //not valid as down in when we use it and compiler will complain
2320
//int[] ids = new int[0]; //valid create an array of length 0
24-
int[] ids1; //valid as we are not using this in the code
21+
int[] ids1; //valid as we are not using this in the code but on usage compiler will complain to initialize
2522
int[] ids2,ids3;
2623

2724
//Accessing and Iteration
@@ -35,7 +32,5 @@ public static void main(String[] args) {
3532
for (int id: ids) {
3633
System.out.print(id);
3734
}
38-
39-
System.out.println();
4035
}
4136
}

src/main/java/com/ab/collection/arrays/Varargs.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.ab.collection.arrays;
22

3-
import java.util.Arrays;
43
/**
54
* @author Arpit Bhardwaj
65
*
@@ -28,7 +27,7 @@ public static void main(String... args) {
2827
walk(1, 2); // 1
2928
walk(1, 2, 3); // 2
3029
walk(1, new int[] {4, 5}); // 2
31-
walk(1,null); // throws NullPointerException in walk
30+
//walk(1,null); // throws NullPointerException in walk
3231

3332
//varargs can only be used as a method parameter.
3433
//String... s = ""; //compile error

src/main/java/com/ab/collection/basic/CollectionDemo.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66

77
/**
88
* @author Arpit Bhardwaj
9-
*
10-
* The right side of the for-each loop must be one of the following:
11-
* A built-in Java array
12-
* An object whose type implements java.lang.Iterable
13-
*
14-
* Hence, Map is not supported in a for-each loop
159
*/
1610
public class CollectionDemo {
1711
public static void main(String[] args) {
@@ -44,9 +38,8 @@ public static void main(String[] args) {
4438
Product product = productIterator2.next();
4539
if(product.getWeight() > 20){
4640
System.out.println(product);
47-
}
48-
else{
49-
productIterator2.remove();
41+
} else{
42+
productIterator2.remove(); //Removes from the underlying collection the last element returned by this iterator
5043
//products.remove(product); //throw ConcurrentModificationException
5144
}
5245
}

src/main/java/com/ab/collection/basic/CollectionsClassDemo.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
*/
1414
public class CollectionsClassDemo {
1515
public static void main(String[] args) {
16-
//List<Integer> integerList = List.of(4, 8, 1, 3, 9); //Returns an unmodifiable list
1716
List<Integer> integerList = new ArrayList<>(List.of(4, 8, 1, 3, 9));
1817

1918
/******** sorting ********/
2019
Collections.sort(integerList);//throws UnsupportedOperationException in case of unmodifiable list
2120
System.out.println(integerList);
2221

2322
/******** searching ********/
24-
System.out.println(Collections.binarySearch(integerList,10));
23+
System.out.println(Collections.binarySearch(integerList,10)); //(-(insertion point) - 1
2524

2625
/******** misc ********/
2726
Collections.reverse(integerList);
@@ -31,6 +30,9 @@ public static void main(String[] args) {
3130
System.out.println(integerList);
3231

3332
List<Product> products = new ArrayList<>();
33+
Product floorPanel = new Product("Panel", 25);
34+
products.add(floorPanel);
35+
3436
Collections.fill(products, new Product("Door",35));
3537
System.out.println(products);
3638
}

src/main/java/com/ab/collection/basic/ImmutableCollections.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
*/
1010
public class ImmutableCollections {
1111
public static void main(String[] args) {
12+
13+
List<Integer> readOnlyList1 = List.of(4, 8, 1, 3, 9); //Returns an unmodifiable list
14+
//readOnlyList1.add(6); //throws UnsupportedOperationException
15+
1216
List<String> list = new ArrayList<>();
1317
list.add("raj");
14-
List<String> readOnlyList1 = Collections.unmodifiableList(list);
15-
//readOnlyList1.add("simran"); //throws UnsupportedOperationException
18+
List<String> readOnlyList2 = Collections.unmodifiableList(list);
19+
//readOnlyList2.add("simran"); //throws UnsupportedOperationException
1620

1721
Set<String> readOnlySet = new HashSet<>();
1822
readOnlySet = Collections.unmodifiableSet(readOnlySet);

src/main/java/com/ab/collection/basic/NullElements.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Map: Allow at most one null key but multiple null values.
1414
* TreeSet: Do not allow null values (as it is an ordered set and uses compareTo method internally)
1515
* TreeMap: Do not allow null keys but multiple null values are allowed
16-
* Hashtable & ConcurrentHashMap: Do not allow null keys or values is because of multi-threaded environment.
16+
* Hashtable & ConcurrentHashMap: Do not allow null keys or values is because of multithreaded environment.
1717
* CopyOnWriteArrayList: Allows multiple null elements
1818
* CopyOnWriteArraySet: Allow single null element
1919
*

src/main/java/com/ab/collection/maps/HashCodeEqualsContract.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
import java.util.Map;
77
import java.util.Objects;
88

9+
/**
10+
* @author Arpit Bhardwaj
11+
*
12+
* Contract:
13+
* If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
14+
*
15+
* It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results.
16+
* However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
17+
*/
18+
919
public class HashCodeEqualsContract {
1020
static class Product{
1121
private final String name;
@@ -29,7 +39,6 @@ public String toString() {
2939
return "Product{ "+name+":"+weight+" }";
3040
}
3141

32-
3342
@Override
3443
public boolean equals(Object o) {
3544
if (this == o) return true;
@@ -39,7 +48,6 @@ public boolean equals(Object o) {
3948
Objects.equals(name, product.name);
4049
}
4150

42-
4351
@Override
4452
public int hashCode() {
4553
return Objects.hash(name, weight);
@@ -56,11 +64,13 @@ public static void main(String[] args) {
5664
map.put(p3,3456);
5765

5866
//implement only equals
67+
//default hasCode() impl returns distinct integer for distinct object
5968
//you will not able to retrieve what you have added as the logical equal products have different hashcode
6069
Product p4 = new Product("P2",24);
6170
System.out.println(map.get(p4));
6271

6372
//implement only hashcode
73+
//default equals() impl returns true only if two objects had the same reference pointer
6474
//you can add duplicate keys which are logically equal
6575
//also you will not able to retrieve what you have added as the logical equal products have different hashcode
6676
Product p5 = new Product("P3",29);

src/main/java/com/ab/collection/maps/MapDemo.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@
88
/**
99
* @author Arpit Bhardwaj
1010
*
11+
* The right side of the for-each loop must be one of the following:
12+
* A built-in Java array
13+
* An object whose type implements java.lang.Iterable
14+
* Hence, Map is not supported in a for-each loop
15+
*
1116
* No arg constructor creates a set of initial capacity of 16 elements with Default Load Factor = 0.75 (75%)
1217
* Map is a combination of set and list
1318
* Keys is basically a set and values is basically a list
1419
*/
1520
public class MapDemo {
1621
public static void main(String[] args) {
1722
Map<Integer,String> map1 = new HashMap<>(20,0.85F);
18-
//read only maps
19-
Map<Integer,String> map = Map.of(1,"str1",3,"str2",4,"str3");
23+
24+
Map<Integer,String> map = Map.of(1,"str1",3,"str2",4,"str3"); //returns read only map
2025

2126
for (Map.Entry entry:
2227
map.entrySet()) {

0 commit comments

Comments
 (0)