Skip to content

Commit 2dc8446

Browse files
committed
added practice exercises for Sequential Collection in Java
1 parent 15af2be commit 2dc8446

13 files changed

+211
-0
lines changed

bin/sequentialCollection/AList.class

1.66 KB
Binary file not shown.
1.72 KB
Binary file not shown.
1.56 KB
Binary file not shown.
1.79 KB
Binary file not shown.
2.37 KB
Binary file not shown.
1.88 KB
Binary file not shown.

src/sequentialCollection/AList.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package sequentialCollection;
2+
3+
import java.util.ArrayList;
4+
5+
public class AList {
6+
public static void main(String args[]) {
7+
ArrayList<Integer> grades = new ArrayList<Integer>();
8+
grades.add(100);
9+
grades.add(200);
10+
grades.add(300);
11+
grades.add(400);
12+
13+
float total = 0f;
14+
// for(int i = 0 ; i < grades.size() ; ++i)
15+
// total+= grades.get(i);
16+
17+
// Collection interface provides us iterator
18+
for (Integer i : grades)
19+
total += i;
20+
21+
System.out.println("Size of grades is " + grades.size());
22+
System.out.println("Average grades is " + total / grades.size());
23+
grades.remove(2);
24+
System.out.println("New size after removing is " + grades.size());
25+
grades.add(60);
26+
grades.add(70);
27+
System.out.println("New size after inserting is " + grades.size());
28+
29+
30+
}
31+
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package sequentialCollection;
2+
3+
import java.util.TreeSet;
4+
5+
public class AlphaTreeSet {
6+
public static void main(String args[]) {
7+
TreeSet<String> names = new TreeSet<String>();
8+
names.add("ozzy");
9+
names.add("marshal");
10+
names.add("jolly");
11+
names.add("jimmy");
12+
13+
System.out.println("Number of names: " + names.size());
14+
15+
// all names will print out in sequential manner
16+
for (String s : names) {
17+
System.out.println(s);
18+
}
19+
System.out.println("Name before marshal : " + names.lower("marshal"));
20+
System.out.println("Name after marshal : " + names.higher("marshal"));
21+
System.out.println("Name at first position : " + names.first());
22+
System.out.println("Name at last position : " + names.last());
23+
24+
25+
}
26+
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package sequentialCollection;
2+
3+
import java.util.PriorityQueue;
4+
5+
public class LinePriorityQueue {
6+
7+
public static void main(String[] args) {
8+
PriorityQueue<String> line = new PriorityQueue();
9+
line.add("Jimmy");
10+
line.add("ozzy");
11+
line.add("jolly");
12+
line.add("marshal");
13+
14+
System.out.println("The line ");
15+
for (String s : line) {
16+
System.out.println(s);
17+
}
18+
19+
line.poll();
20+
21+
System.out.println("The line ");
22+
for(String s : line)
23+
System.out.println(s);
24+
25+
line.remove("marshal");
26+
27+
for(String s : line )
28+
System.out.println(s);
29+
30+
System.out.println("Head of line is : " + line.peek());
31+
}
32+
33+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package sequentialCollection;
2+
3+
import java.util.HashSet;
4+
5+
public class NamesHashSet {
6+
public static void main (String args[])
7+
{
8+
HashSet<String> names = new HashSet<String>();
9+
names.add("jimmy");
10+
names.add("jolly");
11+
12+
System.out.println("The number of names is " + names.size());
13+
for(String s : names)
14+
System.out.println(s);
15+
names.remove("jimmy");
16+
names.add("marshal");
17+
String name = "marshal";
18+
if(names.contains(name))
19+
System.out.println(name + " is in hash set collection");
20+
else
21+
System.out.println(name + " is not in hash set collection");
22+
23+
for(String n : names )
24+
System.out.println(n);
25+
names.clear();
26+
System.out.println("total names : " + names.size());
27+
28+
}
29+
}

0 commit comments

Comments
 (0)