Generic Type Hacks
Generic Types Blog
Queue<String> queue = new LinkedList<>(); // Queue interface uses LL implementation
queue.add("John");
queue.add("Jane");
queue.add("Bob");
// Collections has a toArray convertion
Object[] arr = queue.toArray();
// Empty queue
System.out.println("Empty Queue");
while (queue.size() > 0) // Interate while size
System.out.println(queue.remove());
// Iterate of array
System.out.println("Iterate over Array");
for (Object a : arr) // Type is Object from convertion
System.out.println(a);
public abstract class Generics {
public final String masterType = "Generic";
private String type; // extender should define their data type
// generic enumerated interface
public interface KeyTypes {
String name();
}
protected abstract KeyTypes getKey(); // this method helps force usage of KeyTypes
// getter
public String getMasterType() {
return masterType;
}
// getter
public String getType() {
return type;
}
// setter
public void setType(String type) {
this.type = type;
}
// this method is used to establish key order
public abstract String toString();
// static print method used by extended classes
public static void print(Generics[] objs) {
// print 'Object' properties
System.out.println(objs.getClass() + " " + objs.length);
// print 'Generics' properties
if (objs.length > 0) {
Generics obj = objs[0]; // Look at properties of 1st element
System.out.println(
obj.getMasterType() + ": " +
obj.getType() +
" listed by " +
obj.getKey());
}
// print "Generics: Objects'
for(Object o : objs) // observe that type is Opaque
System.out.println(o);
System.out.println();
}
}
public class Lingo extends Generics {
// Class data
public static KeyTypes key = KeyType.lang; // static initializer
public static void setOrder(KeyTypes key) {Lingo.key = key;}
public enum KeyType implements KeyTypes {lang, years, level}
// Instance data
private final String lang;
private final int years;
private final int level;
// Constructor
Lingo(String lang, int years, int level)
{
this.setType("Lingo");
this.lang = lang;
this.years = years;
this.level = level;
}
/* 'Generics' requires getKey to help enforce KeyTypes usage */
@Override
protected KeyTypes getKey() { return Lingo.key; }
/* 'Generics' requires toString override
* toString provides data based off of Static Key setting */
@Override
public String toString() {
String output="";
if (KeyType.lang.equals(this.getKey())) {
output += this.lang;
} else if (KeyType.level.equals(this.getKey())) {
output += this.years;
} else if (KeyType.level.equals(this.getKey())) {
output += + this.level;
} else {
output = super.getType() + ": " + this.lang + ", " + this.years + ", " + this.level;
}
return output;
}
// Test data initializer
public static Lingo[] languages() {
return new Lingo[]{
new Lingo("Python", 1, 4),
new Lingo("Java", 4, 3),
new Lingo("JavaScript", 3, 5),
new Lingo("C++", 2, 4),
};
}
public static void main(String[] args)
{
// Inheritance Hierarchy
Lingo[] objs = languages();
// print with language
Lingo.setOrder(KeyType.lang);
Lingo.print(objs);
// print level
Lingo.setOrder(KeyType.years);
Lingo.print(objs);
// print correct out of 10
Lingo.setOrder(KeyType.level);
Lingo.print(objs);
}
}
Lingo.main(null);
// Hack 1
public class QueueExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<String>();
// Adding elements to the queue
queue.add("seven");
System.out.println("Enqueued data: " + "seven");
printQueue(queue);
queue.add("slimy");
System.out.println("Enqueued data: " + "slimy");
printQueue(queue);
queue.add("snakes");
System.out.println("Enqueued data: " + "snakes");
printQueue(queue);
queue.add("sallying");
System.out.println("Enqueued data: " + "sallying");
printQueue(queue);
queue.add("slowly");
System.out.println("Enqueued data: " + "slowly");
printQueue(queue);
queue.add("slithered");
System.out.println("Enqueued data: " + "slithered");
printQueue(queue);
queue.add("southward");
System.out.println("Enqueued data: " + "southward");
printQueue(queue);
// Removing elements from the queue
String data =queue.remove();
System.out.println("Dequeued data: " + data);
printQueue(queue);
data = queue.remove();
System.out.println("Dequeued data: " + data);
printQueue(queue);
data = queue.remove();
System.out.println("Dequeued data: " + data);
printQueue(queue);
data = queue.remove();
System.out.println("Dequeued data: " + data);
printQueue(queue);
data = queue.remove();
System.out.println("Dequeued data: " + data);
printQueue(queue);
data = queue.remove();
System.out.println("Dequeued data: " + data);
printQueue(queue);
data = queue.remove();
System.out.println("Dequeued data: " + data);
printQueue(queue);
}
// Helper method to print the contents of the queue
public static void printQueue(Queue<String> queue) {
System.out.println("Words count: " + queue.size() + ", data: " + String.join(" ", queue));
System.out.println();
}
}
QueueExample.main(null);
//Hack 2 - merge 2 queues
Queue<Integer> questionNums1 = new LinkedList<>();
//offer adds an element to the end of the queue and
//returns a boolean value indicating whether the operation was successful
questionNums1.add(1);
questionNums1.add(4);
questionNums1.add(5);
questionNums1.add(8);
Queue<Integer> questionNums2 = new LinkedList<>();
questionNums2.add(2);
questionNums2.add(3);
questionNums2.add(6);
questionNums2.add(7);
Queue<Integer> mergedQueue = new LinkedList<>();
while (!questionNums1.isEmpty() && !questionNums2.isEmpty()) {
if (questionNums1.peek() < questionNums2.peek()) { //Retrieve the first element from each queue using the peek() method
//peek method returns the element at the front of the queue without removing it
mergedQueue.offer(questionNums1.poll());
} else { //Compare the two elements
//enqueue the smaller one to the new queue using the offer() method.
mergedQueue.offer(questionNums2.poll());
}//repeat
}
mergedQueue.addAll(questionNums1);
mergedQueue.addAll(questionNums2);
System.out.println("Python Question # Order: " + mergedQueue);
import java.util.Queue;
import java.util.LinkedList;
import java.util.Random;
public class ShuffleQueue {
public static void shuffle(Queue<String> queue) {
Random rand = new Random();
//static shuffle method that takes a Queue of strings as its argument and shuffles the elements
for (int i = 0; i < queue.size(); i++) {
int randomIndex = rand.nextInt(queue.size());
String temp = queue.peek();
//view or peek at the element at the front of the queue without removing it
for (int j = 0; j < randomIndex; j++) {
queue.add(queue.remove());
}//iterating through the queue and swapping
// each element with another element at a random position in the queue.
queue.remove();
for (int j = 0; j < randomIndex; j++) {
queue.add(queue.remove());
}
queue.add(temp);
}
}
//Random object to generate random indices within the size of the queue
//combination of remove and add methods to move elements around in the queue
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.add("Mastered");
queue.add("Beginner");
queue.add("Expert");
queue.add("Improving");
System.out.println("Original queue: " + queue);
shuffle(queue);
System.out.println("Shuffled queue: " + queue);
}
}
ShuffleQueue.main(null);
//Hack 4
Queue<Integer> queue = new LinkedList<>();
//Create a new empty stack to store the elements of the queue
queue.offer(1);
queue.offer(2);
queue.offer(3);
queue.offer(4);
queue.offer(5);
Stack<Integer> stack = new Stack<>();
while (!queue.isEmpty()) {
stack.push(queue.poll());
}
//Dequeue all elements from the original queue and push them onto the stack
Queue<Integer> reversedQueue = new LinkedList<>();
while (!stack.isEmpty()) {
reversedQueue.offer(stack.pop());
}//Create a new empty queue to store the reversed elements
// pop removes and returns the top element of the stack
System.out.println("Original Question Numbers: " + queue);
System.out.println("Reversed Question Numbers: " + reversedQueue);
import java.util.Queue;
import java.util.Stack;
public class Languages {
public static void reverse(Queue<String> queue) {
Stack<String> stack = new Stack<>();
//The reverse method creates a new Stack object,
//then iterates through the elements in the queue, removing them one by one and pushing them onto the stack
while (!queue.isEmpty()) {
stack.push(queue.remove());
}
while (!stack.isEmpty()) {
queue.add(stack.pop()); //the method iterates
//through the elements in the stack, popping them off one by one and adding them back to the queue
}
}
}
System.out.println("Reversed queue of languages: "+queue);
public class BubbleSort{
public static void main(String[] args)
{
int[] bubble_arr = {23,56,21,34,678,2,4,7,2235,996,446,999,6574,87,67,902,8754};
System.out.print("Original: ");
for(int index=0; index<bubble_arr.length; index++){
System.out.print(bubble_arr[index] + ", ");
}
System.out.println();
/* run continuously until the first time where no swaps happen */
boolean swap = true;
while(swap)
{
swap = false;
for(int i=0; i<bubble_arr.length-1; i++){
if (bubble_arr[i]>bubble_arr[i+1]){
swap = true;
int temp = bubble_arr[i];
bubble_arr[i] = bubble_arr[i+1];
bubble_arr[i+1] = temp;
}
}
}
System.out.print("Sorted: ");
for(int index=0; index<bubble_arr.length; index++){
System.out.print(bubble_arr[index] + ", ");
}
}
}
BubbleSort.main(null);
public class Insertion{
public static void main(String[] args){
int[] array = {3,2,5,1,0,10,43,2353,32,75,12,33};
for(int i = 0; i<array.length; i++){
System.out.print(array[i] + ", ");
}
System.out.println();
for(int index=1; index<array.length; index++){
for(int sorted_index = index; sorted_index>0; sorted_index--){
if(array[sorted_index]<array[sorted_index-1]){
int temp = array[sorted_index];
array[sorted_index] = array[sorted_index-1];
array[sorted_index-1] = temp;
}
}
}
for (int i = 0; i<array.length; i++){
System.out.print(array[i] + ", ");
}
}
}
Insertion.main(null);
//Method 1: PLacing all the lowest integers to the right, or the end of the array.
public class Selection {
public static void main(String[] args){
int[] array = {23,12,57,335,5433,24567,64,25,66};
for (int i = 0; i<array.length; i++){
System.out.print(array[i] + ", ");
}
System.out.println();
int times = array.length;
for (int outer=times; outer>0; outer--){
int min = 0xFFFFFF;
int min_idx = 0;
for(int i=0; i<outer; i++){
if(array[i] < min){
min = array[i];
min_idx = i;
}
}
for (int j = min_idx+1; j < array.length; j++){
array[j-1]=array[j];
}
array[array.length-1] = min;
}
for (int i = 0; i<array.length; i++){
System.out.print(array[i] + ", ");
}
}
}
Selection.main(null);
23, 12, 57, 335, 5433, 24567, 64;