summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorKamal Wickramanayake <kamal@inbox.lk>2026-02-21 20:33:23 +0530
committerKamal Wickramanayake <kamal@inbox.lk>2026-02-21 20:33:23 +0530
commit530141c542ab3b44991f05016e66db651795e9c9 (patch)
treeabf4953878cd274d06d890c3bcad4e0230b30943 /java
parent3625bc6cbddd473659b3f584c0dbec515c07c786 (diff)
Added java/collections and java/error handling sample code
Diffstat (limited to 'java')
-rw-r--r--java/10-collections/.gitignore2
-rw-r--r--java/10-collections/Customer.java48
-rw-r--r--java/10-collections/Test1.java29
-rw-r--r--java/10-collections/Test2.java27
-rw-r--r--java/10-collections/Test3.java22
-rw-r--r--java/10-collections/Test4List.java38
-rw-r--r--java/10-collections/Test5Set.java30
-rw-r--r--java/10-collections/Test6Map.java19
-rw-r--r--java/10-collections/hashcode-contract.txt15
-rw-r--r--java/11-error-handling-1/.gitignore2
-rw-r--r--java/11-error-handling-1/ErrorHandlingExample1.java20
-rw-r--r--java/12-error-handling-2/.gitignore2
-rw-r--r--java/12-error-handling-2/Maths.java11
-rw-r--r--java/12-error-handling-2/MathsException.java22
-rw-r--r--java/12-error-handling-2/MathsTest.java32
-rw-r--r--java/12-error-handling-2/MathsTest2.java27
-rw-r--r--java/12-error-handling-2/MathsTest3.java21
-rw-r--r--java/12-error-handling-2/MathsTest4.java33
-rw-r--r--java/12-error-handling-2/MathsTest5.java21
-rw-r--r--java/12-error-handling-2/MathsTest6.java22
-rw-r--r--java/12-error-handling-2/MathsTest7.java22
21 files changed, 465 insertions, 0 deletions
diff --git a/java/10-collections/.gitignore b/java/10-collections/.gitignore
new file mode 100644
index 0000000..e493dd6
--- /dev/null
+++ b/java/10-collections/.gitignore
@@ -0,0 +1,2 @@
+bin
+dist
diff --git a/java/10-collections/Customer.java b/java/10-collections/Customer.java
new file mode 100644
index 0000000..97fbf9f
--- /dev/null
+++ b/java/10-collections/Customer.java
@@ -0,0 +1,48 @@
+public class Customer {
+ private String name;
+
+ public Customer(String name) {
+ super();
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String toString() {
+ return "Customer [name=" + name + "]";
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((name == null) ? 0 : name.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ Customer other = (Customer) obj;
+ if (name == null) {
+ if (other.name != null)
+ return false;
+ } else if (!name.equals(other.name))
+ return false;
+ return true;
+ }
+
+
+}
diff --git a/java/10-collections/Test1.java b/java/10-collections/Test1.java
new file mode 100644
index 0000000..d0b8660
--- /dev/null
+++ b/java/10-collections/Test1.java
@@ -0,0 +1,29 @@
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+public class Test1 {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ List list = new ArrayList();
+// List list = new LinkedList();
+
+ list.add(new Customer("Nuwan"));
+ list.add(new Customer("Lasitha"));
+ list.add(new Customer("Praveen"));
+// list.add(new Object());
+
+ Iterator itr = list.iterator();
+ while(itr.hasNext()) {
+ Object obj = itr.next();
+ Customer c = (Customer) obj;
+ // Do some work with c here
+ System.out.println(c);
+ }
+ }
+
+}
diff --git a/java/10-collections/Test2.java b/java/10-collections/Test2.java
new file mode 100644
index 0000000..94c7234
--- /dev/null
+++ b/java/10-collections/Test2.java
@@ -0,0 +1,27 @@
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+public class Test2 {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ List<Customer> list = new ArrayList<Customer>();
+// List<Customer> list = new LinkedList<Customer>();
+
+ list.add(new Customer("Nuwan"));
+ list.add(new Customer("Lasitha"));
+ list.add(new Customer("Praveen"));
+// list.add("absdf");
+
+ Iterator<Customer> itr = list.iterator();
+ while(itr.hasNext()) {
+ Customer c = itr.next();
+ // Do some work with c here
+ System.out.println(c);
+ }
+ }
+}
diff --git a/java/10-collections/Test3.java b/java/10-collections/Test3.java
new file mode 100644
index 0000000..57e5e47
--- /dev/null
+++ b/java/10-collections/Test3.java
@@ -0,0 +1,22 @@
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+public class Test3 {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ List<Customer> list = new ArrayList<Customer>();
+
+ list.add(new Customer("Nuwan"));
+ list.add(new Customer("Lasitha"));
+ list.add(new Customer("Praveen"));
+
+ for (Customer c : list) {
+ // Do some work with c here
+ System.out.println(c);
+ }
+ }
+}
diff --git a/java/10-collections/Test4List.java b/java/10-collections/Test4List.java
new file mode 100644
index 0000000..c730a80
--- /dev/null
+++ b/java/10-collections/Test4List.java
@@ -0,0 +1,38 @@
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+public class Test4List {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ List<Customer> list = new ArrayList<Customer>();
+
+// List<Customer> syncList = Collections.synchronizedList(list);
+
+ list.add(new Customer("Nuwan"));
+ list.add(new Customer("Lasitha"));
+ list.add(new Customer("Praveen"));
+
+ Customer x = list.get(2);
+ System.out.println(x);
+
+// list.
+ list.add(2, new Customer("Kamal"));
+
+ Customer y = new Customer("ABC");
+ Customer z = new Customer("ABC");
+
+ list.add(y);
+ list.add(y);
+ list.add(z);
+
+ for (Customer c : list) {
+ // Do some work with c here
+ System.out.println(c);
+ }
+ }
+}
diff --git a/java/10-collections/Test5Set.java b/java/10-collections/Test5Set.java
new file mode 100644
index 0000000..8f182c4
--- /dev/null
+++ b/java/10-collections/Test5Set.java
@@ -0,0 +1,30 @@
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+public class Test5Set {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ Set<Customer> customers = new HashSet<Customer>();
+
+ customers.add(new Customer("Nuwan"));
+ customers.add(new Customer("Lasitha"));
+ customers.add(new Customer("Praveen"));
+
+ Customer y = new Customer("ABC");
+ Customer z = new Customer("ABC");
+ customers.add(y);
+ customers.add(y);
+ customers.add(z);
+
+ for (Customer c : customers) {
+ // Do some work with c here
+ System.out.println(c);
+ }
+ }
+}
diff --git a/java/10-collections/Test6Map.java b/java/10-collections/Test6Map.java
new file mode 100644
index 0000000..77ce613
--- /dev/null
+++ b/java/10-collections/Test6Map.java
@@ -0,0 +1,19 @@
+import java.util.HashMap;
+import java.util.Map;
+
+public class Test6Map {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+
+ Map<String, Customer> customers = new HashMap<String, Customer>();
+// Map<Integer, Account> accounts = ...
+
+ customers.put("nugegoda", new Customer("Kamal"));
+ customers.put("kalubowila", new Customer("Praveen"));
+
+ System.out.println(customers.get("kalubowila"));
+ }
+}
diff --git a/java/10-collections/hashcode-contract.txt b/java/10-collections/hashcode-contract.txt
new file mode 100644
index 0000000..2ed248c
--- /dev/null
+++ b/java/10-collections/hashcode-contract.txt
@@ -0,0 +1,15 @@
+ Whenever it is invoked on the same object more than once during an execu-
+ tion of a Java application, the hashCode() method must consistently return
+ the same integer, provided no information used in equals() comparisons
+ on the object is modified. This integer need not remain consistent from one
+ execution of an application to another execution of the same application.
+
+ If two objects are equal according to the equals(Object) method, then
+ calling the hashCode() method on each of the two objects must produce the
+ same integer result.
+
+ It is NOT required that if two objects are unequal according to the
+ equals(java.lang.Object) method, then calling the hashCode() method
+ on each of the two objects must produce distinct integer results. However,
+ the programmer should be aware that producing distinct integer results for
+ unequal objects may improve the performance of hashtables.
diff --git a/java/11-error-handling-1/.gitignore b/java/11-error-handling-1/.gitignore
new file mode 100644
index 0000000..e493dd6
--- /dev/null
+++ b/java/11-error-handling-1/.gitignore
@@ -0,0 +1,2 @@
+bin
+dist
diff --git a/java/11-error-handling-1/ErrorHandlingExample1.java b/java/11-error-handling-1/ErrorHandlingExample1.java
new file mode 100644
index 0000000..0dc0276
--- /dev/null
+++ b/java/11-error-handling-1/ErrorHandlingExample1.java
@@ -0,0 +1,20 @@
+public class ErrorHandlingExample1 {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ int x = 2;
+ int y = 0;
+ int z = 0;
+
+// try {
+ z = x / y;
+// } catch (ArithmeticException ae) {
+// System.out.println("An error occurred");
+// }
+
+ System.out.println(z);
+ }
+
+}
diff --git a/java/12-error-handling-2/.gitignore b/java/12-error-handling-2/.gitignore
new file mode 100644
index 0000000..e493dd6
--- /dev/null
+++ b/java/12-error-handling-2/.gitignore
@@ -0,0 +1,2 @@
+bin
+dist
diff --git a/java/12-error-handling-2/Maths.java b/java/12-error-handling-2/Maths.java
new file mode 100644
index 0000000..b61a9f2
--- /dev/null
+++ b/java/12-error-handling-2/Maths.java
@@ -0,0 +1,11 @@
+public class Maths {
+
+ public int factorial(int number) throws MathsException {
+ if (number < 0) {
+ MathsException e = new MathsException("Factorial of " + number + " not defined");
+// e.setX(100);
+ throw e;
+ }
+ return number == 0 ? 1 : number * factorial(number - 1);
+ }
+}
diff --git a/java/12-error-handling-2/MathsException.java b/java/12-error-handling-2/MathsException.java
new file mode 100644
index 0000000..bdbaf81
--- /dev/null
+++ b/java/12-error-handling-2/MathsException.java
@@ -0,0 +1,22 @@
+public class MathsException extends Exception {
+
+ public MathsException() {
+ // TODO Auto-generated constructor stub
+ }
+
+ public MathsException(String message) {
+ super(message);
+ // TODO Auto-generated constructor stub
+ }
+
+ public MathsException(Throwable cause) {
+ super(cause);
+ // TODO Auto-generated constructor stub
+ }
+
+ public MathsException(String message, Throwable cause) {
+ super(message, cause);
+ // TODO Auto-generated constructor stub
+ }
+
+}
diff --git a/java/12-error-handling-2/MathsTest.java b/java/12-error-handling-2/MathsTest.java
new file mode 100644
index 0000000..7b6827c
--- /dev/null
+++ b/java/12-error-handling-2/MathsTest.java
@@ -0,0 +1,32 @@
+public class MathsTest {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ Maths m = new Maths();
+ int result = 0;
+
+ try {
+ m.factorial(-5);
+ m.factorial(5); // Not executed
+ } catch (MathsException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ try {
+ result = m.factorial(-5);
+ // ...
+ } catch(MathsException me) {
+// System.err.println(me.getX());
+ System.err.println("Factorial method failed");
+ } finally {
+ System.out.println("Inside finally block");
+ }
+
+ System.out.println(result);
+
+ }
+
+}
diff --git a/java/12-error-handling-2/MathsTest2.java b/java/12-error-handling-2/MathsTest2.java
new file mode 100644
index 0000000..4aa8b59
--- /dev/null
+++ b/java/12-error-handling-2/MathsTest2.java
@@ -0,0 +1,27 @@
+public class MathsTest2 {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ Maths m = new Maths();
+ int result = 0;
+
+ try {
+ result = m.factorial(-5);
+ } catch(MathsException me) { // Catching a specific exception
+ System.err.println("Caught a MathsException");
+ } catch(Exception e) { // Catching the super type
+ System.err.println("Caught an Exception");
+ }
+
+ try {
+ result = m.factorial(-5);
+ } catch (MathsException | ArrayIndexOutOfBoundsException e) { // Java SE 7
+
+ }
+
+ System.out.println(result);
+ }
+
+}
diff --git a/java/12-error-handling-2/MathsTest3.java b/java/12-error-handling-2/MathsTest3.java
new file mode 100644
index 0000000..ca45e5c
--- /dev/null
+++ b/java/12-error-handling-2/MathsTest3.java
@@ -0,0 +1,21 @@
+public class MathsTest3 {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ Maths m = new Maths();
+ int result = 0;
+
+ try {
+ result = m.factorial(-5);
+ } catch(MathsException e) {
+ System.err.println("Factorial method failed");
+ System.err.println(e.getMessage());
+ e.printStackTrace();
+ }
+
+ System.out.println(result);
+ }
+
+}
diff --git a/java/12-error-handling-2/MathsTest4.java b/java/12-error-handling-2/MathsTest4.java
new file mode 100644
index 0000000..b5cca8a
--- /dev/null
+++ b/java/12-error-handling-2/MathsTest4.java
@@ -0,0 +1,33 @@
+public class MathsTest4 {
+
+ /**
+ * @param args
+ * @throws MathsException
+ */
+ public static void main(String[] args) throws MathsException {
+ Maths m = new Maths();
+ int result = 0;
+
+ // No catching! Then the method should be declared
+ // to throw the exception (or a super type like
+ // Exception or even Throwable)
+
+// m.factorial(5);
+
+ // Look! A try block can be defined without a catch block
+ // as well.
+ try {
+ result = m.factorial(-5);
+ System.out.println("xxx");
+ } finally {
+ System.out.println("Inside finally block");
+ }
+
+ // No try block is also possible
+ // Since the method is declared to duct the exception
+ result = m.factorial(-5);
+
+ System.out.println(result);
+ }
+
+}
diff --git a/java/12-error-handling-2/MathsTest5.java b/java/12-error-handling-2/MathsTest5.java
new file mode 100644
index 0000000..56bf64d
--- /dev/null
+++ b/java/12-error-handling-2/MathsTest5.java
@@ -0,0 +1,21 @@
+public class MathsTest5 {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) throws Exception {
+ Maths m = new Maths();
+ int result = 0;
+
+ try {
+ result = m.factorial(-5);
+ } catch (MathsException me) {
+ throw new Exception("My exception wrapper", me);
+ } finally {
+ System.out.println("Inside finally block");
+ }
+
+ System.out.println(result);
+ }
+
+}
diff --git a/java/12-error-handling-2/MathsTest6.java b/java/12-error-handling-2/MathsTest6.java
new file mode 100644
index 0000000..7f176aa
--- /dev/null
+++ b/java/12-error-handling-2/MathsTest6.java
@@ -0,0 +1,22 @@
+public class MathsTest6 {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ Maths m = new Maths();
+ int result = 0;
+
+ try {
+ result = m.factorial(-5);
+ } catch(MathsException me) {
+ System.err.println("Factorial method failed");
+ System.exit(1);
+ } finally {
+ System.out.println("Inside finally block");
+ }
+
+ System.out.println(result);
+ }
+
+}
diff --git a/java/12-error-handling-2/MathsTest7.java b/java/12-error-handling-2/MathsTest7.java
new file mode 100644
index 0000000..f55cfad
--- /dev/null
+++ b/java/12-error-handling-2/MathsTest7.java
@@ -0,0 +1,22 @@
+public class MathsTest7 {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ Maths m = new Maths();
+ int result = 0;
+
+ try {
+ result = m.factorial(-5);
+ } catch(MathsException me) {
+ System.err.println("Factorial method failed");
+ return;
+ } finally {
+ System.out.println("Inside finally block");
+ }
+
+ System.out.println(result);
+ }
+
+}