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.
Binary file not shown.
1.88 KB
Binary file not shown.

src/sequentialCollection/AList.java

+32
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+
}
+27
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+
}
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+
}
+29
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package sequentialCollection;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
import java.util.PriorityQueue;
7+
8+
public class SquareDancePriorityQueue {
9+
public static void main(String[] args) {
10+
PriorityQueue<String> men = new PriorityQueue<String>();
11+
PriorityQueue<String> women = new PriorityQueue<String>();
12+
13+
String line = null;
14+
String file = "E:\\workspace\\java_workspace\\AdvancedJavaPractice\\src\\sequentialCollection\\dance.txt";
15+
String sex = "";
16+
BufferedReader in;
17+
18+
try {
19+
in = new BufferedReader(new FileReader(file));
20+
while ((line = in.readLine()) != null) {
21+
sex = line.substring(0, 1);
22+
if (sex.equals("M"))
23+
men.add(line.substring(2));
24+
else
25+
women.add(line.substring(2));
26+
}
27+
} catch (IOException e) {
28+
e.printStackTrace();
29+
}
30+
31+
while (!men.isEmpty() && !women.isEmpty()) {
32+
System.out.println("The dance partners will be :");
33+
System.out.println(men.poll() + " and " + women.poll());
34+
}
35+
36+
if (men.isEmpty()) {
37+
System.out.println("There are " + women.size()
38+
+ " women waiting to dance");
39+
System.out.println(women.peek() + " is the next women dancer");
40+
} else if (women.isEmpty()) {
41+
System.out.println("There are " + men.size()
42+
+ " men waiting to dance");
43+
System.out.println(men.peek() + " is the next men dancer");
44+
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package sequentialCollection;
2+
3+
import java.util.TreeSet;
4+
5+
public class TreeSetExercise {
6+
public static void main(String[] args) {
7+
TreeSet<String> set = new TreeSet();
8+
9+
String text = "I'll tell you what went wrong,"
10+
+ "I spent my days planning a future when I couldn't see in front of me"
11+
+ "I'll tell you what went wrong,"
12+
+ "I had a great imagination but I couldn't grasp reality"
13+
+ "I'll tell you what went wrong,"
14+
+ "I couldn't stop myself from reaching for a better life for you and me"
15+
+ "I'll tell you what went wrong,"
16+
+ "I couldn't give myself completely cos who I'm losing wouldn't let me be";
17+
String words[] = text.split(" ");
18+
for(String s : words )
19+
{
20+
System.out.println(s);
21+
set.add(s);
22+
}
23+
System.out.println("Total words : " + words.length);
24+
25+
System.out.println("Total unique words : " + set.size());
26+
for(String s : set)
27+
System.out.println(s);
28+
29+
30+
}
31+
}

src/sequentialCollection/dance.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
M Raymond Williams
2+
F Cynthia Fehrenbach
3+
F Jennifer Ingram
4+
M David Durr
5+
M Bryan Frazer
6+
F Beata Lovelace
7+
F Alisa Brown
8+
M Clayton Ruff
9+
M Terrill Beckerman
10+
F Cindy Beck
11+
M Mayo Johnson
12+
M Mike Dahly

0 commit comments

Comments
 (0)