Skip to content

Commit

Permalink
Add CollectionDemo
Browse files Browse the repository at this point in the history
  • Loading branch information
mspatel18 authored Apr 4, 2023
1 parent 354351b commit 5647c08
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions Sem4/CollectionDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.util.*;

public class CollectionDemo {
public static void main(String[] args) {

// ArrayList
List<String> arrayList = new ArrayList<>();
arrayList.add("apple");
arrayList.add("banana");
arrayList.add("cherry");
System.out.println("ArrayList: " + arrayList);

// LinkedList
List<String> linkedList = new LinkedList<>();
linkedList.add("apple");
linkedList.add("banana");
linkedList.add("cherry");
System.out.println("LinkedList: " + linkedList);

// LinkedHashMap
Map<String, Integer> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("apple", 1);
linkedHashMap.put("banana", 2);
linkedHashMap.put("cherry", 3);
System.out.println("LinkedHashMap: " + linkedHashMap);

// TreeMap
Map<String, Integer> treeMap = new TreeMap<>();
treeMap.put("apple", 1);
treeMap.put("banana", 2);
treeMap.put("cherry", 3);
System.out.println("TreeMap: " + treeMap);

// HashSet
Set<String> hashSet = new HashSet<>();
hashSet.add("apple");
hashSet.add("banana");
hashSet.add("cherry");
System.out.println("HashSet: " + hashSet);

// Create
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
System.out.println("Names: " + names);

// Read
String name = names.get(1);
System.out.println("Name at index 1: " + name);

// Update
names.set(1, "Bob Updated");
System.out.println("Names after update: " + names);

// Delete
names.remove(2);
System.out.println("Names after delete: " + names);
}
}

0 comments on commit 5647c08

Please sign in to comment.