From 128680863ca3e9adcdaa3d2fb0ec9eb72c8b8e34 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Sun, 5 Dec 2021 10:01:02 +0530 Subject: [PATCH 01/27] Add arraylist java programs --- collections/arraylist/README.md | 14 +++++ .../ArrayListAddExample.java | 33 ++++++++++++ .../ArrayListExample.java | 34 ++++++++++++ .../arraylist-in-java/ArrayListDuplicate.java | 20 +++++++ .../arraylist-in-java/ArrayListExample.java | 26 ++++++++++ .../ArrayListSetExample.java | 34 ++++++++++++ .../ArrayListIterateExample1.java | 33 ++++++++++++ .../ArrayListIterateExample2.java | 34 ++++++++++++ .../ArrayListIterateExample3.java | 36 +++++++++++++ .../ArrayListIterateExample4.java | 41 +++++++++++++++ .../ArrayListIterateExample5.java | 32 ++++++++++++ .../ArrayListRemoveExample.java | 30 +++++++++++ .../ArrayListRemoveExample2.java | 32 ++++++++++++ .../ArrayListRemoveObjectExample.java | 35 +++++++++++++ .../ArrayListRemoveObjectExample2.java | 36 +++++++++++++ .../Employee.java | 52 +++++++++++++++++++ 16 files changed, 522 insertions(+) create mode 100644 collections/arraylist/README.md create mode 100644 collections/arraylist/add-element-in-arraylist/ArrayListAddExample.java create mode 100644 collections/arraylist/add-element-in-arraylist/ArrayListExample.java create mode 100644 collections/arraylist/arraylist-in-java/ArrayListDuplicate.java create mode 100644 collections/arraylist/arraylist-in-java/ArrayListExample.java create mode 100644 collections/arraylist/change-element-in-arraylist/ArrayListSetExample.java create mode 100644 collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample1.java create mode 100644 collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample2.java create mode 100644 collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample3.java create mode 100644 collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample4.java create mode 100644 collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample5.java create mode 100644 collections/arraylist/remove-element-from-arraylist/ArrayListRemoveExample.java create mode 100644 collections/arraylist/remove-element-from-arraylist/ArrayListRemoveExample2.java create mode 100644 collections/arraylist/remove-element-from-arraylist/ArrayListRemoveObjectExample.java create mode 100644 collections/arraylist/remove-element-from-arraylist/ArrayListRemoveObjectExample2.java create mode 100644 collections/arraylist/remove-element-from-arraylist/Employee.java diff --git a/collections/arraylist/README.md b/collections/arraylist/README.md new file mode 100644 index 0000000..ec09ddd --- /dev/null +++ b/collections/arraylist/README.md @@ -0,0 +1,14 @@ +# Java Collections - ArrayList in Java + +This module contains articles related to ArrayList in Java. + +## Related articles + +- [ArrayList In Java](https://coderolls.com/arraylist-in-java/) +- [How To Add An Element To ArrayList In Java?](https://coderolls.com/add-element-in-arraylist/) +- [How To Change An Element In ArrayList?](https://coderolls.com/change-element-in-arraylist/) +- [How To Remove An Element From An ArrayList?](https://coderolls.com/remove-element-from-arraylist/) +- [Iterating the ArrayList In Java](https://coderolls.com/iterating-the-arraylist-in-java/) + + +Visit [coderolls](https://coderolls.com/) to read more tutorials. diff --git a/collections/arraylist/add-element-in-arraylist/ArrayListAddExample.java b/collections/arraylist/add-element-in-arraylist/ArrayListAddExample.java new file mode 100644 index 0000000..35fe325 --- /dev/null +++ b/collections/arraylist/add-element-in-arraylist/ArrayListAddExample.java @@ -0,0 +1,33 @@ +package ArrayListAddExample; + +import java.util.ArrayList; + +/** + * A Java program to add an element at the specific index. + * + * The add(int index, E element) method will add + * an element at the specified index in ArrayList. + * + * @author Guarav Kukade at coderolls.com + * + */ +public class ArrayListAddExample { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList(); + + //adding elements to the arrayList using normal add method + arrayList.add("Gaurav"); + arrayList.add("Shyam"); + + System.out.println(arrayList);// [Gaurav, Shyam] + + // adding an element to the arrayList at index 1 + arrayList.add(1, "Saurav"); + + System.out.println(arrayList); // [Gaurav, Saurav, Shyam] + + } + +} diff --git a/collections/arraylist/add-element-in-arraylist/ArrayListExample.java b/collections/arraylist/add-element-in-arraylist/ArrayListExample.java new file mode 100644 index 0000000..4dbdbcf --- /dev/null +++ b/collections/arraylist/add-element-in-arraylist/ArrayListExample.java @@ -0,0 +1,34 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; + +/** + * A Java program to add an element to the list. + * + * The add(E e) method will add an element to + * the end of the arrayList. + * + * @author Guarav Kukade at coderolls.com + * + */ +public class ArrayListExample { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList(); + + //adding elements to the arrayList + arrayList.add("Gaurav"); + arrayList.add("Shyam"); + + System.out.println(arrayList);// [Gaurav, Shyam] + + // adding an element to the arrayList + // element will be added to the end of the arrayList + arrayList.add("Saurav"); + + System.out.println(arrayList); // [Gaurav, Shyam, Saurav] + + } + +} diff --git a/collections/arraylist/arraylist-in-java/ArrayListDuplicate.java b/collections/arraylist/arraylist-in-java/ArrayListDuplicate.java new file mode 100644 index 0000000..ac10779 --- /dev/null +++ b/collections/arraylist/arraylist-in-java/ArrayListDuplicate.java @@ -0,0 +1,20 @@ +package com.gaurav.ExProject; + +import java.util.ArrayList; + +public class ArrayListDuplicate { + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(); + + arrayList.add("Gaurav"); + arrayList.add("Shyam"); + arrayList.add("Saurav"); + arrayList.add("Samiksha"); + arrayList.add("Rina"); + arrayList.add("Rina"); + + System.out.println(arrayList); + + } +} diff --git a/collections/arraylist/arraylist-in-java/ArrayListExample.java b/collections/arraylist/arraylist-in-java/ArrayListExample.java new file mode 100644 index 0000000..8c4b065 --- /dev/null +++ b/collections/arraylist/arraylist-in-java/ArrayListExample.java @@ -0,0 +1,26 @@ +package com.gaurav.ExProject; + +import java.util.ArrayList; + +/** + * A Java program showing arraylist maintains the insertion order. + * + * @author gaurav + * + */ +public class ArrayListExample { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList(); + + arrayList.add("Gaurav"); + arrayList.add("Shyam"); + arrayList.add("Saurav"); + arrayList.add("Samiksha"); + arrayList.add("Rina"); + + System.out.println(arrayList); + } + +} diff --git a/collections/arraylist/change-element-in-arraylist/ArrayListSetExample.java b/collections/arraylist/change-element-in-arraylist/ArrayListSetExample.java new file mode 100644 index 0000000..245ffc3 --- /dev/null +++ b/collections/arraylist/change-element-in-arraylist/ArrayListSetExample.java @@ -0,0 +1,34 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; + +/** + * A java program to change an element at + * particular index in ArrayList. + * + * We can use 'public E set(int index, E element)' method + * to change an element. + * + * @author Guarav Kukade at coderolls.com + * + */ +public class ArrayListSetExample { + + public static void main(String[] args) { +ArrayList arrayList = new ArrayList(); + + //adding elements to the arrayList using normal add method + arrayList.add("Gaurav"); + arrayList.add("Shyam"); + arrayList.add("Pradnya"); + + System.out.println(arrayList);// [Gaurav, Shyam, Pradnya] + + // change an element at index 1 + arrayList.set(1, "Saurav"); + + System.out.println(arrayList); // [Gaurav, Saurav, Pradnya] + + } + +} diff --git a/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample1.java b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample1.java new file mode 100644 index 0000000..b18f159 --- /dev/null +++ b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample1.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to iterate through arraylist + * using the basic for loop. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListIterateExample1 { + + public static void main (String[] args) { + + List list = new ArrayList(); + + list.add("Monday"); + list.add("Tuesday"); + list.add("Wednesday"); + list.add("Thursday"); + list.add("Friday"); + list.add("Saturday"); + list.add("Sunday"); + + System.out.println("Iterating through ArrayList using the basic for loop.......\n"); + + for(int i =0; i list = new ArrayList(); + + list.add("Monday"); + list.add("Tuesday"); + list.add("Wednesday"); + list.add("Thursday"); + list.add("Friday"); + list.add("Saturday"); + list.add("Sunday"); + + System.out.println("Iterating through ArrayList using enhanced for loop i.e. For-each loop.......\n"); + + for(String str:list){ + //we get 'str' in for-each loop + System.out.println("Object form the ArrayList is " + str); + } + } +} \ No newline at end of file diff --git a/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample3.java b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample3.java new file mode 100644 index 0000000..9726af2 --- /dev/null +++ b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample3.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to iterate through arraylist + * using while loop. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListIterateExample3 { + + public static void main (String[] args) { + + List list = new ArrayList(); + + list.add("Monday"); + list.add("Tuesday"); + list.add("Wednesday"); + list.add("Thursday"); + list.add("Friday"); + list.add("Saturday"); + list.add("Sunday"); + + System.out.println("Iterating through ArrayList using while loop.......\n"); + + int i=0; + // while loop condition will be true till i is less the the list size + while (list.size()>i) { + System.out.println("Object form the ArrayList at "+ i + " is " + list.get(i)); + i++; + } + } +} \ No newline at end of file diff --git a/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample4.java b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample4.java new file mode 100644 index 0000000..629f3ea --- /dev/null +++ b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample4.java @@ -0,0 +1,41 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * A java program to iterate through arraylist + * using Iterator interface. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListIterateExample4 { + + public static void main (String[] args) { + + List list = new ArrayList(); + + list.add("Monday"); + list.add("Tuesday"); + list.add("Wednesday"); + list.add("Thursday"); + list.add("Friday"); + list.add("Saturday"); + list.add("Sunday"); + + System.out.println("Iterating through ArrayList using Iterator interface........\n"); + + // getting iterator + Iterator iterator = list.iterator(); + + // hasNext() returns true only if object is available at next call + while (iterator.hasNext()) { + + //next() returns obejct, we can cast it to reuqired type + String str = (String) iterator.next(); + System.out.println("Object form the ArrayList is " + str); + } + } +} \ No newline at end of file diff --git a/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample5.java b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample5.java new file mode 100644 index 0000000..fc7b89d --- /dev/null +++ b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample5.java @@ -0,0 +1,32 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to iterate through arraylist + * using forEach method and lamda expression. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListIterateExample5 { + + public static void main (String[] args) { + + List list = new ArrayList(); + + list.add("Monday"); + list.add("Tuesday"); + list.add("Wednesday"); + list.add("Thursday"); + list.add("Friday"); + list.add("Saturday"); + list.add("Sunday"); + + System.out.println("Iterating through ArrayList using forEach method and lamda expression........\n"); + + // add lambda expressionin the foreach loop + list.forEach(s -> System.out.println("Object form the ArrayList is "+ s)); + } +} \ No newline at end of file diff --git a/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveExample.java b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveExample.java new file mode 100644 index 0000000..65c87a3 --- /dev/null +++ b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveExample.java @@ -0,0 +1,30 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; + +/** + * A Java program to remove an element from ArrayList + * using the remove(Object o) method. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListRemoveExample { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList(); + + //adding elements to the arrayList using normal add method + arrayList.add("Red"); + arrayList.add("Green"); + arrayList.add("Pink"); + + System.out.println("ArrayList before removing an element"+ arrayList); // ArrayList before removing an element[Red, Green, Pink] + + //Remove "Pink" from the arrayList + arrayList.remove("Pink"); + + System.out.println("ArrayList after removing an element"+ arrayList);// ArrayList after removing an element[Red, Green] + } +} diff --git a/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveExample2.java b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveExample2.java new file mode 100644 index 0000000..b9f098a --- /dev/null +++ b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveExample2.java @@ -0,0 +1,32 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; + +/** + * A Java program to remove an element from ArrayList + * using the remove(int index) method. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListRemoveExample2 { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList(); + + //adding elements to the arrayList using normal add method + arrayList.add("Apple"); + arrayList.add("Orange"); + arrayList.add("Guava"); + arrayList.add("Banana"); + + System.out.println("ArrayList before removing an element"+ arrayList); // ArrayList before removing an element[Apple, Orange, Guava, Banana] + + //Remove the element at the specified index i.e. 2 + //Remove "Guava" from the arrayList + arrayList.remove(2); + + System.out.println("ArrayList after removing an element"+ arrayList);// ArrayList after removing an element[Apple, Orange, Banana] + } +} diff --git a/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveObjectExample.java b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveObjectExample.java new file mode 100644 index 0000000..1519655 --- /dev/null +++ b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveObjectExample.java @@ -0,0 +1,35 @@ +package com.gaurav.ExProject2; + +import java.util.ArrayList; +/** + * A Java program to remove an object from ArrayList + * using the remove(int index) method. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListRemoveObjectExample { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList(); + + //create Employee objects + Employee john = new Employee(100, "John Doe", 20000); + Employee nathasha = new Employee(101, "Natasha Putin", 28000); + Employee remo = new Employee(102, "Remo Smith", 40000); + + //adding employee objects to the arrayList using normal add method + arrayList.add(john); + arrayList.add(nathasha); + arrayList.add(remo); + + System.out.println("ArrayList before removing an employee Object:\n"+ arrayList); + + //Remove object using the remo0ve(Object o) method + //Remove employee nathasha from the arrayList + arrayList.remove(nathasha); + + System.out.println("\nArrayList after removing an employee Object:\n"+ arrayList); + } +} diff --git a/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveObjectExample2.java b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveObjectExample2.java new file mode 100644 index 0000000..7117524 --- /dev/null +++ b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveObjectExample2.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject2; + +import java.util.ArrayList; + +/** + * A Java program to remove an object from ArrayList + * using the remove(int index) method. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListRemoveObjectExample2 { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList(); + + //create Employee objects + Employee john = new Employee(100, "John Doe", 20000); + Employee nathasha = new Employee(101, "Natasha Putin", 28000); + Employee remo = new Employee(102, "Remo Smith", 40000); + + //adding employee objects to the arrayList using normal add method + arrayList.add(john); + arrayList.add(nathasha); + arrayList.add(remo); + + System.out.println("ArrayList before removing an employee Object:\n"+ arrayList); + + //Remove object using the remove(int index) method + //Remove employee nathasha from the arrayList, so pass natasha's index + arrayList.remove(1); + + System.out.println("\nArrayList after removing an employee Object:\n"+ arrayList); + } +} diff --git a/collections/arraylist/remove-element-from-arraylist/Employee.java b/collections/arraylist/remove-element-from-arraylist/Employee.java new file mode 100644 index 0000000..0e63c8f --- /dev/null +++ b/collections/arraylist/remove-element-from-arraylist/Employee.java @@ -0,0 +1,52 @@ +package com.gaurav.ExProject2; +/** + * An employee class for ArrayList remove example + * @author Gaurav Kukade at coderolls.com + * + */ +public class Employee { + + private int id; + + private String name; + + private int salary; + + // a constructor will all the fields + public Employee(int id, String name, int salary) { + this.id = id; + this.name = name; + this.salary = salary; + } + + // getters and setters for the variables + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getSalary() { + return salary; + } + + public void setSalary(int salary) { + this.salary = salary; + } + + // Override to string method to print student objects + @Override + public String toString() { + return "{"+this.getId()+", "+ this.getName()+ ", "+this.getSalary()+"}"; + } +} From 80b7dcddbfee3006f1fa96e9d7856e704f6e0fc3 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Sun, 5 Dec 2021 11:39:35 +0530 Subject: [PATCH 02/27] Add java programs to convert an arraylist to array --- .../ArrayListToArray.java | 39 +++++++++++++++++++ .../ArrayListToArray2.java | 39 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 collections/arraylist/convert-arraylist-to-array/ArrayListToArray.java create mode 100644 collections/arraylist/convert-arraylist-to-array/ArrayListToArray2.java diff --git a/collections/arraylist/convert-arraylist-to-array/ArrayListToArray.java b/collections/arraylist/convert-arraylist-to-array/ArrayListToArray.java new file mode 100644 index 0000000..1e0fd8b --- /dev/null +++ b/collections/arraylist/convert-arraylist-to-array/ArrayListToArray.java @@ -0,0 +1,39 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; + +/** + * A java program to convert an ArrayList to array using + * toArray method. + * + * toArray() method returns Object[] + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListToArray { + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(); + + //adding elements to the arrayList + arrayList.add("Meta"); + arrayList.add("Apple"); + arrayList.add("Amazon"); + arrayList.add("Netflix"); + arrayList.add("Microsoft"); + arrayList.add("Google"); + + System.out.println("ArrayList: \n"+ arrayList); + + // convert ArrayList to array + Object[] objects = arrayList.toArray(); + + System.out.println("\nArray:"); + + //we will receive the object array, so iterate on it to print each element + for(Object obj:objects) { + System.out.println(obj); + } + } +} diff --git a/collections/arraylist/convert-arraylist-to-array/ArrayListToArray2.java b/collections/arraylist/convert-arraylist-to-array/ArrayListToArray2.java new file mode 100644 index 0000000..345ef39 --- /dev/null +++ b/collections/arraylist/convert-arraylist-to-array/ArrayListToArray2.java @@ -0,0 +1,39 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; + +/** + * A java program to convert an ArrayList to array using + * toArray method. + * + * toArray(T[] a) method returns T[] + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListToArray2 { + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(); + + //adding elements to the arrayList + arrayList.add("Meta"); + arrayList.add("Apple"); + arrayList.add("Amazon"); + arrayList.add("Netflix"); + arrayList.add("Microsoft"); + arrayList.add("Google"); + + System.out.println("ArrayList: \n"+ arrayList); + + // convert ArrayList to array + // pass a new String array of the arrayList size as a param to toArray + String[] elements = arrayList.toArray(new String[arrayList.size()]); + + System.out.println("\nArray:"); + //we will receive the string array, so iterate on it to print each element + for(String str:elements) { + System.out.println(str); + } + } +} From 50e629ee1d3bdee15e127bcfb8698a8578c83679 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Sun, 5 Dec 2021 13:31:54 +0530 Subject: [PATCH 03/27] update java program to convert arraylist to array --- .../convert-arraylist-to-array/ArrayListToArray2.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/collections/arraylist/convert-arraylist-to-array/ArrayListToArray2.java b/collections/arraylist/convert-arraylist-to-array/ArrayListToArray2.java index 345ef39..88b17da 100644 --- a/collections/arraylist/convert-arraylist-to-array/ArrayListToArray2.java +++ b/collections/arraylist/convert-arraylist-to-array/ArrayListToArray2.java @@ -28,7 +28,10 @@ public static void main(String[] args) { // convert ArrayList to array // pass a new String array of the arrayList size as a param to toArray - String[] elements = arrayList.toArray(new String[arrayList.size()]); + //String[] elements = arrayList.toArray(new String[arrayList.size()]); + + //kindly read the 'Important Note' below + String[] elements = arrayList.toArray(new String[0]); System.out.println("\nArray:"); //we will receive the string array, so iterate on it to print each element From c1c9ebcdfe9f5cff041801ec2f37cf5986dde9df Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Tue, 7 Dec 2021 12:22:31 +0530 Subject: [PATCH 04/27] Add for-each loop and arraylist get method example --- .../ArrayListGetExample.java | 41 +++++++++++++++++++ .../for-each-loop/ArrayForEachExample.java | 23 +++++++++++ .../ArrayListForEachExample.java | 36 ++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 collections/arraylist/arraylist-get-method/ArrayListGetExample.java create mode 100644 java-basic/for-each-loop/ArrayForEachExample.java create mode 100644 java-basic/for-each-loop/ArrayListForEachExample.java diff --git a/collections/arraylist/arraylist-get-method/ArrayListGetExample.java b/collections/arraylist/arraylist-get-method/ArrayListGetExample.java new file mode 100644 index 0000000..b75cd5b --- /dev/null +++ b/collections/arraylist/arraylist-get-method/ArrayListGetExample.java @@ -0,0 +1,41 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to get the element by + * its index using the get() method + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListGetExample { + + public static void main(String[] args) { + + List list = new ArrayList(); + + list.add("Monday"); + list.add("Tuesday"); + list.add("Wednesday"); + list.add("Thursday"); + list.add("Friday"); + list.add("Saturday"); + list.add("Sunday"); + + System.out.println("Arraylist:"+ list); + + // get element at index 3 + String element = list.get(3); + System.out.println("\nElement at index 3 is "+ element); + + System.out.println("\nUsing get() method in for loop: \n"); + + // using the get(0 method in for loop + for(int i=0; i numbers = new ArrayList(); + + //add number to the list + numbers.add(1); + numbers.add(8); + numbers.add(8); + numbers.add(5); + numbers.add(4); + numbers.add(6); + numbers.add(3); + numbers.add(2); + numbers.add(9); + + //traversing the collection using for-each loop + for(Integer number: numbers) { + System.out.println("The number is "+ number); + } + } +} From a75cdfa90b0e0f86d64097fc8f60fe0da7bce42c Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Tue, 14 Dec 2021 19:39:31 +0530 Subject: [PATCH 05/27] Add blopost java programs --- .../ArrayListClearMethodExample.java | 38 +++++++++++++++++++ .../ArrayListContainsMethodExample.java | 34 +++++++++++++++++ .../MyArrayList.java | 37 ++++++++++++++++++ .../ReverseStringUsingRecursion.java | 23 +++++++++++ 4 files changed, 132 insertions(+) create mode 100644 collections/arraylist/arraylist-clear-method-in-java/ArrayListClearMethodExample.java create mode 100644 collections/arraylist/arraylist-contains-method/ArrayListContainsMethodExample.java create mode 100644 collections/arraylist/arraylist-removerange-method/MyArrayList.java create mode 100644 java-string/java-reverse-string-using-recursion/ReverseStringUsingRecursion.java diff --git a/collections/arraylist/arraylist-clear-method-in-java/ArrayListClearMethodExample.java b/collections/arraylist/arraylist-clear-method-in-java/ArrayListClearMethodExample.java new file mode 100644 index 0000000..46b70b0 --- /dev/null +++ b/collections/arraylist/arraylist-clear-method-in-java/ArrayListClearMethodExample.java @@ -0,0 +1,38 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to remove all the elements + * of ArrayList using the clear() method. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListClearMethodExample { + + public static void main(String[] args) { + + // create an arraylist cities + List cities = new ArrayList(); + + // add string objects in 'cities' + cities.add("New York City"); + cities.add("Los Angeles"); + cities.add("Mountain View"); + cities.add("Austin"); + cities.add("Atlanta"); + + // printing the cities + System.out.println("Before invoking the clear() method"); + System.out.println("Cities: "+ cities); //print the arraylist with elements + System.out.println("cities size: "+ cities.size()); + + //invoke the clear() method on arraylist + cities.clear(); + System.out.println("\nAfter invoking the clear() method"); + System.out.println("Cities: "+ cities); // empty arraylist + System.out.println("cities size: "+ cities.size()); + } +} diff --git a/collections/arraylist/arraylist-contains-method/ArrayListContainsMethodExample.java b/collections/arraylist/arraylist-contains-method/ArrayListContainsMethodExample.java new file mode 100644 index 0000000..a89aecf --- /dev/null +++ b/collections/arraylist/arraylist-contains-method/ArrayListContainsMethodExample.java @@ -0,0 +1,34 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; +/** + * A java program to check if the ArrayList + * contains the specified element. + * + * @author Gaurav Kukade a coderolls.com + * + */ +public class ArrayListContainsMethodExample { + + public static void main(String[] args) { + + //create an empty arraylist object 'states' + List states = new ArrayList(); + + //add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Florida"); + states.add("New Jersey"); + states.add("Washington"); + + //check if states contains florida + boolean isPresent1 = states.contains("Florida"); + System.out.println("Is Florida present in the list: "+ isPresent1); + + //check if states contains alaska + boolean isPresent2 =states.contains("Alaska"); + System.out.println("\nIs Alaska present in the list: "+ isPresent2); + } +} diff --git a/collections/arraylist/arraylist-removerange-method/MyArrayList.java b/collections/arraylist/arraylist-removerange-method/MyArrayList.java new file mode 100644 index 0000000..a6bcc81 --- /dev/null +++ b/collections/arraylist/arraylist-removerange-method/MyArrayList.java @@ -0,0 +1,37 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to remove the range of elements from + * the MyArraylist (a class which extends the ArrayList) + * using the removeRange() method. + * + * The removeRange() method is protected and is accessed in class, + * subclasses and in a package, but not public. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class MyArrayList extends ArrayList { + + public static void main(String[] args) { + + MyArrayList myArrayList = new MyArrayList(); + + myArrayList.add("Monday"); + myArrayList.add("Tuesday"); + myArrayList.add("Wednesday"); + myArrayList.add("Thursday"); + myArrayList.add("Friday"); + myArrayList.add("Saturday"); + myArrayList.add("Sunday"); + + System.out.println("Arraylist before removing range of elments:\n"+ myArrayList); + + myArrayList.removeRange(2, 4); + + System.out.println("\nArraylist after removing range of elments:\n"+ myArrayList); + } +} diff --git a/java-string/java-reverse-string-using-recursion/ReverseStringUsingRecursion.java b/java-string/java-reverse-string-using-recursion/ReverseStringUsingRecursion.java new file mode 100644 index 0000000..77a8f21 --- /dev/null +++ b/java-string/java-reverse-string-using-recursion/ReverseStringUsingRecursion.java @@ -0,0 +1,23 @@ +package ArrayListAddExample; +/** + * A java program to reverse a striong using recursion. + * + * @author gaurav + * + */ +public class ReverseStringUsingRecursion { + + public static void main(String[] args) { + + String str = "Hello"; + System.out.println(reverse(str)); + + } + + public static String reverse(String str) { + if ((null == str) || (str.length() <= 1)) { + return str; + } + return reverse(str.substring(1)) + str.charAt(0); + } +} From 3f27168a6371ca90a9f77402a39971b840719022 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Fri, 17 Dec 2021 20:03:04 +0530 Subject: [PATCH 06/27] Add blogpost example for the arraylist in java --- .../ArrayListAddAllExample.java | 41 +++++++++++++++ .../ArrayListAddAllExample2.java | 43 ++++++++++++++++ .../ArrayListIndexOfExample.java | 38 ++++++++++++++ .../ArrayListLastIndexOfExample.java | 39 ++++++++++++++ .../ArrayListRetainAllExample.java | 51 +++++++++++++++++++ .../RetainAllNullPointerExceptionExample.java | 39 ++++++++++++++ .../ArrayListSubListExample.java | 33 ++++++++++++ .../SubListMethodExceptionExample1.java | 32 ++++++++++++ .../SubListMethodExceptionExample2.java | 32 ++++++++++++ 9 files changed, 348 insertions(+) create mode 100644 collections/arraylist/arraylist-addall-method-in-java/ArrayListAddAllExample.java create mode 100644 collections/arraylist/arraylist-addall-method-in-java/ArrayListAddAllExample2.java create mode 100644 collections/arraylist/arraylist-indexOf-method/ArrayListIndexOfExample.java create mode 100644 collections/arraylist/arraylist-lastindexof-method/ArrayListLastIndexOfExample.java create mode 100644 collections/arraylist/arraylist-retainall-method/ArrayListRetainAllExample.java create mode 100644 collections/arraylist/arraylist-retainall-method/RetainAllNullPointerExceptionExample.java create mode 100644 collections/arraylist/sublist-method-in-arraylist/ArrayListSubListExample.java create mode 100644 collections/arraylist/sublist-method-in-arraylist/SubListMethodExceptionExample1.java create mode 100644 collections/arraylist/sublist-method-in-arraylist/SubListMethodExceptionExample2.java diff --git a/collections/arraylist/arraylist-addall-method-in-java/ArrayListAddAllExample.java b/collections/arraylist/arraylist-addall-method-in-java/ArrayListAddAllExample.java new file mode 100644 index 0000000..731829d --- /dev/null +++ b/collections/arraylist/arraylist-addall-method-in-java/ArrayListAddAllExample.java @@ -0,0 +1,41 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A Java program to add all elements of the specified collection + * to the end of the ArrayList + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListAddAllExample { + + public static void main(String[] args) { + + // ArrayList contains actresses + List actresses1 = new ArrayList(); + + actresses1.add("Natalie Portman"); + actresses1.add("Abigail Anderson"); + actresses1.add("Jennifer Lawrence"); + + System.out.println("Actresses collection 1: \n"+ actresses1 ); + + // ArrayList contains actresses + List actresses2 = new ArrayList(); + + + + actresses2.add("Angelina Jolie"); + actresses2.add("Scarlett Johansson"); + + System.out.println("Actresses collection 2: \n"+ actresses2); + + // Add actresses2 collections all elements to actresses1 collection using addAll()method + actresses1.addAll(actresses2); + + System.out.println("Actresses : \n"+ actresses1); + } +} diff --git a/collections/arraylist/arraylist-addall-method-in-java/ArrayListAddAllExample2.java b/collections/arraylist/arraylist-addall-method-in-java/ArrayListAddAllExample2.java new file mode 100644 index 0000000..d32e64e --- /dev/null +++ b/collections/arraylist/arraylist-addall-method-in-java/ArrayListAddAllExample2.java @@ -0,0 +1,43 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A Java program to add all elements of the specified collection + * to the ArrayList from a particular index + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListAddAllExample2 { + + public static void main(String[] args) { + + // ArrayList containing entrepreneurs + List entrepreneurs1 = new ArrayList(); + + entrepreneurs1.add("Bill Gates"); + entrepreneurs1.add("Steve Jobs"); + entrepreneurs1.add("Jeff Bezos"); + entrepreneurs1.add("Richard Branson"); + entrepreneurs1.add("Mark Cuban"); + + System.out.println("Entrepreneurs collection 1: \n"+ entrepreneurs1 ); + + // ArrayList containing entrepreneurs + List entrepreneurs2 = new ArrayList(); + + entrepreneurs2.add("Mark Zuckerberg"); + entrepreneurs2.add("Larry Page"); + entrepreneurs2.add("Larry Ellison"); + + System.out.println("Entrepreneurs collection 2: \n"+ entrepreneurs2); + + // Add entrepreneurs2 collections all elements to entrepreneurs1 collection + // starting from index 2 + entrepreneurs1.addAll(2, entrepreneurs2); + + System.out.println("Entrepreneurs : \n"+ entrepreneurs1); + } +} diff --git a/collections/arraylist/arraylist-indexOf-method/ArrayListIndexOfExample.java b/collections/arraylist/arraylist-indexOf-method/ArrayListIndexOfExample.java new file mode 100644 index 0000000..43b8260 --- /dev/null +++ b/collections/arraylist/arraylist-indexOf-method/ArrayListIndexOfExample.java @@ -0,0 +1,38 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to explain the indexOf() method + * of the arrayList class in java. + * + * @author Gaurav Kukade + */ +public class ArrayListIndexOfExample { + + public static void main(String[] args) { + + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist, Florida multiple times + states.add("California"); + states.add("Texas"); + states.add("Florida"); + states.add("Florida"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Florida"); + + int index = states.indexOf("Florida"); + + //prints index of the first occurrences of Florida i.e. 2 + System.out.println("The index of the Florida: "+ index); + + //trying get the index of element, which is not present + int index2 = states.indexOf("Alaska"); + + System.out.println("The index of the Alaska: " + index2); + } +} diff --git a/collections/arraylist/arraylist-lastindexof-method/ArrayListLastIndexOfExample.java b/collections/arraylist/arraylist-lastindexof-method/ArrayListLastIndexOfExample.java new file mode 100644 index 0000000..52ff0ab --- /dev/null +++ b/collections/arraylist/arraylist-lastindexof-method/ArrayListLastIndexOfExample.java @@ -0,0 +1,39 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to explain the indexOf() method + * of the arrayList class in java. + * + * @author Gaurav Kukade + */ +public class ArrayListLastIndexOfExample { + +public static void main(String[] args) { + + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist, Florida multiple times + states.add("California"); + states.add("Texas"); + states.add("Florida"); + states.add("Florida"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Florida"); + + int index = states.lastIndexOf("Florida"); + + //prints index of the last occurrences of Florida i.e. 6 + System.out.println("The last index of the Florida: "+ index); + + //trying get the index of element, which is not present + int index2 = states.lastIndexOf("Alaska"); + + System.out.println("The index of the Alaska: " + index2); + } + +} \ No newline at end of file diff --git a/collections/arraylist/arraylist-retainall-method/ArrayListRetainAllExample.java b/collections/arraylist/arraylist-retainall-method/ArrayListRetainAllExample.java new file mode 100644 index 0000000..265abbe --- /dev/null +++ b/collections/arraylist/arraylist-retainall-method/ArrayListRetainAllExample.java @@ -0,0 +1,51 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to to eplain the retainAll() method of the Arraylist class. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListRetainAllExample { + + public static void main(String[] args) { + + //create an empty arraylist of Integer to retain elements + List numbers1 = new ArrayList(); + + //add number to the list + numbers1.add(4); + numbers1.add(5); + numbers1.add(6); + + + //create an empty arraylist of Integer + List numbers2 = new ArrayList(); + + // add number to the list + numbers2.add(1); + numbers2.add(2); + numbers2.add(3); + numbers2.add(4); + numbers2.add(5); + numbers2.add(6); + numbers2.add(7); + numbers2.add(8); + numbers2.add(9); + + + System.out.println("Lists before the method call"); + System.out.println("numbers1: "+ numbers1); + System.out.println("numbers2: "+ numbers2); + + //retain all the elements of numbers1 in the list numbers2 + numbers2.retainAll(numbers1); + + System.out.println("\nLists after the method call"); + System.out.println("numbers1: "+ numbers1); + System.out.println("numbers2: "+ numbers2); + } +} diff --git a/collections/arraylist/arraylist-retainall-method/RetainAllNullPointerExceptionExample.java b/collections/arraylist/arraylist-retainall-method/RetainAllNullPointerExceptionExample.java new file mode 100644 index 0000000..942c2ed --- /dev/null +++ b/collections/arraylist/arraylist-retainall-method/RetainAllNullPointerExceptionExample.java @@ -0,0 +1,39 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; +/** + * A java program to show the NullPointerException in retainAll() method + * of the arrayList class. + * + * @author gaurav + * + */ +public class RetainAllNullPointerExceptionExample { + + public static void main(String[] args) { + + //create an empty arraylist of Integer to retain elements + List numbers1 = null; + + //create an empty arraylist of Integer + List numbers2 = new ArrayList(); + + // add number to the list + numbers2.add(1); + numbers2.add(2); + numbers2.add(3); + numbers2.add(4); + + System.out.println("Lists before the method call"); + System.out.println("numbers1: "+ numbers1); + System.out.println("numbers2: "+ numbers2); + + //retain all the elements of numbers1 in the list numbers2 + numbers2.retainAll(numbers1); + + System.out.println("\nLists after the method call"); + System.out.println("numbers1: "+ numbers1); + System.out.println("numbers2: "+ numbers2); + } +} diff --git a/collections/arraylist/sublist-method-in-arraylist/ArrayListSubListExample.java b/collections/arraylist/sublist-method-in-arraylist/ArrayListSubListExample.java new file mode 100644 index 0000000..8a222ed --- /dev/null +++ b/collections/arraylist/sublist-method-in-arraylist/ArrayListSubListExample.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to explain the subList() method of the ArrayList class in Java + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListSubListExample { + + public static void main(String[] args) { + + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Florida"); + states.add("New Jersey"); + states.add("Washington"); + + System.out.println("The states list: \n"+ states); + + // sublist the states list from 1 to 3 index + List statesSubList = states.subList(1, 3); + + System.out.println("\nThe states sublist from index 1 (inclusive) to 3 (exclusive): \n"+ statesSubList); + } +} diff --git a/collections/arraylist/sublist-method-in-arraylist/SubListMethodExceptionExample1.java b/collections/arraylist/sublist-method-in-arraylist/SubListMethodExceptionExample1.java new file mode 100644 index 0000000..7b9bbe3 --- /dev/null +++ b/collections/arraylist/sublist-method-in-arraylist/SubListMethodExceptionExample1.java @@ -0,0 +1,32 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to explain the subList() methods + * IndexOutOfBoundsException exception case. + * + * @author Gaurav Kukade at coderolls.com + */ +public class SubListMethodExceptionExample1 { + + public static void main(String[] args) { + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Florida"); + states.add("New Jersey"); + states.add("Washington"); + + System.out.println("The states list: \n"+ states); + + // will throw IndexOutOfBoundsException + List statesSubList = states.subList(1, 7); + + System.out.println("\nThe states sublist from index 1 (inclusive) to 3 (exclusive): \n"+ statesSubList); + } +} diff --git a/collections/arraylist/sublist-method-in-arraylist/SubListMethodExceptionExample2.java b/collections/arraylist/sublist-method-in-arraylist/SubListMethodExceptionExample2.java new file mode 100644 index 0000000..b134bc6 --- /dev/null +++ b/collections/arraylist/sublist-method-in-arraylist/SubListMethodExceptionExample2.java @@ -0,0 +1,32 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to explain the subList() methods + * IllegalArgumentException exception case. + * + * @author Gaurav Kukade at coderolls.com + */ +public class SubListMethodExceptionExample2 { + + public static void main(String[] args) { + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Florida"); + states.add("New Jersey"); + states.add("Washington"); + + System.out.println("The states list: \n"+ states); + + // will throw IllegalArgumentException + List statesSubList = states.subList(3, 1); + + System.out.println("\nThe states sublist from index 1 (inclusive) to 3 (exclusive): \n"+ statesSubList); + } +} From b5ad31343eb192ca435c59945f64a74d3006635d Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Wed, 22 Dec 2021 21:09:24 +0530 Subject: [PATCH 07/27] Add blopost example for the removeIf, removeAll and isEmpty method of the arrayList in java --- .../ArrayListIsEmptyExample.java | 44 +++++++++++++++++ .../ArrayListRemoveAllExample.java | 49 +++++++++++++++++++ ...rrayListRemoveAllNullPointerException.java | 46 +++++++++++++++++ .../ArrayListRemoveIfExample.java | 37 ++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 collections/arraylist/arraylist-isempty-method/ArrayListIsEmptyExample.java create mode 100644 collections/arraylist/arraylist-removeall-method/ArrayListRemoveAllExample.java create mode 100644 collections/arraylist/arraylist-removeall-method/ArrayListRemoveAllNullPointerException.java create mode 100644 collections/arraylist/arraylist-removeif-method/ArrayListRemoveIfExample.java diff --git a/collections/arraylist/arraylist-isempty-method/ArrayListIsEmptyExample.java b/collections/arraylist/arraylist-isempty-method/ArrayListIsEmptyExample.java new file mode 100644 index 0000000..886e31e --- /dev/null +++ b/collections/arraylist/arraylist-isempty-method/ArrayListIsEmptyExample.java @@ -0,0 +1,44 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +/** + * An example java program for an isEmpty method + * of the ArrayList class in java. + * + * @author Gaurav Kukade at coderolls + */ +public class ArrayListIsEmptyExample { + + public static void main(String[] args) { + + // create an arraylyst teams + ArrayList teams = new ArrayList(); + + // add elements to the arraylist teams + teams.add("LA Galaxy"); + teams.add("Orlando City FC"); + teams.add("Portland Timbers"); + teams.add("Columbus Crew"); + + boolean isTeamsEmpty = teams.isEmpty(); + + if(isTeamsEmpty) { + System.out.println("Teams arraylist is empty."); + } + else { + System.out.println("Teams arraylist is not empty."); + } + + // create an arraylist players + ArrayList players = new ArrayList(); + + boolean isPlayersEmpty = players.isEmpty(); + + if(isPlayersEmpty) { + System.out.println("Players arraylist is empty."); + } + else { + System.out.println("Players arraylist is not empty."); + } + } +} diff --git a/collections/arraylist/arraylist-removeall-method/ArrayListRemoveAllExample.java b/collections/arraylist/arraylist-removeall-method/ArrayListRemoveAllExample.java new file mode 100644 index 0000000..0033f0c --- /dev/null +++ b/collections/arraylist/arraylist-removeall-method/ArrayListRemoveAllExample.java @@ -0,0 +1,49 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Predicate; + +/** + * A java program showing an example for the removeAll() + * method of the ArrayList class in Java. + * + * @author gaurav + */ +public class ArrayListRemoveAllExample { + + public static void main(String[] args) { + + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist, Florida multiple times + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + states.add("Minnesota"); + states.add("Colorado"); + states.add("Missouri"); + states.add("Nevada"); + + // create another ArrayList for items to be removed + List statesToBeRemoved = new ArrayList(); + statesToBeRemoved.add("Minnesota"); + statesToBeRemoved.add("Missouri"); + statesToBeRemoved.add("Montana"); + statesToBeRemoved.add("Michigan"); + + System.out.println("The states list before the removeAll() method call: \n" + states); + + // removes all elements specified in the ArrayList statesToBeRemoved + states.removeAll(statesToBeRemoved); + + System.out.println("\nThe states list after the removeAll() method call: \n" + states); + } +} diff --git a/collections/arraylist/arraylist-removeall-method/ArrayListRemoveAllNullPointerException.java b/collections/arraylist/arraylist-removeall-method/ArrayListRemoveAllNullPointerException.java new file mode 100644 index 0000000..c6dbb56 --- /dev/null +++ b/collections/arraylist/arraylist-removeall-method/ArrayListRemoveAllNullPointerException.java @@ -0,0 +1,46 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Predicate; + +/** + * A java program showing a NullPointerException case + * for the removeAll() method of the ArrayList class in Java. + * + * @author gaurav + */ +public class ArrayListRemoveAllNullPointerException { + + public static void main(String[] args) { + + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist, Florida multiple times + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + states.add("Minnesota"); + states.add("Colorado"); + states.add("Missouri"); + states.add("Nevada"); + + // create another ArrayList with null value, + //so that removeAll will throw NullPointerException + List statesToBeRemoved =null; + + System.out.println("The states list before the removeAll() method call: \n" + states); + + // removes all elements specified in the ArrayList statesToBeRemoved + states.removeAll(statesToBeRemoved); + + System.out.println("\nThe states list after the removeAll() method call: \n" + states); + } +} diff --git a/collections/arraylist/arraylist-removeif-method/ArrayListRemoveIfExample.java b/collections/arraylist/arraylist-removeif-method/ArrayListRemoveIfExample.java new file mode 100644 index 0000000..27c0c29 --- /dev/null +++ b/collections/arraylist/arraylist-removeif-method/ArrayListRemoveIfExample.java @@ -0,0 +1,37 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Predicate; + +/** + * A java program showing an example for the removeIf() + * method of the ArrayList class in Java. + * + * @author gaurav + */ +public class ArrayListRemoveIfExample { + + public static void main(String[] args) { + + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist, Florida multiple times + states.add("California"); + states.add("Texas"); + states.add("Florida"); + states.add("Florida"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Florida"); + + System.out.println("The states list before the removeIf() call: \n" + states); + + // a predicate for the condition + Predicate p = s -> s.equals("Florida"); + states.removeIf(p); // removes all elements which satisfy the predicate p + + System.out.println("\nThe states list after the removeIf() call: \n" + states); + } +} From 2e3b92961f2a3bf05209b9bf3cd8cba718f58d89 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Fri, 24 Dec 2021 19:14:27 +0530 Subject: [PATCH 08/27] Add coding examples --- .../ArrayListEnsureCapacityExample.java | 39 +++++++++++++++++++ .../ArrayListListIteratorExample.java | 35 +++++++++++++++++ .../ArrayListListIteratorExample2.java | 36 +++++++++++++++++ .../ArrayListListIteratorException.java | 36 +++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 collections/arraylist/arraylist-ensurecapacity-method/ArrayListEnsureCapacityExample.java create mode 100644 collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorExample.java create mode 100644 collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorExample2.java create mode 100644 collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorException.java diff --git a/collections/arraylist/arraylist-ensurecapacity-method/ArrayListEnsureCapacityExample.java b/collections/arraylist/arraylist-ensurecapacity-method/ArrayListEnsureCapacityExample.java new file mode 100644 index 0000000..e23fd9e --- /dev/null +++ b/collections/arraylist/arraylist-ensurecapacity-method/ArrayListEnsureCapacityExample.java @@ -0,0 +1,39 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +/** + * An example java program for ensureCapacity(int MinCapacity) method + * of the ArrayList class in java. + * @author Gaurav at coderolls.com + * + */ +public class ArrayListEnsureCapacityExample { + + public static void main(String[] args) { + + // create an empty arraylist object 'states' + ArrayList states = new ArrayList<>(); + + // add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + states.add("Minnesota"); + states.add("Colorado"); + states.add("Missouri"); + states.add("Nevada"); + System.out.println("ArrayList Elements are: \n"+ states); + //We need to add more states, so we will ensure + //that arraylist should hold 50 elements + states.ensureCapacity(50); + + System.out.println("\nWe ensured that the arraylist should hold 50 elements."); + } + +} diff --git a/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorExample.java b/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorExample.java new file mode 100644 index 0000000..d25c5b1 --- /dev/null +++ b/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorExample.java @@ -0,0 +1,35 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.ListIterator; +/** + * An example java program about the listIterator() + * method of the arrayList. + * + * @author coderolls.com + */ +public class ArrayListListIteratorExample { + + public static void main(String[] args) { + + ArrayList states = new ArrayList<>(); + + // add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + + ListIterator itr = states.listIterator(); + + while(itr.hasNext()) { + String state = itr.next(); + System.out.println("The state :"+ state); + } + } +} diff --git a/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorExample2.java b/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorExample2.java new file mode 100644 index 0000000..3f9c41e --- /dev/null +++ b/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorExample2.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.ListIterator; +/** + * An example java program about the listIterator() + * method of the arrayList. + * + * @author coderolls.com + */ +public class ArrayListListIteratorExample2 { + + public static void main(String[] args) { + + ArrayList states = new ArrayList<>(); + + // add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + + //given index 3 iterator start from element with index 3 + ListIterator itr = states.listIterator(3); + + while(itr.hasNext()) { + String state = itr.next(); + System.out.println("The state :"+ state); + } + } +} diff --git a/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorException.java b/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorException.java new file mode 100644 index 0000000..a62a1cb --- /dev/null +++ b/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorException.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.ListIterator; +/** + * An example java program about the listIterator() + * method of the arrayList. + * + * @author coderolls.com + */ +public class ArrayListListIteratorException { + + public static void main(String[] args) { + + ArrayList states = new ArrayList<>(); + + // add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + + //given index 120, it will throw IndexOutOfBoundsException + ListIterator itr = states.listIterator(120); + + while(itr.hasNext()) { + String state = itr.next(); + System.out.println("The state :"+ state); + } + } +} From 95ce3c4d0d149bed3273fb81033dc5000e8b0cb3 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Fri, 28 Jan 2022 17:40:13 +0530 Subject: [PATCH 09/27] Add LinkedList in java blog examples --- .../linkedlist/LinkedAllowDuplicates.java | 25 +++++++++++++++++++ collections/linkedlist/LinkedAllowNull.java | 25 +++++++++++++++++++ .../linkedlist/LinkedListInsertionOrder.java | 22 ++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 collections/linkedlist/LinkedAllowDuplicates.java create mode 100644 collections/linkedlist/LinkedAllowNull.java create mode 100644 collections/linkedlist/LinkedListInsertionOrder.java diff --git a/collections/linkedlist/LinkedAllowDuplicates.java b/collections/linkedlist/LinkedAllowDuplicates.java new file mode 100644 index 0000000..03b62bb --- /dev/null +++ b/collections/linkedlist/LinkedAllowDuplicates.java @@ -0,0 +1,25 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +/** + * A Java program to prove that LinkedList Allows null values + * @author coderolls.com + * + */ +public class LinkedAllowDuplicates { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("John"); + linkedList.add(null); + linkedList.add("Mark"); + linkedList.add(null); + linkedList.add("Ruby"); + linkedList.add("Lucy"); + linkedList.add("Vikram"); + linkedList.add(null); + + System.out.println(linkedList); + } +} diff --git a/collections/linkedlist/LinkedAllowNull.java b/collections/linkedlist/LinkedAllowNull.java new file mode 100644 index 0000000..a26f0dd --- /dev/null +++ b/collections/linkedlist/LinkedAllowNull.java @@ -0,0 +1,25 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +/** + * A Java program to prove that LinkedList Allows Duplicates + * @author coderolls.com + * + */ +public class LinkedAllowNull { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("John"); + linkedList.add("Mark"); + linkedList.add("Mark"); + linkedList.add("Mark"); + linkedList.add("Ruby"); + linkedList.add("Lucy"); + linkedList.add("Vikram"); + linkedList.add("Vikram"); + + System.out.println(linkedList); + } +} diff --git a/collections/linkedlist/LinkedListInsertionOrder.java b/collections/linkedlist/LinkedListInsertionOrder.java new file mode 100644 index 0000000..14007ae --- /dev/null +++ b/collections/linkedlist/LinkedListInsertionOrder.java @@ -0,0 +1,22 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +/** + * A Java program to prove that LinkedList maintains insertion order + * @author coderolls.com + * + */ +public class LinkedListInsertionOrder { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("John"); + linkedList.add("Mark"); + linkedList.add("Ruby"); + linkedList.add("Lucy"); + linkedList.add("Vikram"); + + System.out.println(linkedList); + } +} From 1d58ac2e3c4092983348405b87c2eabafe646a1a Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Fri, 28 Jan 2022 17:42:06 +0530 Subject: [PATCH 10/27] Add LinkedList in java blog examples --- .../{ => linkedlist-in-java}/LinkedAllowDuplicates.java | 0 .../linkedlist/{ => linkedlist-in-java}/LinkedAllowNull.java | 0 .../{ => linkedlist-in-java}/LinkedListInsertionOrder.java | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename collections/linkedlist/{ => linkedlist-in-java}/LinkedAllowDuplicates.java (100%) rename collections/linkedlist/{ => linkedlist-in-java}/LinkedAllowNull.java (100%) rename collections/linkedlist/{ => linkedlist-in-java}/LinkedListInsertionOrder.java (100%) diff --git a/collections/linkedlist/LinkedAllowDuplicates.java b/collections/linkedlist/linkedlist-in-java/LinkedAllowDuplicates.java similarity index 100% rename from collections/linkedlist/LinkedAllowDuplicates.java rename to collections/linkedlist/linkedlist-in-java/LinkedAllowDuplicates.java diff --git a/collections/linkedlist/LinkedAllowNull.java b/collections/linkedlist/linkedlist-in-java/LinkedAllowNull.java similarity index 100% rename from collections/linkedlist/LinkedAllowNull.java rename to collections/linkedlist/linkedlist-in-java/LinkedAllowNull.java diff --git a/collections/linkedlist/LinkedListInsertionOrder.java b/collections/linkedlist/linkedlist-in-java/LinkedListInsertionOrder.java similarity index 100% rename from collections/linkedlist/LinkedListInsertionOrder.java rename to collections/linkedlist/linkedlist-in-java/LinkedListInsertionOrder.java From db4e4095ed4bb86609afc41f77ac453e2c28c1a3 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Fri, 28 Jan 2022 17:45:12 +0530 Subject: [PATCH 11/27] Add and update README.md for linkedlist --- collections/linkedlist/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 collections/linkedlist/README.md diff --git a/collections/linkedlist/README.md b/collections/linkedlist/README.md new file mode 100644 index 0000000..c2945f3 --- /dev/null +++ b/collections/linkedlist/README.md @@ -0,0 +1,9 @@ +# Java Collections - LinkedList in Java + +This module contains articles related to LinkedList in Java. + +## Related articles + +- [LinkedList In Java](https://coderolls.com/linkedlist-in-java/) + +Visit [coderolls](https://coderolls.com/) to read more tutorials. From 8f0441c0c972f54791905746450a74bbc33a1196 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Thu, 3 Feb 2022 18:31:01 +0530 Subject: [PATCH 12/27] Add linkedlist addfirst method example --- .../LinkedListAddFirst.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 collections/linkedlist/linkedlist-addfirst-method/LinkedListAddFirst.java diff --git a/collections/linkedlist/linkedlist-addfirst-method/LinkedListAddFirst.java b/collections/linkedlist/linkedlist-addfirst-method/LinkedListAddFirst.java new file mode 100644 index 0000000..bdcc85d --- /dev/null +++ b/collections/linkedlist/linkedlist-addfirst-method/LinkedListAddFirst.java @@ -0,0 +1,24 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +public class LinkedListAddFirst { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("Austin"); + linkedList.add("Boston"); + linkedList.add("Atlanta"); + linkedList.add("Madison"); + linkedList.add("Columbia"); + + System.out.println(linkedList); + + // add element at the beginning + linkedList.addFirst("Albany"); + System.out.println(linkedList); + + } + +} From 08e365550a5ffa625c7e0e15804f9ab26939f1e3 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Thu, 3 Feb 2022 18:56:21 +0530 Subject: [PATCH 13/27] Add linkedlist addlast method example --- .../LinkedListAddLast.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 collections/linkedlist/linkedlist-addlast-method/LinkedListAddLast.java diff --git a/collections/linkedlist/linkedlist-addlast-method/LinkedListAddLast.java b/collections/linkedlist/linkedlist-addlast-method/LinkedListAddLast.java new file mode 100644 index 0000000..30fec6f --- /dev/null +++ b/collections/linkedlist/linkedlist-addlast-method/LinkedListAddLast.java @@ -0,0 +1,24 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +public class LinkedListAddLast { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("Austin"); + linkedList.add("Boston"); + linkedList.add("Atlanta"); + linkedList.add("Madison"); + linkedList.add("Columbia"); + + System.out.println(linkedList); + + // add element at the end + linkedList.addLast("Albany"); + System.out.println(linkedList); + + } + +} From 15f92a195eff0707fce9026fbf985163051cab5b Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Fri, 4 Feb 2022 14:44:29 +0530 Subject: [PATCH 14/27] Add linkedlist to array example --- .../LinkedListToArray.java | 36 +++++++++++++++++++ .../LinkedListToArray2.java | 36 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 collections/linkedlist/convert-linkedlist-to-array/LinkedListToArray.java create mode 100644 collections/linkedlist/convert-linkedlist-to-array/LinkedListToArray2.java diff --git a/collections/linkedlist/convert-linkedlist-to-array/LinkedListToArray.java b/collections/linkedlist/convert-linkedlist-to-array/LinkedListToArray.java new file mode 100644 index 0000000..e3d4365 --- /dev/null +++ b/collections/linkedlist/convert-linkedlist-to-array/LinkedListToArray.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A java program to convert linkedlist to an array + * using the toArray() method. + * + * @author coderolls.com + * + */ +public class LinkedListToArray { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("Walmart"); + linkedList.add("Amazon"); + linkedList.add("Apple"); + linkedList.add("Microsoft"); + linkedList.add("Google"); + linkedList.add("Uber"); + linkedList.add("Tesla"); + + System.out.println("Printing the linkedList: "); + System.out.println(linkedList); + + // convert linkedList to Object[] + Object[] arr = linkedList.toArray(); + + System.out.println("\nPrinting the object array elements: "); + for(Object obj:arr) { + System.out.println(obj.toString()); + } + } +} diff --git a/collections/linkedlist/convert-linkedlist-to-array/LinkedListToArray2.java b/collections/linkedlist/convert-linkedlist-to-array/LinkedListToArray2.java new file mode 100644 index 0000000..12203c4 --- /dev/null +++ b/collections/linkedlist/convert-linkedlist-to-array/LinkedListToArray2.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A java program to convert linkedlist to an array + * using the toArray(T[] a) method. + * + * @author coderolls.com + * + */ +public class LinkedListToArray2 { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("Walmart"); + linkedList.add("Amazon"); + linkedList.add("Apple"); + linkedList.add("Microsoft"); + linkedList.add("Google"); + linkedList.add("Uber"); + linkedList.add("Tesla"); + + System.out.println("Printing the linkedList: "); + System.out.println(linkedList); + + // convert linkedList to String array + String[] arr = linkedList.toArray(new String[linkedList.size()]); + + System.out.println("\nPrinting the String array elements: "); + for(String str:arr) { + System.out.println(str); + } + } +} From 4b14095e34af92812a1d15972d7eb9a46f5a4c30 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Sat, 5 Feb 2022 12:28:20 +0530 Subject: [PATCH 15/27] Add example for add element to the linkedlist --- .../LinkedListAddExample.java | 32 ++++++++++++++++++ .../LinkedListAddExample2.java | 33 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 collections/linkedlist/add-element-in-linkedlist/LinkedListAddExample.java create mode 100644 collections/linkedlist/add-element-in-linkedlist/LinkedListAddExample2.java diff --git a/collections/linkedlist/add-element-in-linkedlist/LinkedListAddExample.java b/collections/linkedlist/add-element-in-linkedlist/LinkedListAddExample.java new file mode 100644 index 0000000..13ce6f9 --- /dev/null +++ b/collections/linkedlist/add-element-in-linkedlist/LinkedListAddExample.java @@ -0,0 +1,32 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to add an element to the LinkedList. + * + * The add(E e) method will add an element to + * the end of the linkedList. + * + * @author coderolls.com + * + */ +public class LinkedListAddExample { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + //adding elements to the linkedList + linkedList.add("John"); + linkedList.add("Peter"); + + System.out.println(linkedList);// [John, Peter] + + // adding an element to the linkedList + // element will be added to the end of the linkedList + linkedList.add("Noah"); + + System.out.println(linkedList); // [John, Peter, Noah] + } +} diff --git a/collections/linkedlist/add-element-in-linkedlist/LinkedListAddExample2.java b/collections/linkedlist/add-element-in-linkedlist/LinkedListAddExample2.java new file mode 100644 index 0000000..978faad --- /dev/null +++ b/collections/linkedlist/add-element-in-linkedlist/LinkedListAddExample2.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to add an element to the linkedlist + * at the specific index. + * + * The add(int index, E element) method will add + * an element at the specified index in LinkedList. + * + * @author coderolls.com + * + */ +public class LinkedListAddExample2 { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + //adding elements to the linkedList using normal add method + linkedList.add("John"); + linkedList.add("Peter"); + linkedList.add("Karan"); + + System.out.println(linkedList);// [John, Peter, Karan] + + // adding an element to the LinkedList at index 1 + linkedList.add(1, "Noah"); + + System.out.println(linkedList); // [John, Noah, Peter, Karan] + } +} \ No newline at end of file From 5f14b90ce38d436a9f14fb3af39940344959b415 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Tue, 8 Feb 2022 19:10:33 +0530 Subject: [PATCH 16/27] Add examples for remove an element form linkedlist blogpost --- .../LinkedListRemove.java | 33 +++++++++++++++++++ .../LinkedListRemoveFirst.java | 33 +++++++++++++++++++ .../LinkedListRemoveLast.java | 33 +++++++++++++++++++ .../LinkedListRemoveObject.java | 33 +++++++++++++++++++ .../LinkedListRemoveObjectByIndex.java | 32 ++++++++++++++++++ 5 files changed, 164 insertions(+) create mode 100644 collections/linkedlist/remove-element-from-linkedlist/LinkedListRemove.java create mode 100644 collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveFirst.java create mode 100644 collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveLast.java create mode 100644 collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveObject.java create mode 100644 collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveObjectByIndex.java diff --git a/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemove.java b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemove.java new file mode 100644 index 0000000..4c3e3a2 --- /dev/null +++ b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemove.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to remove head element from + * the linkedList using the remove() method. + * + * @author coderolls.com + * + */ +public class LinkedListRemove { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + linkedList.add("BHIM"); + linkedList.add("PayTM"); + linkedList.add("Freecharge"); + linkedList.add("PhonePe"); + linkedList.add("JioPay"); + + System.out.println("LinkedList before removing an element:"); + System.out.println(linkedList); + + // removes head(first element) i.e. removes BHIM + linkedList.remove(); + + System.out.println("\nLinkedList after removing an element using remove() method:"); + System.out.println(linkedList); + } +} diff --git a/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveFirst.java b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveFirst.java new file mode 100644 index 0000000..116fbff --- /dev/null +++ b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveFirst.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to remove first element from + * the linkedList using the removeFirst() method. + * + * @author coderolls.com + * + */ +public class LinkedListRemoveFirst { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + linkedList.add("BHIM"); + linkedList.add("PayTM"); + linkedList.add("Freecharge"); + linkedList.add("PhonePe"); + linkedList.add("JioPay"); + + System.out.println("LinkedList before removing an element:"); + System.out.println(linkedList); + + // removes first element i.e. removes BHIM + linkedList.removeFirst(); + + System.out.println("\nLinkedList after removing an element using removeFirst() method:"); + System.out.println(linkedList); + } +} diff --git a/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveLast.java b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveLast.java new file mode 100644 index 0000000..048b98a --- /dev/null +++ b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveLast.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to remove last element from + * the linkedList using the removeLast() method. + * + * @author coderolls.com + * + */ +public class LinkedListRemoveLast { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + linkedList.add("BHIM"); + linkedList.add("PayTM"); + linkedList.add("Freecharge"); + linkedList.add("PhonePe"); + linkedList.add("JioPay"); + + System.out.println("LinkedList before removing an element:"); + System.out.println(linkedList); + + // removes first element i.e. removes BHIM + linkedList.removeLast(); + + System.out.println("\nLinkedList after removing an element using removeLast() method:"); + System.out.println(linkedList); + } +} diff --git a/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveObject.java b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveObject.java new file mode 100644 index 0000000..53ac935 --- /dev/null +++ b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveObject.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to remove the specified element from + * the linkedList using the remove(Object o) method. + * + * @author coderolls.com + * + */ +public class LinkedListRemoveObject { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + linkedList.add("BHIM"); + linkedList.add("PayTM"); + linkedList.add("Freecharge"); + linkedList.add("PhonePe"); + linkedList.add("JioPay"); + + System.out.println("LinkedList before removing an element:"); + System.out.println(linkedList); + + // removes the specified element i.e. removes Freecharge + linkedList.remove("Freecharge"); + + System.out.println("\nLinkedList after removing an element using remove(Object o) method:"); + System.out.println(linkedList); + } +} diff --git a/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveObjectByIndex.java b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveObjectByIndex.java new file mode 100644 index 0000000..23da316 --- /dev/null +++ b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveObjectByIndex.java @@ -0,0 +1,32 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to remove element at the specified position i.e. index + * from the linkedList using the remove(int index) method. + * + * @author coderolls.com + */ +public class LinkedListRemoveObjectByIndex { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + linkedList.add("BHIM"); + linkedList.add("PayTM"); + linkedList.add("Freecharge"); + linkedList.add("PhonePe"); + linkedList.add("JioPay"); + + System.out.println("LinkedList before removing an element:"); + System.out.println(linkedList); + + // removes the element at index 3 i.e. removes PhonePe + linkedList.remove(3); + + System.out.println("\nLinkedList after removing an element using remove(int index) method:"); + System.out.println(linkedList); + } +} From 5d1164c61d7bbcc133fd4e9dffc18b1569c48ecd Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Wed, 9 Feb 2022 08:20:28 +0530 Subject: [PATCH 17/27] Add example for LinkedList contains() method --- .../LinkedListContains.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 collections/linkedlist/check-if-linkedlist-contains-an-element/LinkedListContains.java diff --git a/collections/linkedlist/check-if-linkedlist-contains-an-element/LinkedListContains.java b/collections/linkedlist/check-if-linkedlist-contains-an-element/LinkedListContains.java new file mode 100644 index 0000000..4fb2e0b --- /dev/null +++ b/collections/linkedlist/check-if-linkedlist-contains-an-element/LinkedListContains.java @@ -0,0 +1,40 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to check if the element is present + * in the linkedList using the contains(Object o) method. + * + * @author coderolls.com + * + */ +public class LinkedListContains { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + linkedList.add("Uber"); + linkedList.add("Ola"); + linkedList.add("Didi"); + linkedList.add("Lyft"); + linkedList.add("Yandex"); + + System.out.println("LinkedList is as follows: \n"+ linkedList); + + + // checks if the linkedList contains "Ola" + // returns true if present + boolean isOlaPresnet = linkedList.contains("Ola"); // true + + System.out.println("\nCheck if linkedList contains Ola: "+ isOlaPresnet); + + // checks if the linkedList contains "Grab" + // returns true if present + boolean isGrabPresnet = linkedList.contains("Grab"); // false + + System.out.println("\nCheck if linkedList contains Grab: "+ isGrabPresnet); + + } +} From 6011fa4808f1e4c582f37264c9de5dcf109bcae5 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Thu, 10 Feb 2022 16:34:09 +0530 Subject: [PATCH 18/27] Add get element from linkedlist example --- .../LinkedListGetElement.java | 30 +++++++++++++++++++ .../LinkedListGetFirst.java | 30 +++++++++++++++++++ .../LinkedListGetLast.java | 30 +++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetElement.java create mode 100644 collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetFirst.java create mode 100644 collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetLast.java diff --git a/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetElement.java b/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetElement.java new file mode 100644 index 0000000..62fe1b9 --- /dev/null +++ b/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetElement.java @@ -0,0 +1,30 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to get an element from + * the linkedlist by it's index. + * + * @author coderolls.com + */ +public class LinkedListGetElement { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("Adidas"); + linkedList.add("Air"); + linkedList.add("Nike"); + linkedList.add("Puma"); + linkedList.add("Reebok"); + + System.out.println("LinkedList: "); + System.out.println(linkedList); + + // get element at index 3 + String element = linkedList.get(3); // Puma + + System.out.println("\nElement at index 3 is: "+ element); + } +} diff --git a/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetFirst.java b/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetFirst.java new file mode 100644 index 0000000..883797e --- /dev/null +++ b/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetFirst.java @@ -0,0 +1,30 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to get the first element from + * the linkedlist using getFirst() method. + * + * @author coderolls.com + */ +public class LinkedListGetFirst { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("Adidas"); + linkedList.add("Air"); + linkedList.add("Nike"); + linkedList.add("Puma"); + linkedList.add("Reebok"); + + System.out.println("LinkedList: "); + System.out.println(linkedList); + + // get first element + String element = linkedList.getFirst(); // Adidas + + System.out.println("\nFirst element of the linkedList is: "+ element); + } +} diff --git a/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetLast.java b/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetLast.java new file mode 100644 index 0000000..a72f44e --- /dev/null +++ b/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetLast.java @@ -0,0 +1,30 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to get the last element of + * the linkedlist using getLast() method. + * + * @author coderolls.com + */ +public class LinkedListGetLast { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("Adidas"); + linkedList.add("Air"); + linkedList.add("Nike"); + linkedList.add("Puma"); + linkedList.add("Reebok"); + + System.out.println("LinkedList: "); + System.out.println(linkedList); + + // get first element + String element = linkedList.getLast(); // Adidas + + System.out.println("\nLast element of the linkedList is: "+ element); + } +} From 5837e94432f912a026ff51b6e1e122ea2a8014ba Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Thu, 10 Feb 2022 20:12:01 +0530 Subject: [PATCH 19/27] Add example for get an index of element from linkedlist blog --- .../LinkedListIndexOf.java | 38 +++++++++++++++++++ .../LinkedListLastIndexOf.java | 38 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListIndexOf.java create mode 100644 collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java diff --git a/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListIndexOf.java b/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListIndexOf.java new file mode 100644 index 0000000..98df49f --- /dev/null +++ b/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListIndexOf.java @@ -0,0 +1,38 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to get the first index of the element from + * the linkedlist using indexOf(Object o) method. + * + * @author coderolls.com + */ +public class LinkedListIndexOf { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("PepsiCo"); + linkedList.add("DrPepper"); + linkedList.add("GoldSpot"); + linkedList.add("GoldSpot"); + linkedList.add("CocaCola"); + linkedList.add("Moxie"); + linkedList.add("GoldSpot"); + linkedList.add("Moxie"); + + System.out.println("LinkedList: "); + System.out.println(linkedList); + + // get first index of GoldSpot + int index = linkedList.indexOf("GoldSpot"); // 2 + + System.out.println("\nIndex of the GoldSpot in linkedList is: "+ index); + + // get first index of Moxie + int indexMoxie = linkedList.indexOf("Moxie"); // 5 + + System.out.println("\nIndex of the Moxie in linkedList is: "+ indexMoxie); + } +} diff --git a/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java b/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java new file mode 100644 index 0000000..6d3f3a1 --- /dev/null +++ b/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java @@ -0,0 +1,38 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to get the last index of the element from + * the linkedlist using lastIndexOf(Object o) method. + * + * @author coderolls.com + */ +public class LinkedListLastIndexOf { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("PepsiCo"); + linkedList.add("DrPepper"); + linkedList.add("GoldSpot"); + linkedList.add("GoldSpot"); + linkedList.add("CocaCola"); + linkedList.add("Moxie"); + linkedList.add("GoldSpot"); + linkedList.add("Moxie"); + + System.out.println("LinkedList: "); + System.out.println(linkedList); + + // get first index of GoldSpot + int index = linkedList.lastIndexOf("GoldSpot"); // 6 + + System.out.println("\nIndex of the GoldSpot in linkedList is: "+ index); + + // get first index of Moxie + int indexMoxie = linkedList.lastIndexOf("Moxie"); // 7 + + System.out.println("\nIndex of the Moxie in linkedList is: "+ indexMoxie); + } +} From 7d76ced3b76a98b18167f907e51ede8dda4c96d5 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Thu, 10 Feb 2022 20:13:17 +0530 Subject: [PATCH 20/27] Add example for get an index of element from linkedlist blog --- .../LinkedListLastIndexOf.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java b/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java index 6d3f3a1..6d8b452 100644 --- a/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java +++ b/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java @@ -25,12 +25,12 @@ public static void main(String[] args) { System.out.println("LinkedList: "); System.out.println(linkedList); - // get first index of GoldSpot + // get last index of GoldSpot int index = linkedList.lastIndexOf("GoldSpot"); // 6 System.out.println("\nIndex of the GoldSpot in linkedList is: "+ index); - // get first index of Moxie + // get last index of Moxie int indexMoxie = linkedList.lastIndexOf("Moxie"); // 7 System.out.println("\nIndex of the Moxie in linkedList is: "+ indexMoxie); From 49c4ee20da18a6da5dbf406f939c803b6cec5a63 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Fri, 11 Feb 2022 05:52:00 +0530 Subject: [PATCH 21/27] Updated blogpost example --- .../LinkedListIndexOf.java | 9 +++++++-- .../LinkedListLastIndexOf.java | 11 ++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListIndexOf.java b/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListIndexOf.java index 98df49f..aad7aaf 100644 --- a/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListIndexOf.java +++ b/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListIndexOf.java @@ -26,9 +26,14 @@ public static void main(String[] args) { System.out.println(linkedList); // get first index of GoldSpot - int index = linkedList.indexOf("GoldSpot"); // 2 + int index = linkedList.indexOf("DrPepper"); // 1 - System.out.println("\nIndex of the GoldSpot in linkedList is: "+ index); + System.out.println("\nIndex of the DrPepper in linkedList is: "+ index); + + // get first index of GoldSpot + int indexGS = linkedList.indexOf("GoldSpot"); // 2 + + System.out.println("\nIndex of the GoldSpot in linkedList is: "+ indexGS); // get first index of Moxie int indexMoxie = linkedList.indexOf("Moxie"); // 5 diff --git a/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java b/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java index 6d8b452..b15b3ff 100644 --- a/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java +++ b/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java @@ -26,13 +26,18 @@ public static void main(String[] args) { System.out.println(linkedList); // get last index of GoldSpot - int index = linkedList.lastIndexOf("GoldSpot"); // 6 + int index = linkedList.lastIndexOf("DrPepper"); // 1 - System.out.println("\nIndex of the GoldSpot in linkedList is: "+ index); + System.out.println("\nLast index of the DrPepper in linkedList is: "+ index); + + // get last index of GoldSpot + int indexGS = linkedList.lastIndexOf("GoldSpot"); // 6 + + System.out.println("\nLast index of the GoldSpot in linkedList is: "+ indexGS); // get last index of Moxie int indexMoxie = linkedList.lastIndexOf("Moxie"); // 7 - System.out.println("\nIndex of the Moxie in linkedList is: "+ indexMoxie); + System.out.println("\nLast index of the Moxie in linkedList is: "+ indexMoxie); } } From bad40426aafc80fb3556d9a83a22d2939b171fb9 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Tue, 15 Feb 2022 11:40:17 +0530 Subject: [PATCH 22/27] Add linkedlist set method example --- .../LinkedListSet.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 collections/linkedlist/set-element-at-an-index-in-linkedlist/LinkedListSet.java diff --git a/collections/linkedlist/set-element-at-an-index-in-linkedlist/LinkedListSet.java b/collections/linkedlist/set-element-at-an-index-in-linkedlist/LinkedListSet.java new file mode 100644 index 0000000..251898b --- /dev/null +++ b/collections/linkedlist/set-element-at-an-index-in-linkedlist/LinkedListSet.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A java program to set an element at specified index + * using the set(int index, E element) method. + * + * @author coderolls.com + */ +public class LinkedListSet { + + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + + linkedList.add("IN"); + linkedList.add("US"); + linkedList.add("FR"); + linkedList.add("JP"); + linkedList.add("CN"); + linkedList.add("RU"); + + System.out.println("LinkedList before setting an element at index 4:"); + System.out.println(linkedList); + + // replaces CN with BR + // returns the previously present element at index 4 i.e CN + String previousElement = linkedList.set(4, "BR"); + + System.out.println("\nLinkedList after setting an element BR at index 4:"); + System.out.println(linkedList); + + System.out.println("\nPreviously present element at index 4:"); + System.out.println(previousElement); + } +} From 4677defd1e5725dab6b454b6db65e800328257ba Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Wed, 16 Feb 2022 14:23:52 +0530 Subject: [PATCH 23/27] Add iterate linkedlist example --- .../LinkedListForEachMethod.java | 30 +++++++++++++ .../LinkedListForLoop.java | 38 ++++++++++++++++ .../LinkedListForeachLoop.java | 33 ++++++++++++++ .../LinkedListIterator.java | 37 ++++++++++++++++ .../LinkedListWhileLoop.java | 44 +++++++++++++++++++ 5 files changed, 182 insertions(+) create mode 100644 collections/linkedlist/iterate-linkedlist-in-java/LinkedListForEachMethod.java create mode 100644 collections/linkedlist/iterate-linkedlist-in-java/LinkedListForLoop.java create mode 100644 collections/linkedlist/iterate-linkedlist-in-java/LinkedListForeachLoop.java create mode 100644 collections/linkedlist/iterate-linkedlist-in-java/LinkedListIterator.java create mode 100644 collections/linkedlist/iterate-linkedlist-in-java/LinkedListWhileLoop.java diff --git a/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForEachMethod.java b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForEachMethod.java new file mode 100644 index 0000000..f9889c4 --- /dev/null +++ b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForEachMethod.java @@ -0,0 +1,30 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +import java.util.List; + +/** + * A java program to iterate over linkedList + * using the java 8 foreach method. + * + * @author coderolls.com + */ +public class LinkedListForEachMethod { + + public static void main(String[] args) { + + List linkedList = new LinkedList(); + + linkedList.add("Mango"); + linkedList.add("Banana"); + linkedList.add("Grapes"); + linkedList.add("Watermelon"); + linkedList.add("Apple"); + linkedList.add("Avocado"); + + System.out.println("Iterating through LinkedList using forEach method and lamda expression........\n"); + + // add lambda expression in the foreach method + linkedList.forEach(s -> System.out.println("Object form the LinkedList is " + s)); + } +} diff --git a/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForLoop.java b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForLoop.java new file mode 100644 index 0000000..92428da --- /dev/null +++ b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForLoop.java @@ -0,0 +1,38 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +import java.util.List; + +/** + * A java program to iterate over linkedList + * using the basic for loop. + * + * @author coderolls.com + * + */ +public class LinkedListForLoop { + + public static void main(String[] args) { + + List linkedList = new LinkedList(); + + linkedList.add("Mango"); + linkedList.add("Banana"); + linkedList.add("Grapes"); + linkedList.add("Watermelon"); + linkedList.add("Apple"); + linkedList.add("Avocado"); + + System.out.println("Iterating through LinkedList using the basic for loop!\n"); + + int len = linkedList.size(); + + // iterate over linkedList using basic for loop + for (int i = 0; i < len; i++) { + + // get String object from linkedList at index i + String str = linkedList.get(i); + System.out.println("Object form the LinkedList at " + i + " is " + str); + } + } +} diff --git a/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForeachLoop.java b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForeachLoop.java new file mode 100644 index 0000000..cd4f7eb --- /dev/null +++ b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForeachLoop.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +import java.util.List; + +/** + * A java program to iterate over linkedList + * using the foreach loop. + * + * @author coderolls.com + * + */ +public class LinkedListForeachLoop { + + public static void main(String[] args) { + + List linkedList = new LinkedList(); + + linkedList.add("Mango"); + linkedList.add("Banana"); + linkedList.add("Grapes"); + linkedList.add("Watermelon"); + linkedList.add("Apple"); + linkedList.add("Avocado"); + + System.out.println("Iterating through LinkedList using the foreach loop!\n"); + + // iterate over linkedList using foreach loop + for (String str: linkedList) { + System.out.println("Object form the LinkedList is "+str); + } + } +} diff --git a/collections/linkedlist/iterate-linkedlist-in-java/LinkedListIterator.java b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListIterator.java new file mode 100644 index 0000000..163e27a --- /dev/null +++ b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListIterator.java @@ -0,0 +1,37 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +/** + * A java program to iterate through LinkedList using Iterator interface. + * + * @author coderolls.com + * + */ +public class LinkedListIterator { + + public static void main(String[] args) { + List linkedList = new LinkedList(); + + linkedList.add("Mango"); + linkedList.add("Banana"); + linkedList.add("Grapes"); + linkedList.add("Watermelon"); + linkedList.add("Apple"); + linkedList.add("Avocado"); + System.out.println("Iterating through LinkedList using Iterator interface........\n"); + + // getting iterator + Iterator iterator = linkedList.iterator(); + + // hasNext() returns true only if object is available at next call + while (iterator.hasNext()) { + + // next() returns obejct, we can cast it to reuqired type + String str = iterator.next(); + System.out.println("Object form the LinkedList is " + str); + } + } +} \ No newline at end of file diff --git a/collections/linkedlist/iterate-linkedlist-in-java/LinkedListWhileLoop.java b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListWhileLoop.java new file mode 100644 index 0000000..3293863 --- /dev/null +++ b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListWhileLoop.java @@ -0,0 +1,44 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +import java.util.List; + +/** + * A java program to iterate over linkedList + * using the while loop. + * + * @author coderolls.com + * + */ +public class LinkedListWhileLoop { + + public static void main(String[] args) { + + List linkedList = new LinkedList(); + + linkedList.add("Mango"); + linkedList.add("Banana"); + linkedList.add("Grapes"); + linkedList.add("Watermelon"); + linkedList.add("Apple"); + linkedList.add("Avocado"); + + System.out.println("Iterating through LinkedList using the while loop!\n"); + + int len = linkedList.size(); + + // iterate over linkedList using while loop + int i =0; + + while(i Date: Tue, 8 Mar 2022 17:42:43 +0530 Subject: [PATCH 24/27] Add linkedlist listiterator example --- .../LinkedListListIteratorExample.java | 36 +++++++++++++++++++ .../LinkedListListIteratorException.java | 36 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 collections/linkedlist/linkedlist-listiterator/LinkedListListIteratorExample.java create mode 100644 collections/linkedlist/linkedlist-listiterator/LinkedListListIteratorException.java diff --git a/collections/linkedlist/linkedlist-listiterator/LinkedListListIteratorExample.java b/collections/linkedlist/linkedlist-listiterator/LinkedListListIteratorExample.java new file mode 100644 index 0000000..7216f99 --- /dev/null +++ b/collections/linkedlist/linkedlist-listiterator/LinkedListListIteratorExample.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +import java.util.ListIterator; +/** + * An example java program about the listIterator(int index) + * method of the linkedList. + * + * @author coderolls.com + */ +public class LinkedListListIteratorExample { + + public static void main(String[] args) { + + LinkedList states = new LinkedList<>(); + + // add state in the linkedList + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + + //given index 3, iterator start from element with index 3 + ListIterator itr = states.listIterator(3); + System.out.println("Iterating the list from index 3\n"); + while(itr.hasNext()) { + String state = itr.next(); + System.out.println("The state :"+ state); + } + } +} \ No newline at end of file diff --git a/collections/linkedlist/linkedlist-listiterator/LinkedListListIteratorException.java b/collections/linkedlist/linkedlist-listiterator/LinkedListListIteratorException.java new file mode 100644 index 0000000..4eeb2ad --- /dev/null +++ b/collections/linkedlist/linkedlist-listiterator/LinkedListListIteratorException.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +import java.util.ListIterator; +/** + * An example java program about the listIterator() + * method of the linkedList. + * + * @author coderolls.com + */ +public class LinkedListListIteratorException { + + public static void main(String[] args) { + + LinkedList states = new LinkedList<>(); + + // add state in the linkedList + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + + //given index 120, it will throw IndexOutOfBoundsException + ListIterator itr = states.listIterator(120); + + while(itr.hasNext()) { + String state = itr.next(); + System.out.println("The state :"+ state); + } + } +} \ No newline at end of file From f094820f2b13e82c60f5e52783e4befe675b9fd2 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Sat, 8 Oct 2022 17:04:07 +0530 Subject: [PATCH 25/27] Add largest element in an array examples --- README.md | 2 +- .../LargestElementInArray.java | 32 +++++++++++++++++++ .../LargestElementInArrayUsingArrays.java | 28 ++++++++++++++++ .../LargestElementInArrayUsingStream.java | 26 +++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 java-programs/largest-element-in-an-array/LargestElementInArray.java create mode 100644 java-programs/largest-element-in-an-array/LargestElementInArrayUsingArrays.java create mode 100644 java-programs/largest-element-in-an-array/LargestElementInArrayUsingStream.java diff --git a/README.md b/README.md index 77eb236..1079472 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# Visit coderolls.com +# Visit [coderolls.com](https://coderolls.com) diff --git a/java-programs/largest-element-in-an-array/LargestElementInArray.java b/java-programs/largest-element-in-an-array/LargestElementInArray.java new file mode 100644 index 0000000..a4bb4d9 --- /dev/null +++ b/java-programs/largest-element-in-an-array/LargestElementInArray.java @@ -0,0 +1,32 @@ +package com.coderolls.JavaPrograms; +/** + * A Java program to find the largest number in an array + * using for loop iteration. + * + * @author coderolls.com + * + */ +public class LargestElementInArray { + + public static void main(String[] args) { + int[] arr = {2, 5, 9, 8, 11}; + + int largestElement = getLargest(arr); + System.out.println("Largest element in an array 'arr' is :"+ largestElement); + + } + + private static int getLargest(int[] arr) { + int n = arr.length; + int largest = arr[0]; + + for(int i=0; iarr[0]) { + largest = arr[i]; + } + } + return largest; + } +} diff --git a/java-programs/largest-element-in-an-array/LargestElementInArrayUsingArrays.java b/java-programs/largest-element-in-an-array/LargestElementInArrayUsingArrays.java new file mode 100644 index 0000000..11be28b --- /dev/null +++ b/java-programs/largest-element-in-an-array/LargestElementInArrayUsingArrays.java @@ -0,0 +1,28 @@ +package com.coderolls.JavaPrograms; + +import java.util.Arrays; + +/** + * A Java program to find the largest number in an array + * using Arrays.sort(). + * + * Arrays.sort() sort the array in natural sorting order + * + * @author coderolls.com + * + */ +public class LargestElementInArrayUsingArrays { + + public static void main(String[] args) { + int[] arr = {2, 5, 9, 8, 11}; + + int largestElement = getLargest(arr); + System.out.println("Largest element in an array 'arr' using Array.sort() is :"+ largestElement); + + } + + private static int getLargest(int[] arr) { + Arrays.sort(arr); + return arr[arr.length-1]; + } +} diff --git a/java-programs/largest-element-in-an-array/LargestElementInArrayUsingStream.java b/java-programs/largest-element-in-an-array/LargestElementInArrayUsingStream.java new file mode 100644 index 0000000..eb6299a --- /dev/null +++ b/java-programs/largest-element-in-an-array/LargestElementInArrayUsingStream.java @@ -0,0 +1,26 @@ +package com.coderolls.JavaPrograms; + +import java.util.Arrays; +/** + * A Java program to find the largest number in an array + * using Java 8 Streams API. + * + * max() returns the maximum element of this stream + * + * @author coderolls.com + * + */ +public class LargestElementInArrayUsingStream { + + public static void main(String[] args) { + int[] arr = {2, 5, 9, 8, 11}; + + int largestElement = getLargest(arr); + System.out.println("Largest element in an array 'arr' using Java 8 Streams API is :"+ largestElement); + } + + private static int getLargest(int[] arr) { + int largest = Arrays.stream(arr).max().getAsInt(); + return largest; + } +} From cda18d7544c5949baa98a1ab6854d5794149b686 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Sat, 8 Oct 2022 19:00:21 +0530 Subject: [PATCH 26/27] Add examples for second laregst element in an array --- .../SecondLargestElementInArray.java | 40 +++++++++++++++++++ ...econdLargestElementInArrayUsingArrays.java | 25 ++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 java-programs/second-largest-element-in-an-array/SecondLargestElementInArray.java create mode 100644 java-programs/second-largest-element-in-an-array/SecondLargestElementInArrayUsingArrays.java diff --git a/java-programs/second-largest-element-in-an-array/SecondLargestElementInArray.java b/java-programs/second-largest-element-in-an-array/SecondLargestElementInArray.java new file mode 100644 index 0000000..70b0b97 --- /dev/null +++ b/java-programs/second-largest-element-in-an-array/SecondLargestElementInArray.java @@ -0,0 +1,40 @@ +package com.coderolls.JavaPrograms.second_largest; +/** + * A Java program to find the second largest number in an array + * by iterating over an array using for loop. + * + * @author coderolls.com + */ +public class SecondLargestElementInArray { + + public static void main(String[] args) { + int[] arr = {2, 5, 9, 8, 11, 18, 13}; + + int secondLargest = getSecondLargest(arr); + System.out.println("The second largest element in an array 'arr' is :"+ secondLargest); + } + + private static int getSecondLargest(int[] arr) { + int n =arr.length; + int largest =arr[0]; + int secondLargest = -1; + + for(int i=0; ilargest) { + //if you found the new largest, + //copy current largest to second largest and + //copy current element arr[i] to largest + secondLargest = largest; + largest = arr[i]; + }else if(arr[i]!=largest) { + // if the current element arr[i] is not the largest and + // still larger than the current secondLargest + // then copy it to secondLargest + if(arr[i]>secondLargest) { + secondLargest = arr[i]; + } + } + } + return secondLargest; + } +} diff --git a/java-programs/second-largest-element-in-an-array/SecondLargestElementInArrayUsingArrays.java b/java-programs/second-largest-element-in-an-array/SecondLargestElementInArrayUsingArrays.java new file mode 100644 index 0000000..dcd0803 --- /dev/null +++ b/java-programs/second-largest-element-in-an-array/SecondLargestElementInArrayUsingArrays.java @@ -0,0 +1,25 @@ +package com.coderolls.JavaPrograms.second_largest; + +import java.util.Arrays; + +/** + * A Java program to find the second largest number in an array + * using Arrays.sort() method. + * + * @author coderolls.com + */ +public class SecondLargestElementInArrayUsingArrays { + + public static void main(String[] args) { + int[] arr = {2, 5, 9, 8, 11, 18, 13}; + + int secondLargest = getSecondLargest(arr); + System.out.println("The second largest element in an array 'arr' is using Arrays.sort() :"+ secondLargest); + } + + private static int getSecondLargest(int[] arr) { + Arrays.sort(arr); + // return second largest, so length-2 + return arr[arr.length-2]; + } +} From 707c930a55746aca155f5632e1f5a9bf327776d5 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Sat, 8 Oct 2022 19:14:47 +0530 Subject: [PATCH 27/27] Update sysout line in examples --- .../LargestElementInArrayUsingArrays.java | 4 ++-- .../LargestElementInArrayUsingStream.java | 3 ++- .../SecondLargestElementInArray.java | 3 ++- .../SecondLargestElementInArrayUsingArrays.java | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/java-programs/largest-element-in-an-array/LargestElementInArrayUsingArrays.java b/java-programs/largest-element-in-an-array/LargestElementInArrayUsingArrays.java index 11be28b..d061699 100644 --- a/java-programs/largest-element-in-an-array/LargestElementInArrayUsingArrays.java +++ b/java-programs/largest-element-in-an-array/LargestElementInArrayUsingArrays.java @@ -17,8 +17,8 @@ public static void main(String[] args) { int[] arr = {2, 5, 9, 8, 11}; int largestElement = getLargest(arr); - System.out.println("Largest element in an array 'arr' using Array.sort() is :"+ largestElement); - + System.out.println("Largest element in an array 'arr' " + + "using Array.sort() is :"+ largestElement); } private static int getLargest(int[] arr) { diff --git a/java-programs/largest-element-in-an-array/LargestElementInArrayUsingStream.java b/java-programs/largest-element-in-an-array/LargestElementInArrayUsingStream.java index eb6299a..311ee0d 100644 --- a/java-programs/largest-element-in-an-array/LargestElementInArrayUsingStream.java +++ b/java-programs/largest-element-in-an-array/LargestElementInArrayUsingStream.java @@ -16,7 +16,8 @@ public static void main(String[] args) { int[] arr = {2, 5, 9, 8, 11}; int largestElement = getLargest(arr); - System.out.println("Largest element in an array 'arr' using Java 8 Streams API is :"+ largestElement); + System.out.println("Largest element in an array 'arr'" + + "using Java 8 Streams API is :"+ largestElement); } private static int getLargest(int[] arr) { diff --git a/java-programs/second-largest-element-in-an-array/SecondLargestElementInArray.java b/java-programs/second-largest-element-in-an-array/SecondLargestElementInArray.java index 70b0b97..170298a 100644 --- a/java-programs/second-largest-element-in-an-array/SecondLargestElementInArray.java +++ b/java-programs/second-largest-element-in-an-array/SecondLargestElementInArray.java @@ -11,7 +11,8 @@ public static void main(String[] args) { int[] arr = {2, 5, 9, 8, 11, 18, 13}; int secondLargest = getSecondLargest(arr); - System.out.println("The second largest element in an array 'arr' is :"+ secondLargest); + System.out.println("The second largest element in " + + "an array 'arr'is :"+ secondLargest); } private static int getSecondLargest(int[] arr) { diff --git a/java-programs/second-largest-element-in-an-array/SecondLargestElementInArrayUsingArrays.java b/java-programs/second-largest-element-in-an-array/SecondLargestElementInArrayUsingArrays.java index dcd0803..5e3aa18 100644 --- a/java-programs/second-largest-element-in-an-array/SecondLargestElementInArrayUsingArrays.java +++ b/java-programs/second-largest-element-in-an-array/SecondLargestElementInArrayUsingArrays.java @@ -14,7 +14,8 @@ public static void main(String[] args) { int[] arr = {2, 5, 9, 8, 11, 18, 13}; int secondLargest = getSecondLargest(arr); - System.out.println("The second largest element in an array 'arr' is using Arrays.sort() :"+ secondLargest); + System.out.println("The second largest element in" + + "an array 'arr' is using Arrays.sort() :"+ secondLargest); } private static int getSecondLargest(int[] arr) { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy