diff options
| author | Kamal Wickramanayake <kamal@inbox.lk> | 2026-02-21 20:33:23 +0530 |
|---|---|---|
| committer | Kamal Wickramanayake <kamal@inbox.lk> | 2026-02-21 20:33:23 +0530 |
| commit | 530141c542ab3b44991f05016e66db651795e9c9 (patch) | |
| tree | abf4953878cd274d06d890c3bcad4e0230b30943 /java/10-collections | |
| parent | 3625bc6cbddd473659b3f584c0dbec515c07c786 (diff) | |
Added java/collections and java/error handling sample code
Diffstat (limited to 'java/10-collections')
| -rw-r--r-- | java/10-collections/.gitignore | 2 | ||||
| -rw-r--r-- | java/10-collections/Customer.java | 48 | ||||
| -rw-r--r-- | java/10-collections/Test1.java | 29 | ||||
| -rw-r--r-- | java/10-collections/Test2.java | 27 | ||||
| -rw-r--r-- | java/10-collections/Test3.java | 22 | ||||
| -rw-r--r-- | java/10-collections/Test4List.java | 38 | ||||
| -rw-r--r-- | java/10-collections/Test5Set.java | 30 | ||||
| -rw-r--r-- | java/10-collections/Test6Map.java | 19 | ||||
| -rw-r--r-- | java/10-collections/hashcode-contract.txt | 15 |
9 files changed, 230 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. |
