Skip to content

Commit 3d68ab1

Browse files
committed
Added collections, exception handling, interface and abstract class examples
1 parent 164de42 commit 3d68ab1

31 files changed

+907
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.stacktips.collections.list.arraylist;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
import java.util.stream.Stream;
9+
10+
public class _1a_ArrayList {
11+
public static void main(String[] args) {
12+
//Using the Default Constructor
13+
List<String> list1 = new ArrayList<>();
14+
list1.add("Apple");
15+
list1.add("Banana");
16+
17+
// Using Constructor with Initial Capacity
18+
List<String> list2 = new ArrayList<>(20);
19+
list2.add("Apple");
20+
list2.add("Banana");
21+
22+
//Initializing array collections.list using the anonymous inner class method
23+
List<String> list3 = new ArrayList<String>() {{
24+
add("Apple");
25+
add("Banana");
26+
add("Orange");
27+
}};
28+
29+
// fixed-size collections.list from the elements of another collection
30+
List<String> list4 = Arrays.asList("Apple", "Banana", "Orange");
31+
List<String> list5 = new ArrayList<>(list4);
32+
33+
// Using Arrays.asList method
34+
List<String> list6 = new ArrayList<>(Arrays.asList("Apple", "Banana", "Orange"));
35+
36+
// Using Collections.addAll method
37+
List<String> list7 = new ArrayList<>();
38+
Collections.addAll(list7, "Apple", "Banana", "Orange");
39+
40+
// Using Java8 streams and collectors
41+
List<String> list8 = Stream.of("Apple", "Banana", "Orange")
42+
.collect(Collectors.toCollection(ArrayList::new));
43+
44+
System.out.println(list1.get(1));
45+
46+
System.out.println(list2.contains("Banana"));
47+
}
48+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.stacktips.collections.list.arraylist;
2+
3+
import java.util.List;
4+
5+
public class _2a_ImmutableList {
6+
public static void main(String[] args) {
7+
List<String> immutableList = List.of("A", "B", "C");
8+
System.out.println(immutableList);
9+
10+
immutableList.add("D"); // throws UnsupportedOperationException
11+
12+
System.out.println(immutableList);
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.stacktips.collections.list.arraylist;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class _2b_UnmodifiableListExample {
8+
public static void main(String[] args) {
9+
List<String> list = new ArrayList<>();
10+
list.add("A");
11+
list.add("B");
12+
list.add("C");
13+
14+
List<String> unmodifiableList = Collections.unmodifiableList(list);
15+
System.out.println(unmodifiableList);
16+
17+
unmodifiableList.add("D"); // throws UnsupportedOperationException
18+
System.out.println(unmodifiableList);
19+
}
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.stacktips.collections.list.arraylist;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _3a_NonSynchronizedArrayList {
7+
public static void main(String[] args) {
8+
List<Integer> arrayList = new ArrayList<>();
9+
10+
// Create a runnable task that adds elements to the collections.list
11+
Runnable addItemsTask = () -> {
12+
for (int i = 0; i < 1000; i++) {
13+
arrayList.add(i);
14+
}
15+
};
16+
17+
// Create multiple threads that will run the addItemsTask
18+
Thread thread1 = new Thread(addItemsTask);
19+
thread1.start();
20+
21+
Thread thread2 = new Thread(addItemsTask);
22+
thread2.start();
23+
24+
try {
25+
thread1.join();
26+
thread2.join();
27+
} catch (InterruptedException e) {
28+
System.out.println(e.getMessage());
29+
}
30+
System.out.println("Size of List: " + arrayList.size());
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.stacktips.collections.list.arraylist;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class _3b_SynchronizedArrayList {
8+
public static void main(String[] args) {
9+
List<Integer> arrayList = Collections.synchronizedList(new ArrayList<>());
10+
11+
// Create a runnable task that adds elements to the collections.list
12+
Runnable addItemsTask = () -> {
13+
for (int i = 0; i < 1000; i++) {
14+
arrayList.add(i);
15+
}
16+
};
17+
18+
// Create multiple threads that will run the addItemsTask
19+
Thread thread1 = new Thread(addItemsTask);
20+
thread1.start();
21+
22+
Thread thread2 = new Thread(addItemsTask);
23+
thread2.start();
24+
try {
25+
thread1.join();
26+
thread2.join();
27+
} catch (InterruptedException e) {
28+
System.out.println(e.getMessage());
29+
}
30+
System.out.println("Size of List: " + arrayList.size());
31+
}
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.stacktips.collections.list.copyonwritearraylist;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _1a_ConcurrentModificationException {
7+
8+
public static void main(String[] args) {
9+
List<String> list = new ArrayList<>();
10+
list.add("Apple");
11+
list.add("Banana");
12+
list.add("Guava");
13+
list.add("Grapes");
14+
15+
for (String value : list) {
16+
if (value.equals("Banana")) {
17+
list.remove("Grapes");
18+
}
19+
}
20+
21+
System.out.println(list);
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.stacktips.collections.list.copyonwritearraylist;
2+
3+
import java.util.List;
4+
import java.util.concurrent.CopyOnWriteArrayList;
5+
6+
public class _1b_CopyOnWriteArrayList {
7+
8+
public static void main(String[] args) {
9+
List<String> list = new CopyOnWriteArrayList<>();
10+
list.add("Apple");
11+
list.add("Banana");
12+
list.add("Guava");
13+
list.add("Grapes");
14+
15+
for (String value : list) {
16+
if (value.equals("Banana")) {
17+
list.remove("Grapes");
18+
}
19+
}
20+
21+
System.out.println(list);
22+
}
23+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.stacktips.collections.list.linkedlist;
2+
3+
public record Song(String title, String artist) {
4+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.stacktips.collections.list.linkedlist;
2+
3+
import java.util.LinkedList;
4+
5+
public class _1a_MusicPlayList {
6+
private final LinkedList<Song> playlist;
7+
8+
public _1a_MusicPlayList() {
9+
playlist = new LinkedList<>();
10+
}
11+
12+
public void addSong(Song song) {
13+
playlist.add(song);
14+
}
15+
16+
public void removeSong(Song song) {
17+
playlist.remove(song);
18+
}
19+
20+
public void displayPlaylist() {
21+
for (int i = 0; i < playlist.size(); i++) {
22+
System.out.println((i + 1) + ". " + playlist.get(i));
23+
}
24+
}
25+
26+
public static void main(String[] args) {
27+
_1a_MusicPlayList myPlaylist = new _1a_MusicPlayList();
28+
29+
Song song1 = new Song("Bohemian Rhapsody", "Queen");
30+
Song song2 = new Song("Imagine", "John Lennon");
31+
Song song3 = new Song("Hotel California", "Eagles");
32+
33+
myPlaylist.addSong(song1);
34+
myPlaylist.addSong(song2);
35+
myPlaylist.addSong(song3);
36+
37+
System.out.println("My Playlist:");
38+
myPlaylist.displayPlaylist();
39+
40+
System.out.println("Removing 'Imagine' from playlist.");
41+
myPlaylist.removeSong(song2);
42+
43+
System.out.println("Updated Playlist:");
44+
myPlaylist.displayPlaylist();
45+
}
46+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.stacktips.collections.list.stack;
2+
3+
import java.util.Stack;
4+
5+
public class _1a_TextEditor {
6+
private final Stack<String> textStack = new Stack<>();
7+
private final Stack<String> undoStack = new Stack<>();
8+
9+
public void type(String text) {
10+
textStack.push(text);
11+
undoStack.clear();
12+
}
13+
14+
public void undo() {
15+
if (!textStack.isEmpty()) {
16+
String lastText = textStack.pop();
17+
undoStack.push(lastText);
18+
} else {
19+
System.out.println("Nothing to undo.");
20+
}
21+
}
22+
23+
public void redo() {
24+
if (!undoStack.isEmpty()) {
25+
String lastUndoText = undoStack.pop();
26+
textStack.push(lastUndoText);
27+
} else {
28+
System.out.println("Nothing to redo.");
29+
}
30+
}
31+
32+
public void display() {
33+
System.out.println("Current Text: " + String.join("", textStack));
34+
}
35+
36+
public static void main(String[] args) {
37+
_1a_TextEditor editor = new _1a_TextEditor();
38+
editor.type("Hello, ");
39+
editor.type("world!");
40+
editor.display();
41+
42+
editor.undo();
43+
editor.display();
44+
45+
editor.redo();
46+
editor.display();
47+
48+
editor.undo();
49+
editor.undo();
50+
editor.display();
51+
52+
editor.redo();
53+
editor.display();
54+
55+
56+
}
57+
}
58+

0 commit comments

Comments
 (0)