Skip to content

Commit 2c2571b

Browse files
author
Rajeev Kumar Singh
committed
Initial Commit
0 parents  commit 2c2571b

File tree

50 files changed

+1383
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1383
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
*.class
3+
*.iml

Readme.md

Whitespace-only changes.

java-arraylist-examples/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
*.class
3+
*.iml

java-arraylist-examples/Readme.md

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class AccessElementsFromArrayListExample {
5+
public static void main(String[] args) {
6+
List<String> topCompanies = new ArrayList<>();
7+
8+
// Check if an ArrayList is empty
9+
System.out.println("Is the topCompanies list empty? : " + topCompanies.isEmpty());
10+
11+
topCompanies.add("Google");
12+
topCompanies.add("Apple");
13+
topCompanies.add("Microsoft");
14+
topCompanies.add("Amazon");
15+
topCompanies.add("Facebook");
16+
17+
// Find the size of an ArrayList
18+
System.out.println("Here are the top " + topCompanies.size() + " companies in the world");
19+
System.out.println(topCompanies);
20+
21+
// Retrieve the element at a given index
22+
String bestCompany = topCompanies.get(0);
23+
String secondBestCompany = topCompanies.get(1);
24+
String lastCompany = topCompanies.get(topCompanies.size() - 1);
25+
26+
System.out.println("Best Company: " + bestCompany);
27+
System.out.println("Second Best Company: " + secondBestCompany);
28+
System.out.println("Last Company in the list: " + lastCompany);
29+
30+
// Modify the element at a given index
31+
topCompanies.set(4, "Walmart");
32+
System.out.println("Modified top companies list: " + topCompanies);
33+
}
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.List;
4+
5+
public class ArrayListCollectionsSortExample {
6+
public static void main(String[] args) {
7+
List<Integer> numbers = new ArrayList<>();
8+
numbers.add(13);
9+
numbers.add(7);
10+
numbers.add(18);
11+
numbers.add(5);
12+
numbers.add(2);
13+
14+
System.out.println("Before : " + numbers);
15+
16+
// Sorting an ArrayList using Collections.sort() method
17+
Collections.sort(numbers);
18+
19+
System.out.println("After : " + numbers);
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.ArrayList;
2+
import java.util.Iterator;
3+
import java.util.List;
4+
5+
public class ArrayListIteratorRemoveExample {
6+
public static void main(String[] args) {
7+
List<Integer> numbers = new ArrayList<>();
8+
numbers.add(13);
9+
numbers.add(18);
10+
numbers.add(25);
11+
numbers.add(40);
12+
13+
Iterator<Integer> numbersIterator = numbers.iterator();
14+
while (numbersIterator.hasNext()) {
15+
Integer num = numbersIterator.next();
16+
if(num % 2 != 0) {
17+
numbersIterator.remove();
18+
}
19+
}
20+
21+
System.out.println(numbers);
22+
}
23+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.Comparator;
4+
import java.util.List;
5+
6+
class Person {
7+
private String name;
8+
private Integer age;
9+
10+
public Person(String name, Integer age) {
11+
this.name = name;
12+
this.age = age;
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
23+
public Integer getAge() {
24+
return age;
25+
}
26+
27+
public void setAge(Integer age) {
28+
this.age = age;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return "{" +
34+
"name='" + name + '\'' +
35+
", age=" + age +
36+
'}';
37+
}
38+
}
39+
40+
public class ArrayListObjectSortExample {
41+
public static void main(String[] args) {
42+
List<Person> people = new ArrayList<>();
43+
people.add(new Person("Sachin", 47));
44+
people.add(new Person("Chris", 34));
45+
people.add(new Person("Rajeev", 25));
46+
people.add(new Person("David", 31));
47+
48+
System.out.println("Person List : " + people);
49+
50+
// Sort People by their Age
51+
people.sort((person1, person2) -> {
52+
return person1.getAge() - person2.getAge();
53+
});
54+
55+
// A more concise way of writing the above sorting function
56+
people.sort(Comparator.comparingInt(Person::getAge));
57+
58+
System.out.println("Sorted Person List by Age : " + people);
59+
60+
// You can also sort using Collections.sort() method by passing the custom Comparator
61+
Collections.sort(people, Comparator.comparing(Person::getName));
62+
System.out.println("Sorted Person List by Name : " + people);
63+
}
64+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.ArrayList;
2+
import java.util.Comparator;
3+
import java.util.List;
4+
5+
public class ArrayListSortExample {
6+
public static void main(String[] args) {
7+
List<String> names = new ArrayList<>();
8+
names.add("Lisa");
9+
names.add("Jennifer");
10+
names.add("Mark");
11+
names.add("David");
12+
13+
System.out.println("Names : " + names);
14+
15+
// Sort an ArrayList using its sort() method. You must pass a Comparator to the ArrayList.sort() method.
16+
names.sort(new Comparator<String>() {
17+
@Override
18+
public int compare(String name1, String name2) {
19+
return name1.compareTo(name2);
20+
}
21+
});
22+
23+
// The above `sort()` method call can also be written simply using lambda expressions
24+
names.sort((name1, name2) -> name1.compareTo(name2));
25+
26+
// Following is an even more concise solution
27+
names.sort(Comparator.naturalOrder());
28+
29+
System.out.println("Sorted Names : " + names);
30+
}
31+
}

0 commit comments

Comments
 (0)