Generics Types and Collections
Done as treo!
import java.util.LinkedList;
import java.util.Queue;
public class QueueChallenge {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
// count number of words in queue
int wordCount = 0;
// Enqueue data
// .add adds each word to the end of the queue
queue.add("seven");
wordCount++;
printQueue(queue, wordCount);
queue.add("slimy");
wordCount++;
printQueue(queue, wordCount);
queue.add("snakes");
wordCount++;
printQueue(queue, wordCount);
queue.add("sallying");
wordCount++;
printQueue(queue, wordCount);
queue.add("slowly");
wordCount++;
printQueue(queue, wordCount);
queue.add("slithered");
wordCount++;
printQueue(queue, wordCount);
queue.add("southward");
wordCount++;
printQueue(queue, wordCount);
// Dequeue data
// queue.remove() removes the first element from the queue
String data = queue.remove();
wordCount--;
System.out.println("Dequeued data: " + data);
printQueue(queue, wordCount);
data = queue.remove();
wordCount--;
System.out.println("Dequeued data: " + data);
printQueue(queue, wordCount);
data = queue.remove();
wordCount--;
System.out.println("Dequeued data: " + data);
printQueue(queue, wordCount);
data = queue.remove();
wordCount--;
System.out.println("Dequeued data: " + data);
printQueue(queue, wordCount);
data = queue.remove();
wordCount--;
System.out.println("Dequeued data: " + data);
printQueue(queue, wordCount);
data = queue.remove();
wordCount--;
System.out.println("Dequeued data: " + data);
printQueue(queue, wordCount);
data = queue.remove();
wordCount--;
System.out.println("Dequeued data: " + data);
printQueue(queue, wordCount);
}
private static void printQueue(Queue<String> queue, int wordCount) {
// Print the queue
System.out.print("Words count: " + wordCount + ", data: ");
// iterating over a queue is similar to iterating over a list
for (String element : queue) {
System.out.print(element + " ");
}
}
}
QueueChallenge.main(null);
import java.util.LinkedList;
import java.util.Queue;
public class MergeQueues {
public static void main(String[] args) {
// start off with two separate queues, and use .add to add integers to each queue
Queue<Integer> queue1 = new LinkedList<>();
queue1.add(1);
queue1.add(4);
queue1.add(5);
queue1.add(8);
Queue<Integer> queue2 = new LinkedList<>();
queue2.add(2);
queue2.add(3);
queue2.add(6);
queue2.add(7);
Queue<Integer> mergedQueue = mergeQueues(queue1, queue2);
System.out.println(mergedQueue);
}
public static Queue<Integer> mergeQueues(Queue<Integer> queue1, Queue<Integer> queue2) {
// mergedQueue is the final sorted queue
Queue<Integer> mergedQueue = new LinkedList<>();
while (!queue1.isEmpty() && !queue2.isEmpty()) {
// .peek looks at the first value in each queue
int head1 = queue1.peek();
int head2 = queue2.peek();
if (head1 < head2) {
// the smaller integer is added to the merged queue, and is removed from the original queue
mergedQueue.add(head1);
queue1.remove();
} else {
mergedQueue.add(head2);
queue2.remove();
}
}
while (!queue1.isEmpty()) {
mergedQueue.add(queue1.peek());
queue1.remove();
}
while (!queue2.isEmpty()) {
mergedQueue.add(queue2.peek());
queue2.remove();
}
return mergedQueue;
}
}
MergeQueues.main(null);
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
public class ShuffleQueue {
public static void main(String[] args) {
// Create a new Queue and add elements
Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
queue.add(3);
queue.add(4);
queue.add(5);
queue.add(6);
queue.add(7);
queue.add(8);
// Shuffle the Queue by iterating through each element and swapping with a random position
for (int i = 0; i < queue.size(); i++) {
int randomIndex = (int)Math.random()*10;
int temp = queue.peek(); // Get the element at the head of the queue
queue.poll(); // Remove the element from the head of the queue
queue.add(temp); // Add the element back to the queue
for (int j = 0; j < randomIndex; j++) {
queue.add(queue.peek()); // Move the elements up to the random index
queue.poll();
}
}
// Print out the shuffled Queue
while (!queue.isEmpty()) {
System.out.print(queue.poll() + " ");
}
}
}
ShuffleQueue.main(null);
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class ReverseQueueWithStack {
public static void main(String[] args) {
// create a Queue and add elements
Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
queue.add(3);
System.out.println("Queue elements: " + queue);
// create a Stack and transfer elements from Queue to Stack
Stack<Integer> stack = new Stack<>();
while (!queue.isEmpty()) {
stack.push(queue.remove());
}
// print the elements of the Stack
System.out.print("Stack elements: ");
while (!stack.isEmpty()) {
// .pop takes the last element of the stack
System.out.print(stack.pop() + " ");
}
}
}
ReverseQueueWithStack.main(null);
// Person.java
public class Person {
public final String name;
public Person(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
// Student.java
public class Student extends Person {
private final ArrayList<Grade> grades;
public Student(String name) {
super(name);
grades = new ArrayList<>();
}
public void addGrade(Grade grade) {
grades.add(grade);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(super.toString()).append(":\n");
for (Grade grade : grades) {
sb.append("\t").append(grade.toString()).append("\n");
}
return sb.toString();
}
}
// Class.java
public class Class {
private final ArrayList<Student> students;
public Class() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Class:\n");
for (Student student : students) {
sb.append(student.toString());
}
return sb.toString();
}
}
// Main.java
public class Main {
public static void main(String[] args) {
Class myClass = new Class();
Student student1 = new Student("Alice");
student1.addGrade(new Grade("Math", 90.0));
student1.addGrade(new Grade("English", 85.0));
myClass.addStudent(student1);
Student student2 = new Student("Bob");
student2.addGrade(new Grade("Math", 80.0));
student2.addGrade(new Grade("English", 75.0));
myClass.addStudent(student2);
System.out.println(myClass.toString());
}
}