Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e469ebd

Browse files
collection Examples
1 parent c93e38c commit e469ebd

16 files changed

+402
-0
lines changed

‎collection/List/ArrayAsList.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package collection.List;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class ArrayAsList {
7+
8+
public static void main(String[] args) {
9+
String[] strs = {"JAVA", "Python", "GoLang","PHP"};
10+
System.out.println(Arrays.toString(strs));
11+
12+
List<String> list = Arrays.asList(strs);
13+
System.out.println(list);
14+
15+
List<Integer> IntList = Arrays.asList(22, 44, 11, 33);
16+
System.out.println(IntList);
17+
}
18+
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package collection.List;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
6+
public class ArrayListExample {
7+
8+
public static void main(String[] args) {
9+
ArrayList<Integer> list= new ArrayList<>();
10+
list.add(10);
11+
list.add(8);
12+
list.add(17);
13+
list.add(14);
14+
System.out.println("Element at index 2: "+list.get(2));
15+
Iterator<Integer> itr=list.iterator();
16+
while(itr.hasNext())
17+
System.out.println(itr.next());
18+
System.out.println("Removed Element: "+list.remove(2));
19+
System.out.println("New Element at index 2: "+list.get(2));
20+
System.out.println(list.parallelStream());
21+
System.out.println(list.iterator());
22+
list.set(2,15);
23+
System.out.println("Postion 2 Element Changed to "+list.get(2));
24+
}
25+
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package collection.List;
2+
3+
import java.util.LinkedList;
4+
import java.util.ListIterator;
5+
6+
public class LinkedListExample {
7+
8+
public static void main(String[] args) {
9+
LinkedList<Integer> list= new LinkedList<>();
10+
list.add(10);
11+
list.add(8);
12+
list.add(17);
13+
list.add(14);
14+
System.out.println("Element at index 2: "+list.get(2));
15+
ListIterator<Integer> itr=list.listIterator();
16+
while(itr.hasNext())
17+
System.out.println(itr.next());
18+
System.out.println("Removed Element: "+list.remove(2));
19+
System.out.println("New Element at index 2: "+list.get(2));
20+
System.out.println(list.parallelStream());
21+
System.out.println(list.iterator());
22+
list.set(2,15);
23+
System.out.println("Postion 2 Element Changed to "+list.get(2));
24+
}
25+
}

‎collection/List/ListToArray.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package collection.List;
2+
3+
import java.util.ArrayList;
4+
5+
public class ListToArray {
6+
7+
public static void main(String[] args) {
8+
ArrayList<Integer> list= new ArrayList<>();
9+
list.add(10);
10+
list.add(8);
11+
list.add(17);
12+
list.add(14);
13+
Integer[] arr=list.toArray(new Integer[0]);
14+
for(Integer a:arr) {
15+
System.out.println(a.toString());
16+
}
17+
18+
}
19+
20+
}

‎collection/List/VectorExample.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package collection.List;
2+
3+
import java.util.Iterator;
4+
import java.util.Vector;
5+
6+
public class VectorExample {
7+
8+
public static void main(String[] args) {
9+
Vector<String> list= new Vector<>();
10+
list.add("Java");
11+
list.add("C");
12+
list.add("C++");
13+
list.add("HTML");
14+
System.out.println("Element at index 2: "+list.get(2));
15+
Iterator<String> itr=list.iterator();
16+
while(itr.hasNext())
17+
System.out.println(itr.next());
18+
System.out.println("Removed Element: "+list.remove(2));
19+
System.out.println("New Element at index 2: "+list.get(2));
20+
System.out.println(list.parallelStream());
21+
System.out.println(list.iterator());
22+
list.set(2,"JavaScript");
23+
System.out.println("Postion 2 Element Changed to "+list.get(2));
24+
}
25+
}

‎collection/Map/HashMapExample.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package collection.Map;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
/*
6+
* Output-
7+
Value Associated with key 'two'= 2
8+
Keysets: [six, one, seven, two]
9+
Entry of map: [six=6, one=1, seven=7, two=2]
10+
Size of map: 4
11+
one : 1
12+
{six=6, one=1, seven=7, two=2}
13+
*
14+
*/
15+
public class HashMapExample {
16+
17+
public static void main(String[] args) {
18+
Map<String,Integer> m=new HashMap<>();
19+
m.put("six", 6);
20+
m.put("one", 1);
21+
m.put("two", 2);
22+
m.put("seven", 7);
23+
System.out.print("Value Associated with key 'two'= "+m.get("two")+"\n");
24+
System.out.print("Keysets: "+m.keySet()+"\n");
25+
System.out.print("Entry of map: "+m.entrySet()+"\n");
26+
System.out.println("Size of map: "+m.size());
27+
if(m.containsKey("one")) {
28+
Integer x=m.get("one");
29+
System.out.println("one : "+x);
30+
}
31+
if(!m.isEmpty())
32+
System.out.println(m);
33+
}
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package collection.Map;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
public class LinekedHAshMapExample {
7+
8+
public static void main(String[] args) {
9+
Map<String,Integer> m=new LinkedHashMap<>();
10+
m.put("six", 6);
11+
m.put("one", 1);
12+
m.put("two", 2);
13+
m.put("seven", 7);
14+
System.out.print("Value Associated with key 'two'= "+m.get("two")+"\n");
15+
System.out.print("Keysets: "+m.keySet()+"\n");
16+
System.out.print("Entry of map: "+m.entrySet()+"\n");
17+
System.out.println("Size of map: "+m.size());
18+
if(m.containsKey("one")) {
19+
Integer x=m.get("one");
20+
System.out.println("one : "+x);
21+
}
22+
if(!m.isEmpty())
23+
System.out.println(m);
24+
}
25+
26+
}

‎collection/Map/TreeMapExample.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package collection.Map;
2+
3+
import java.util.Map;
4+
import java.util.TreeMap;
5+
/*
6+
Value Associated with key 'two'= 2
7+
Keysets: [one, seven, six, two]
8+
Entry of map: [one=1, seven=7, six=6, two=2]
9+
Size of map: 4
10+
one : 1
11+
{one=1, seven=7, six=6, two=2}
12+
*/
13+
public class TreeMapExample {
14+
public static void main(String[] args) {
15+
Map<String,Integer> m=new TreeMap<>();
16+
//Treemap sorts according to key
17+
m.put("six", 6);
18+
m.put("one", 1);
19+
m.put("two", 2);
20+
m.put("seven", 7);
21+
System.out.print("Value Associated with key 'two'= "+m.get("two")+"\n");
22+
System.out.print("Keysets: "+m.keySet()+"\n");
23+
System.out.print("Entry of map: "+m.entrySet()+"\n");
24+
System.out.println("Size of map: "+m.size());
25+
if(m.containsKey("one")) {
26+
Integer x=m.get("one");
27+
System.out.println("one : "+x);
28+
}
29+
if(!m.isEmpty())
30+
System.out.println(m);
31+
}
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package collection.Queue;
2+
3+
import java.util.Iterator;
4+
import java.util.PriorityQueue;
5+
import java.util.Queue;
6+
7+
/*
8+
C
9+
JavaScript
10+
Java
11+
Python
12+
*/
13+
14+
public class PriorityQueueExample {
15+
public static void main(String[] args) {
16+
Queue<String> list= new PriorityQueue<>();
17+
list.add("Java");
18+
list.add("Python");
19+
list.add("C");
20+
list.add("JavaScript");
21+
Iterator<String> itr=list.iterator();
22+
while(itr.hasNext())
23+
System.out.println(itr.next());
24+
System.out.println("Removed Element: "+list.remove("C"));
25+
}
26+
}

‎collection/Queue/QueueExample.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package collection.Queue;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Iterator;
5+
import java.util.Queue;
6+
/* Java
7+
Python
8+
C
9+
JavaScript
10+
*/
11+
public class QueueExample {
12+
13+
public static void main(String[] args) {
14+
Queue<String> list= new ArrayDeque<>();
15+
list.add("Java");
16+
list.add("Python");
17+
list.add("C");
18+
list.add("JavaScript");
19+
Iterator<String> itr=list.iterator();
20+
while(itr.hasNext())
21+
System.out.println(itr.next());
22+
System.out.println("Removed Element: "+list.remove("C"));
23+
}
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /