Skip to content

Commit e469ebd

Browse files
authoredSep 3, 2017
collection Examples
1 parent c93e38c commit e469ebd

16 files changed

+402
-0
lines changed
 

Diff for: ‎collection/List/ArrayAsList.java

+19
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+
}

Diff for: ‎collection/List/ArrayListExample.java

+26
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+
}

Diff for: ‎collection/List/LinkedListExample.java

+25
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+
}

Diff for: ‎collection/List/ListToArray.java

+20
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+
}

Diff for: ‎collection/List/VectorExample.java

+25
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+
}

Diff for: ‎collection/Map/HashMapExample.java

+34
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+
}

Diff for: ‎collection/Map/LinekedHAshMapExample.java

+26
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+
}

Diff for: ‎collection/Map/TreeMapExample.java

+32
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+
}

Diff for: ‎collection/Queue/PriorityQueueExample.java

+26
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+
}

Diff for: ‎collection/Queue/QueueExample.java

+24
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+
}

Diff for: ‎collection/Set/Book.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package collection.Set;
2+
3+
public class Book {
4+
private int price;
5+
private String title;
6+
public Book(int p,String t) {
7+
price=p;
8+
title=t;
9+
}
10+
public String getTitle() {
11+
return title;
12+
}
13+
public int getPrice() {
14+
return price;
15+
}
16+
@Override
17+
public int hashCode() {
18+
return price/100;
19+
}
20+
21+
@Override
22+
public boolean equals(Object arg) {
23+
if(!(arg instanceof Book))
24+
return false;
25+
Book b=(Book)arg;
26+
return title.equals(b.title) && price==b.price;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return "[Title: "+title+", Price: "+price+"]";
32+
}
33+
}

Diff for: ‎collection/Set/BookNameComparator.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package collection.Set;
2+
3+
import java.util.Comparator;
4+
5+
public class BookNameComparator implements Comparator<Book> {
6+
7+
@Override
8+
public int compare(Book o1, Book o2) {
9+
return o1.getTitle().compareTo(o2.getTitle());
10+
}
11+
12+
}

Diff for: ‎collection/Set/BookPriceComparator.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package collection.Set;
2+
3+
import java.util.Comparator;
4+
5+
public class BookPriceComparator implements Comparator<Book> {
6+
@Override
7+
public int compare(Book o1, Book o2) {
8+
return (o1.getPrice()<o2.getPrice())?-1:(o1.getPrice()>o2.getPrice())?1:0;
9+
}
10+
}

Diff for: ‎collection/Set/HashSetExample.java

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

Diff for: ‎collection/Set/LinkedHashSetEx.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package collection.Set;
2+
3+
/*
4+
* Output-
5+
*
6+
* Java
7+
Python
8+
C
9+
JavaScript
10+
Removed Element: true
11+
12+
*
13+
* */
14+
import java.util.Iterator;
15+
import java.util.LinkedHashSet;
16+
import java.util.Set;
17+
18+
public class LinkedHashSetEx {
19+
20+
public static void main(String[] args) {
21+
Set<String> list= new LinkedHashSet<>();
22+
list.add("Java");
23+
list.add("Python");
24+
list.add("C");
25+
list.add("JavaScript");
26+
Iterator<String> itr=list.iterator();
27+
while(itr.hasNext())
28+
System.out.println(itr.next());
29+
System.out.println("Removed Element: "+list.remove("C"));
30+
31+
}
32+
}

Diff for: ‎collection/Set/TreeSetExample.java

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

0 commit comments

Comments
 (0)
Please sign in to comment.