Compare commits

..

15 Commits

Author SHA1 Message Date
391dc19c74 I forgor to push this, this code is like months old lol 2024-04-11 16:46:49 -05:00
ecce3951fb Markou moment 2024-02-19 09:40:34 -06:00
f08b26a4a2 Glad that's over with 2024-02-13 20:17:38 -06:00
bcb6aa480a agh 2024-02-09 14:44:07 -06:00
eb94a026a7 broken code :) 2024-02-08 14:01:43 -06:00
a48615c8f8 agh 2024-02-02 19:19:48 -06:00
31f634c637 absolute bruh moment 2024-02-02 15:12:56 -06:00
ad38d64219 honey, its time for you to learn something else in java! \n yes honey... :depressed: 2024-01-31 16:28:12 -06:00
3047558ef6 aaaa 2024-01-29 09:59:41 -06:00
7962e14215 sync 2024-01-26 15:30:51 -06:00
aef4bcc29c Markou is absolutely insane 2024-01-26 15:29:46 -06:00
aa46f43ea5 upload practice exam 2023-12-03 15:25:38 -06:00
5e48abd081 Upload files to "Semester 3/Exams/ZIPs" 2023-11-29 12:28:56 -06:00
cf4d08f752 PriorityQueue.java 2023-11-15 21:08:23 -06:00
490f041804 Agonizing torment 2023-11-15 10:17:04 -06:00
132 changed files with 15319 additions and 92 deletions

15
.gitignore vendored
View File

@@ -194,3 +194,18 @@
/Semester 3/Assignments/LinkedList/build/
/Semester 3/Assignments/MP6_CalebFontenot/target/
/Semester 3/Assignments/Lab_CalebFontenot_MaximumOrderedString/target/
/Semester 4/EnumDemo/target/
/Semester 4/Assignments/DAO/target/
/Semester 4/Assignments/ProjectTrees_CalebFontenot/target/
/Semester 4/Assignments/DataStructures/target/
/MergeSort/target/
/Semester 3/Exams/ProgrammingExam2_CalebFontenot/target/
/Semester 4/Assignments/HashMapASDV_Ant/nbproject/private/
/Semester 4/Assignments/HashMapASDV_Ant/build/
/Semester 4/Assignments/HashMapASDV_Ant/dist/
/Semester 4/Assignments/MP1_ManyToMany_CalebFontenot/target/
/Semester 4/Assignments/Multithreading_CalebFontenot/target/
/Semester 4/Assignments/MapASDV_CalebFontenot/nbproject/private/
/Semester 4/Assignments/MapASDV_CalebFontenot/build/
/Semester 4/Assignments/Hashing_CalebFontenot/target/
/Semester 4/Assignments/MP2_CalebFontenot/target/

14
MergeSort/pom.xml Normal file
View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.calebfontenot</groupId>
<artifactId>MergeSort</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>com.calebfontenot.mergesort.MergeSort</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,88 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.calebfontenot.mergesort;
/**
*
* @author caleb
*/
public class MergeSort {
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
public static void main(String args[]) {
// merge sort = recursively divide array in 2, sort, re-combine
// run-time complexity = O(n Log n)
// space complexity = O(n)
int[] array = {8, 2, 5, 3, 4, 7, 6, 1, 0};
printArray(array);
mergeSort(array);
printArray(array);
}
private static void mergeSort(int[] array) {
int length = array.length;
if (length <= 1) {
return; //base case
}
int middle = length / 2;
int[] leftArray = new int[middle];
int[] rightArray = new int[length - middle];
int i = 0; //left array
int j = 0; //right array
for (; i < length; i++) {
if (i < middle) {
leftArray[i] = array[i];
} else {
rightArray[j] = array[i];
j++;
}
}
mergeSort(leftArray);
mergeSort(rightArray);
merge(leftArray, rightArray, array);
}
private static void merge(int[] leftArray, int[] rightArray, int[] array) {
int leftSize = array.length / 2;
int rightSize = array.length - leftSize;
int i = 0, l = 0, r = 0; //indices
//check the conditions for merging
while (l < leftSize && r < rightSize) {
if (leftArray[l] < rightArray[r]) {
array[i] = leftArray[l];
i++;
l++;
} else {
array[i] = rightArray[r];
i++;
r++;
}
}
while (l < leftSize) {
array[i] = leftArray[l];
i++;
l++;
}
while (r < rightSize) {
array[i] = rightArray[r];
i++;
r++;
}
}
}

View File

@@ -0,0 +1,55 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>CompareSize.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.number {color: #6897bb}
.ST1 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/Lab_CalebFontenot_MaximumOrderedString/src/main/java/edu/slcc/asdv/caleb/lab_calebfontenot_maximumorderedstring/CompareSize.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> edu.slcc.asdv.caleb.lab_calebfontenot_maximumorderedstring;
<span class="literal">import</span> java.util.Comparator;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> CompareSize <span class="literal">implements</span> Comparator {
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST1">compare</span>(Object o1, Object o2)
{
<span class="literal">int</span> str1 = ((String) o1).length();
<span class="literal">int</span> str2 = ((String) o2).length();
<span class="literal">if</span> (str1 &gt; str2) {
<span class="literal">return</span> -<span class="number">1</span>;
} <span class="literal">else</span> <span class="literal">if</span>(str1 == str2) {
<span class="literal">return</span> <span class="number">0</span>;
} <span class="literal">else</span> <span class="literal">if</span> (str1 &lt; str2) {
<span class="literal">return</span> <span class="number">1</span>;
}
<span class="literal">return</span> Integer.<span class="ST2">MAX_VALUE</span>;
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,74 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>FindMax.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.number {color: #6897bb}
.string {color: #6a8759}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/Lab_CalebFontenot_MaximumOrderedString/src/main/java/edu/slcc/asdv/caleb/lab_calebfontenot_maximumorderedstring/FindMax.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> edu.slcc.asdv.caleb.lab_calebfontenot_maximumorderedstring;
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> java.util.Collections;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">util</span><span class="comment">.</span><span class="comment">Comparator</span><span class="comment">;</span>
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">util</span><span class="comment">.</span><span class="comment">Scanner</span><span class="comment">;</span>
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> FindMax {
<span class="literal">public</span> <span class="literal">static</span> String <span class="ST1">findMax</span>(String input) {
<span class="literal">int</span> iterationCount = <span class="number">0</span>;
ArrayList&lt;String&gt; possibleSubStr = <span class="literal">new</span> ArrayList&lt;&gt;();
String currentStr = <span class="string">&quot;&quot;</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; input.length() -<span class="number">2</span>; ++i) {
currentStr = input.charAt(i) + <span class="string">&quot;&quot;</span>;
<span class="literal">for</span>(<span class="literal">int</span> j = i + <span class="number">1</span>; j &lt; input.length() -<span class="number">1</span>; ++j) {
iterationCount++;
<span class="literal">if</span> (input.toLowerCase().charAt(i) &lt; input.toLowerCase().charAt(j)) {
currentStr += input.charAt(j);
} <span class="literal">else</span> {
possibleSubStr.add(currentStr);
<span class="literal">break</span>;
}
}
}
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Iteration count: </span><span class="string">&quot;</span> + iterationCount);
System.<span class="ST2">out</span>.println(possibleSubStr);
Collections.<span class="ST3">sort</span>(possibleSubStr, <span class="literal">new</span> CompareSize());
<span class="literal">return</span> possibleSubStr.get(<span class="number">0</span>);
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args)
{
<span class="comment">//System.out.print(&quot;Enter a string: &quot;);</span>
<span class="comment">//Scanner input = new Scanner(System.in);</span>
String inputString = <span class="string">&quot;</span><span class="string">abcdabcdefgzabcdefadcdefab</span><span class="string">&quot;</span>;
<span class="comment">//input.nextLine();</span>
System.<span class="ST2">out</span>.print(<span class="string">&quot;</span><span class="string">The maximum sorted subString is: </span><span class="string">&quot;</span>);
System.<span class="ST2">out</span>.println(<span class="ST3">f</span><span class="ST3">indMax</span>(inputString));
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,62 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>FindMaxOrderedSubstring.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/Lab_CalebFontenot_MaximumOrderedString/src/main/java/edu/slcc/asdv/caleb/lab_calebfontenot_maximumorderedstring/FindMaxOrderedSubstring.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> edu.slcc.asdv.caleb.lab_calebfontenot_maximumorderedstring;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">util</span><span class="comment">.</span><span class="comment">ArrayList</span><span class="comment">;</span>
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">util</span><span class="comment">.</span><span class="comment">Collections</span><span class="comment">;</span>
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> FindMaxOrderedSubstring {
<span class="literal">public</span> <span class="literal">static</span> String <span class="ST1">findMaxOrderedSubString</span>(String input) {
String outputString = <span class="string">&quot;&quot;</span>;
<span class="literal">int</span> j = <span class="number">0</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; input.length(); ++i) {
<span class="literal">if</span> (i == <span class="number">0</span>) {
outputString += input.charAt(i);
}
<span class="literal">if</span> (input.charAt(i) &gt; outputString.charAt(j)) {
++j;
outputString += input.charAt(i);
}
}
<span class="literal">return</span> outputString;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
System.<span class="ST2">out</span>.println(<span class="ST3">f</span><span class="ST3">indMaxOrderedSubString</span>(<span class="string">&quot;</span><span class="string">Welcome!</span><span class="string">&quot;</span>));
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,30 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.lab_calebfontenot_maximumorderedstring;
import java.util.Comparator;
/**
*
* @author caleb
*/
public class CompareSize implements Comparator {
@Override
public int compare(Object o1, Object o2)
{
int str1 = ((String) o1).length();
int str2 = ((String) o2).length();
if (str1 > str2) {
return -1;
} else if(str1 == str2) {
return 0;
} else if (str1 < str2) {
return 1;
}
return Integer.MAX_VALUE;
}
}

View File

@@ -15,11 +15,13 @@ import java.util.Scanner;
*/
public class FindMax {
public static String findMax(String input) {
int iterationCount = 0;
ArrayList<String> possibleSubStr = new ArrayList<>();
String currentStr = "";
for (int i = 0; i < input.length() -2; ++i) {
currentStr = input.charAt(i) + "";
for(int j = i + 1; j < input.length() -1; ++j) {
iterationCount++;
if (input.toLowerCase().charAt(i) < input.toLowerCase().charAt(j)) {
currentStr += input.charAt(j);
} else {
@@ -28,8 +30,9 @@ public class FindMax {
}
}
}
System.out.println("Iteration count: " + iterationCount);
System.out.println(possibleSubStr);
Collections.sort(possibleSubStr, new CompareSize());
//System.out.println(possibleSubStr);
return possibleSubStr.get(0);
}
public static void main(String[] args)
@@ -41,22 +44,4 @@ public class FindMax {
System.out.print("The maximum sorted subString is: ");
System.out.println(findMax(inputString));
}
}
class CompareSize implements Comparator {
@Override
public int compare(Object o1, Object o2)
{
int str1 = ((String) o1).length();
int str2 = ((String) o2).length();
if (str1 > str2) {
return -1;
} else if(str1 == str2) {
return 0;
} else if (str1 < str2) {
return 1;
}
return Integer.MAX_VALUE;
}
}

View File

@@ -0,0 +1,38 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.lab_calebfontenot_maximumorderedstring;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
*
* @author caleb
*/
public class FindMaxOrderedSubstring {
public static String findMaxOrderedSubString(String input) {
String outputString = "";
int j = 0;
for (int i = 0; i < input.length(); ++i) {
if (i == 0) {
outputString += input.charAt(i);
}
if (input.charAt(i) > outputString.charAt(j)) {
System.out.println(++j);
outputString += input.charAt(i);
}
}
return outputString;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String inputStr = input.nextLine();
System.out.println(findMaxOrderedSubString(inputStr));
}
}

View File

@@ -0,0 +1,105 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.lab_calebfontenot_maximumorderedstring;
/**
*
* @author caleb
*/
import java.util.Comparator;
public class GenericMergeSort {
public static <E extends Comparable<E>> void mergeSort(E[] list)
{
mergeSort(list,
new Comparator<E>() {
@Override
public int compare(E e1, E e2)
{
return ((Comparable<E>) e1).compareTo(e2);
}
});
}
static int recursiveCallCount = 0;
public static <E> void mergeSort(E[] list,
Comparator<? super E> comparator)
{
if (list.length > 1) {
recursiveCallCount++;
// Merge sort the first half
E[] firstHalf = (E[]) new Object[list.length / 2];
System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
mergeSort(firstHalf, comparator);
// Merge sort the second half
int secondHalfLength = list.length - list.length / 2;
E[] secondHalf = (E[]) new Object[secondHalfLength];
System.arraycopy(list, list.length / 2,
secondHalf, 0, secondHalfLength);
mergeSort(secondHalf, comparator);
// Merge firstHalf with secondHalf
E[] temp = merge1(firstHalf, secondHalf, comparator);
System.arraycopy(temp, 0, list, 0, temp.length);
}
}
private static <E> E[]
merge1(E[] list1, E[] list2, Comparator<? super E> comparator)
{
E[] temp = (E[]) new Object[list1.length + list2.length];
int current1 = 0; // Index in list1
int current2 = 0; // Index in list2
int current3 = 0; // Index in temp
while (current1 < list1.length && current2 < list2.length) {
if (comparator.compare(list1[current1], list2[current2]) < 0) {
temp[current3++] = list1[current1++];
} else {
temp[current3++] = list2[current2++];
}
}
while (current1 < list1.length) {
temp[current3++] = list1[current1++];
}
while (current2 < list2.length) {
temp[current3++] = list2[current2++];
}
return temp;
}
public static void main(String[] args)
{
Integer[] list
= {
2, 3, 2, 5, 6, 1, -2, 3, 14, 12
};
mergeSort(list);
System.out.println("number of recursive calls: " + recursiveCallCount);
recursiveCallCount = 0;
for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + " ");
}
System.out.println();
String[] list1
= {
"ABC", "abc", "abm", "Anf", "Good", "Bad", "nice"
};
mergeSort(list1, (s1, s2) -> s1.compareToIgnoreCase(s2));
System.out.println("number of recursive calls: " + recursiveCallCount);
recursiveCallCount = 0;
for (int i = 0; i < list1.length; i++) {
System.out.print(list1[i] + " ");
}
}
}

View File

@@ -0,0 +1,56 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.lab_calebfontenot_maximumorderedstring;
/**
*
* @author caleb
*/
public class JudeFindMax {
public static void main(String[] args) {
System.out.println(consecLetters("abcabcdgabxy"));
System.out.println(consecLetters("abcabcdgabmnsxy"));
System.out.println(consecLetters("abchjsfhajshfhijklmnopqjfaksfhkajhsf"));
System.out.println(consecLetters("hnafffgardghikortmmnmnmn"));
}
public static String consecLetters(String input)
{
String consec = "";
String maxConsec = "";
for (int i = 1; i < input.length(); i++) //n - 1
{
//If two in order
if (input.charAt(i) > input.charAt(i - 1))
{
//If length is zero then add previous as well
if (consec.length() == 0)
consec += input.charAt(i - 1);
consec += input.charAt(i);
}
//If not in order
else
{
//Check current consec length against maximum length
if (consec.length() > maxConsec.length())
{
//set new max
maxConsec = consec;
}
consec = "";
}
}
//Check current consec length against maximum length
if (consec.length() > maxConsec.length())
{
//set new max
maxConsec = consec;
}
return "Most letters found in order: " + maxConsec;
}
}

View File

@@ -0,0 +1,2 @@
#!/bin/bash
java --module-path /usr/lib --add-modules javafx.fxml,javafx.controls,javafx.media -XX:+UnlockExperimentalVMOptions -XX:+EagerJVMCI -XX:+EnableJVMCI -XX:+UseJVMCICompiler -XX:+UseJVMCINativeLibrary -Djava.awt.graphicsenv=net.java.openjdk.cacio.wayland.WaylandGraphicsEnvironment -jar dist/MP2-chapter4_Java20_CalebFontenot.jar

View File

@@ -0,0 +1,944 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>PriorityQueueASDV.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.comment {color: #808080}
.whitespace {color: #505050}
.ST2 {color: #9999ff; font-family: monospace; font-style: italic}
.ST5 {font-family: monospace; font-weight: bold; font-style: italic}
.string {color: #6a8759}
.number {color: #6897bb}
.ST1 {color: #9876aa}
.ST4 {color: #ffc66d}
.ST3 {color: #8a653b}
.ST6 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST7 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP6_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/mp6_calebfontenot/PriorityQueueASDV.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> edu.slcc.asdv.caleb.mp6_calebfontenot;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> java.util.Arrays;
<span class="literal">import</span> java.util.Collection;
<span class="literal">import</span> java.util.Iterator;
<span class="literal">import</span> java.util.List;
<span class="literal">import</span> java.util.NoSuchElementException;
<span class="literal">import</span> java.util.PriorityQueue;
<span class="literal">import</span> java.util.Queue;
<span class="literal">import</span> java.util.function.Consumer;
<span class="literal">public</span> <span class="literal">class</span> PriorityQueueASDV&lt;E <span class="literal">extends</span> Comparable&lt;E&gt;&gt;
<span class="literal">implements</span> Queue&lt;E&gt;, Cloneable {
<span class="literal">private</span> Node&lt;E&gt; <span class="ST1">head</span>;<span class="comment">//head</span>
<span class="literal">private</span> Node&lt;E&gt; <span class="ST1">tail</span>;<span class="comment">//tail</span>
<span class="literal">class</span> Node&lt;E&gt; {
E <span class="ST1">e</span>;
Node&lt;E&gt; <span class="ST1">l</span>;
Node&lt;E&gt; <span class="ST1">r</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Inserts</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">into</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">if</span> <span class="comment">it</span> <span class="comment">is</span> <span class="comment">possible</span> <span class="comment">to</span> <span class="comment">do</span> <span class="comment">so</span> <span class="comment">immediately</span> <span class="comment">without</span> <span class="comment">violating</span> <span class="comment">capacity</span> <span class="comment">restrictions</span><span class="comment">, </span><span class="comment">returning</span> <span class="comment">true</span> <span class="comment">upon</span> <span class="comment">success</span> <span class="comment">and</span> <span class="comment">throwing</span> <span class="comment">an</span> <span class="comment">IllegalStateException</span> <span class="comment">if</span> <span class="comment">no</span> <span class="comment">space</span> <span class="comment">is</span> <span class="comment">currently</span> <span class="comment">available</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">Specified</span> <span class="comment">by</span><span class="comment">: </span><span class="comment">add</span> <span class="comment">in</span> <span class="comment">interface</span> <span class="comment">Collection</span><span class="ST2">&lt;E&gt;</span>
<span class="comment"> * </span><span class="comment">Parameters</span><span class="comment">: </span><span class="comment">e</span><span class="comment"> - </span><span class="comment">the</span> <span class="comment">element</span> <span class="comment">to</span> <span class="comment">add</span> <span class="comment">Returns</span><span class="comment">: </span><span class="comment">true</span><span class="comment"> (</span><span class="comment">as</span> <span class="comment">specified</span> <span class="comment">by</span> <span class="comment">Collection</span><span class="comment">.</span><span class="comment">add</span><span class="comment">(</span><span class="comment">E</span><span class="comment">)) </span><span class="comment">Throws</span><span class="comment">: </span><span class="comment">IllegalStateException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">element</span> <span class="comment">cannot</span> <span class="comment">be</span> <span class="comment">added</span> <span class="comment">at</span> <span class="comment">this</span> <span class="comment">time</span> <span class="comment">due</span> <span class="comment">to</span> <span class="comment">capacity</span> <span class="comment">restrictions</span> <span class="comment">ClassCastException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">class</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">prevents</span> <span class="comment">it</span> <span class="comment">from</span> <span class="comment">being</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">elements</span> <span class="comment">IllegalArgumentException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">some</span> <span class="comment">property</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">element</span> <span class="comment">prevents</span> <span class="comment">it</span> <span class="comment">from</span> <span class="comment">being</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">queue</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">e</span><span class="comment"> - </span><span class="comment">the</span> <span class="comment">element</span> <span class="comment">to</span> <span class="comment">add</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">changed</span> <span class="comment">as</span> <span class="comment">a</span> <span class="comment">result</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">call</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">IllegalStateException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">element</span> <span class="comment">cannot</span> <span class="comment">be</span> <span class="comment">added</span> <span class="comment">at</span> <span class="comment">this</span> <span class="comment">time</span> <span class="comment">due</span> <span class="comment">to</span> <span class="comment">capacity</span> <span class="comment">restrictions</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ClassCastException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">class</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">elements</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">IllegalArgumentException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">some</span> <span class="comment">property</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">element</span> <span class="comment">prevents</span> <span class="comment">it</span> <span class="comment">from</span> <span class="comment">being</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">queue</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">add</span>(E e) {
<span class="literal">if</span> (e == <span class="literal">null</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">NULL elements not allowed!</span><span class="string">&quot;</span>);
}
Node&lt;E&gt; newNode = <span class="literal">new</span> Node&lt;E&gt;();
newNode.<span class="ST1">e</span> = e;
<span class="comment">//1. empty queue</span>
<span class="literal">if</span> (<span class="literal">this</span>.<span class="ST1">head</span> == <span class="literal">null</span> &amp;&amp; <span class="literal">this</span>.<span class="ST1">tail</span> == <span class="literal">null</span>) {
<span class="literal">this</span>.<span class="ST1">head</span> = <span class="literal">this</span>.<span class="ST1">tail</span> = newNode;
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="literal">int</span> index = findCorrectPositionToInsertElement(e);
<span class="comment">//int index = findCorrectPositionToInsertElementHashCode(e);</span>
<span class="comment">//2. we add at size ( last node)</span>
<span class="literal">if</span> (index == size()) {
<span class="ST1">tail</span>.<span class="ST1">r</span> = newNode;<span class="comment">//1</span>
newNode.<span class="ST1">l</span> = <span class="ST1">tail</span>;<span class="comment">//2</span>
<span class="ST1">tail</span> = newNode;<span class="comment">//3</span>
} <span class="comment">//3. we add at 0 in the front</span>
<span class="literal">else</span> <span class="literal">if</span> (index == <span class="number">0</span>) {
newNode.<span class="ST1">r</span> = <span class="ST1">head</span>;
<span class="literal">this</span>.<span class="ST1">head</span>.<span class="ST1">l</span> = newNode;
<span class="literal">this</span>.<span class="ST1">head</span> = newNode;
<span class="literal">if</span> (size() == <span class="number">1</span>) {
<span class="ST1">tail</span> = <span class="ST1">head</span>;
}
} <span class="comment">//4. we add in the middle</span>
<span class="literal">else</span> {
Node&lt;E&gt; p = <span class="ST1">head</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; index - <span class="number">1</span>; ++i) {
p = p.<span class="ST1">r</span>;
}
<span class="comment">//after for loop p point one position before insertion</span>
newNode.<span class="ST1">l</span> = p;<span class="comment">//we connect the left of the new node </span>
<span class="comment">//to the node that is BEFORE </span>
<span class="comment">//the node to be inserted</span>
newNode.<span class="ST1">r</span> = p.<span class="ST1">r</span>;<span class="comment">//we connect the right of the new node</span>
<span class="comment">// to the node thta is AFTER </span>
<span class="comment">//the node to be inserted</span>
p.<span class="ST1">r</span> = newNode; <span class="comment">//we connect the right the node BEFORE the node</span>
<span class="comment">//to be inserted to the new node</span>
p.<span class="ST1">r</span>.<span class="ST1">r</span>.<span class="ST1">l</span> = newNode;<span class="comment">//we connect the left of the node AFTER the node </span>
<span class="comment">//to be iserted to the new node</span>
}
<span class="literal">return</span> <span class="literal">true</span>;
}
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST4">size</span>() {
Node&lt;E&gt; p = <span class="literal">this</span>.<span class="ST1">head</span>;
<span class="literal">int</span> count = <span class="number">0</span>;
<span class="literal">while</span> (p != <span class="literal">null</span>) {
p = p.<span class="ST1">r</span>;
count++;
}
<span class="literal">return</span> count;
}
<span class="literal">private</span> <span class="literal">int</span> <span class="ST4">findCorrectPositionToInsertElement</span>(E e) {
Node&lt;E&gt; p = <span class="literal">this</span>.<span class="ST1">head</span>;
<span class="literal">int</span> pos = <span class="number">0</span>;
<span class="literal">while</span> (p != <span class="literal">null</span>) {
<span class="literal">if</span> (e.compareTo(p.<span class="ST1">e</span>) &gt; <span class="number">0</span>) {
p = p.<span class="ST1">r</span>;
++pos;
} <span class="literal">else</span> {
<span class="literal">break</span>;
}
}
<span class="literal">return</span> pos;
}
<span class="literal">private</span> <span class="literal">int</span> <span class="comment">findCorrectPositionToInsertElementHashCode</span>(E e) {
Node&lt;E&gt; p = <span class="literal">this</span>.<span class="ST1">head</span>;
<span class="literal">int</span> pos = <span class="number">0</span>;
<span class="literal">while</span> (p != <span class="literal">null</span>) {
<span class="literal">if</span> (e.hashCode() &gt; p.<span class="ST1">e</span>.hashCode()) {
p = p.<span class="ST1">r</span>;
++pos;
} <span class="literal">else</span> {
<span class="literal">break</span>;
}
}
<span class="literal">return</span> pos;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Inserts</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">into</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">if</span> <span class="comment">it</span> <span class="comment">is</span> <span class="comment">possible</span> <span class="comment">to</span> <span class="comment">do</span> <span class="comment">so</span> <span class="comment">immediately</span> <span class="comment">without</span> <span class="comment">violating</span> <span class="comment">capacity</span> <span class="comment">restrictions</span><span class="comment">.</span> <span class="comment">When</span> <span class="comment">using</span> <span class="comment">a</span> <span class="comment">capacity</span><span class="comment">-</span><span class="comment">restricted</span> <span class="comment">queue</span><span class="comment">, </span><span class="comment">this</span> <span class="comment">method</span> <span class="comment">is</span> <span class="comment">generally</span> <span class="comment">preferable</span> <span class="comment">to</span> <span class="comment">add</span><span class="comment">(</span><span class="comment">E</span><span class="comment">), </span><span class="comment">which</span> <span class="comment">can</span> <span class="comment">fail</span> <span class="comment">to</span> <span class="comment">insert</span> <span class="comment">an</span> <span class="comment">element</span> <span class="comment">only</span> <span class="comment">by</span> <span class="comment">throwing</span> <span class="comment">an</span> <span class="comment">exception</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">e</span><span class="comment"> - </span><span class="comment">the</span> <span class="comment">element</span> <span class="comment">to</span> <span class="comment">add</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">IllegalStateException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">element</span> <span class="comment">cannot</span> <span class="comment">be</span> <span class="comment">added</span> <span class="comment">at</span> <span class="comment">this</span> <span class="comment">time</span> <span class="comment">due</span> <span class="comment">to</span> <span class="comment">capacity</span> <span class="comment">restrictions</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ClassCastException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">class</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">elements</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">IllegalArgumentException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">some</span> <span class="comment">property</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">element</span> <span class="comment">prevents</span> <span class="comment">it</span> <span class="comment">from</span> <span class="comment">being</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">queue</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">element</span> <span class="comment">was</span> <span class="comment">added</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">offer</span>(E e) {
<span class="literal">return</span> add(e);
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Retrieves</span> <span class="comment">and</span> <span class="comment">removes</span> <span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span><span class="comment">.</span> <span class="comment">This</span> <span class="comment">method</span> <span class="comment">differs</span> <span class="comment">from</span><span class="comment"> {</span><span class="comment">@link</span> <span class="comment">#</span><span class="comment">poll</span> <span class="comment">poll</span><span class="comment">} </span><span class="comment">only</span> <span class="comment">in</span> <span class="comment">that</span> <span class="comment">it</span> <span class="comment">throws</span> <span class="comment">an</span> <span class="comment">exception</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;p&gt;</span>
<span class="comment"> * </span><span class="comment">This</span> <span class="comment">implementation</span> <span class="comment">returns</span> <span class="comment">the</span> <span class="comment">result</span> <span class="comment">of</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">poll</span><span class="comment">}</span> <span class="comment">unless</span> <span class="comment">the</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NoSuchElementException</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> E <span class="ST4">remove</span>() {
Node&lt;E&gt; <span class="comment">pointer</span> = <span class="ST1">head</span>.<span class="ST1">r</span>;
E removedElement = <span class="ST1">head</span>.<span class="ST1">e</span>;
<span class="ST1">head</span> = <span class="ST1">head</span>.<span class="ST1">r</span>;
<span class="ST1">head</span>.<span class="ST1">l</span> = <span class="literal">null</span>;
<span class="literal">return</span> removedElement;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Retrieves</span> <span class="comment">and</span> <span class="comment">removes</span> <span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">returns</span> <span class="comment">null</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">Returns</span><span class="comment">: </span><span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">null</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> E <span class="ST4">poll</span>() {
<span class="literal">if</span> (size() == <span class="number">0</span>) {
<span class="literal">return</span> <span class="literal">null</span>;
}
<span class="literal">if</span> (size() &gt; <span class="number">1</span>) {
<span class="ST1">head</span> = <span class="ST1">head</span>.<span class="ST1">r</span>;
E e = <span class="ST1">head</span>.<span class="ST1">l</span>.<span class="ST1">e</span>;
<span class="ST1">head</span>.<span class="ST1">l</span> = <span class="literal">null</span>;
<span class="literal">return</span> e;
} <span class="literal">else</span> <span class="comment">//size 1</span>
{
E e = <span class="ST1">head</span>.<span class="ST1">e</span>;
<span class="ST1">head</span> = <span class="ST1">tail</span> = <span class="literal">null</span>;
<span class="literal">return</span> e;
}
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Retrieves</span><span class="comment">, </span><span class="comment">but</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">remove</span><span class="comment">, </span><span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span><span class="comment">.</span> <span class="comment">This</span> <span class="comment">method</span> <span class="comment">differs</span> <span class="comment">from</span><span class="comment"> {</span><span class="comment">@link</span> <span class="comment">#</span><span class="comment">peek</span> <span class="comment">peek</span><span class="comment">} </span><span class="comment">only</span> <span class="comment">in</span> <span class="comment">that</span> <span class="comment">it</span> <span class="comment">throws</span> <span class="comment">an</span> <span class="comment">exception</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NoSuchElementException</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> E <span class="ST4">element</span>() {
<span class="literal">if</span> (<span class="ST1">head</span> != <span class="literal">null</span>) {
<span class="literal">return</span> (E) <span class="ST1">head</span>;
} <span class="literal">else</span> {
<span class="literal">throw</span> <span class="literal">new</span> NoSuchElementException(<span class="string">&quot;</span><span class="string">Element does not exist.</span><span class="string">&quot;</span>);
}
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Retrieves</span><span class="comment">, </span><span class="comment">but</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">remove</span><span class="comment">, </span><span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">returns</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">null</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">head</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">queue</span><span class="comment">, </span><span class="comment">or</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">null</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">queue</span> <span class="comment">is</span> <span class="comment">empty</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> E <span class="ST4">peek</span>() {
<span class="literal">return</span> (E) <span class="ST1">head</span>;
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">isEmpty</span>() {
<span class="literal">return</span> <span class="ST1">head</span> == <span class="literal">null</span> &amp;&amp; <span class="ST1">tail</span> == <span class="literal">null</span> ? <span class="literal">true</span> : <span class="literal">false</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">true</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span><span class="comment">.</span> <span class="comment">More</span> <span class="comment">formally</span><span class="comment">, </span><span class="comment">returns</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">true</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">and</span> <span class="comment">only</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">at</span> <span class="comment">least</span> <span class="comment">one</span> <span class="comment">element</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">e</span><span class="comment">}</span> <span class="comment">such</span> <span class="comment">that</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">Objects</span><span class="comment">.</span><span class="comment">equals</span><span class="comment">(</span><span class="comment">o</span><span class="comment">, </span><span class="comment">e</span><span class="comment">)</span><span class="comment">}</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">o</span> <span class="comment">element</span> <span class="comment">whose</span> <span class="comment">presence</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">tested</span>
<span class="comment"> * </span><span class="comment">@return</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">true</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ClassCastException</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">type</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">is</span> <span class="comment">incompatible</span> <span class="comment">with</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment"> ({</span><span class="comment">@linkplain</span> <span class="comment">Collection</span><span class="comment">#</span><span class="comment">#</span><span class="comment">optional</span><span class="comment">-</span><span class="comment">restrictions</span> <span class="comment">optional</span><span class="comment">})</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">elements</span><span class="comment"> ({</span><span class="comment">@linkplain</span> <span class="comment">Collection</span><span class="comment">#</span><span class="comment">#</span><span class="comment">optional</span><span class="comment">-</span><span class="comment">restrictions</span> <span class="comment">optional</span><span class="comment">})</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">contains</span>(Object o) {
Node&lt;E&gt; pointer = <span class="ST1">head</span>;
<span class="literal">do</span> {
<span class="literal">if</span> (pointer.equals(o)) {
<span class="literal">return</span> <span class="literal">true</span>;
} <span class="literal">else</span> {
pointer = pointer.<span class="ST1">r</span>;
}
} <span class="literal">while</span> (pointer != <span class="literal">null</span>);
<span class="literal">return</span> <span class="literal">false</span>;
}
@Override
<span class="literal">public</span> Iterator&lt;E&gt; <span class="ST4">iterator</span>() {
Iterator&lt;E&gt; it = <span class="literal">new</span> Iterator&lt;E&gt;() {
Node&lt;E&gt; <span class="ST1">p</span> = <span class="ST1">head</span>;
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">hasNext</span>() {
<span class="literal">return</span> <span class="ST1">p</span> == <span class="literal">null</span> ? <span class="literal">false</span> : <span class="literal">true</span>;
}
@Override
<span class="literal">public</span> E <span class="ST4">next</span>() {
<span class="literal">if</span> (hasNext() == <span class="literal">false</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NoSuchElementException(<span class="string">&quot;</span><span class="string">the is no next element</span><span class="string">&quot;</span>);
}
E e = <span class="ST1">p</span>.<span class="ST1">e</span>;
<span class="ST1">p</span> = <span class="ST1">p</span>.<span class="ST1">r</span>;
<span class="literal">return</span> e;
}
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">forEachRemaining</span>(Consumer&lt;? <span class="literal">super</span> E&gt; action) {
<span class="literal">while</span> (hasNext()) {
action.accept(<span class="ST1">p</span>.<span class="ST1">e</span>);
<span class="ST1">p</span> = <span class="ST1">p</span>.<span class="ST1">r</span>;
}
}
};
<span class="literal">return</span> it;
}
@Override
<span class="literal">public</span> Object[] <span class="ST4">toArray</span>() {
Node&lt;E&gt; pointer = <span class="ST1">head</span>;
Object[] returnArray = <span class="literal">new</span> Object[<span class="literal">this</span>.size()];
<span class="literal">int</span> i = <span class="number">0</span>;
<span class="literal">while</span> (pointer.<span class="ST1">r</span> != <span class="literal">null</span>) {
returnArray[i++] = pointer.<span class="ST1">e</span>;
pointer = pointer.<span class="ST1">r</span>;
}
returnArray[i++] = pointer.<span class="ST1">e</span>;
<span class="literal">return</span> returnArray;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">an</span> <span class="comment">array</span> <span class="comment">containing</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">; </span><span class="comment">the</span> <span class="comment">runtime</span> <span class="comment">type</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">returned</span> <span class="comment">array</span> <span class="comment">is</span> <span class="comment">that</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">array</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">collection</span> <span class="comment">fits</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">array</span><span class="comment">, </span><span class="comment">it</span> <span class="comment">is</span> <span class="comment">returned</span> <span class="comment">therein</span><span class="comment">.</span> <span class="comment">Otherwise</span><span class="comment">, </span><span class="comment">a</span> <span class="comment">new</span> <span class="comment">array</span> <span class="comment">is</span> <span class="comment">allocated</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">runtime</span> <span class="comment">type</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">array</span> <span class="comment">and</span> <span class="comment">the</span> <span class="comment">size</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;p&gt;</span>
<span class="comment"> * </span><span class="comment">If</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">fits</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">array</span> <span class="comment">with</span> <span class="comment">room</span> <span class="comment">to</span> <span class="comment">spare</span><span class="comment"> (</span><span class="comment">i</span><span class="comment">.</span><span class="comment">e</span><span class="comment">.</span><span class="comment">, </span><span class="comment">the</span> <span class="comment">array</span> <span class="comment">has</span> <span class="comment">more</span> <span class="comment">elements</span> <span class="comment">than</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">), </span><span class="comment">the</span> <span class="comment">element</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">immediately</span> <span class="comment">following</span> <span class="comment">the</span> <span class="comment">end</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">set</span> <span class="comment">to</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">null</span><span class="comment">}</span><span class="comment">.</span><span class="comment"> (</span><span class="comment">This</span> <span class="comment">is</span> <span class="comment">useful</span> <span class="comment">in</span> <span class="comment">determining</span> <span class="comment">the</span> <span class="comment">length</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="ST2">&lt;i&gt;</span><span class="comment">only</span><span class="ST2">&lt;/i&gt;</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">caller</span> <span class="comment">knows</span> <span class="comment">that</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">contain</span> <span class="comment">any</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">null</span><span class="comment">}</span> <span class="comment">elements</span><span class="comment">.</span><span class="comment">)</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;p&gt;</span>
<span class="comment"> * </span><span class="comment">If</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">makes</span> <span class="comment">any</span> <span class="comment">guarantees</span> <span class="comment">as</span> <span class="comment">to</span> <span class="comment">what</span> <span class="comment">order</span> <span class="comment">its</span> <span class="comment">elements</span> <span class="comment">are</span> <span class="comment">returned</span> <span class="comment">by</span> <span class="comment">its</span> <span class="comment">iterator</span><span class="comment">, </span><span class="comment">this</span> <span class="comment">method</span> <span class="comment">must</span> <span class="comment">return</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">same</span> <span class="comment">order</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@apiNote</span> <span class="comment">This</span> <span class="comment">method</span> <span class="comment">acts</span> <span class="comment">as</span> <span class="comment">a</span> <span class="comment">bridge</span> <span class="comment">between</span> <span class="comment">array</span><span class="comment">-</span><span class="comment">based</span> <span class="comment">and</span> <span class="comment">collection</span><span class="comment">-</span><span class="comment">based</span> <span class="comment">APIs</span><span class="comment">.</span> <span class="comment">It</span> <span class="comment">allows</span> <span class="comment">an</span> <span class="comment">existing</span> <span class="comment">array</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">reused</span> <span class="comment">under</span> <span class="comment">certain</span> <span class="comment">circumstances</span><span class="comment">.</span> <span class="comment">Use</span><span class="comment"> {</span><span class="comment">@link</span> <span class="comment">#</span><span class="comment">toArray</span><span class="comment">()} </span><span class="comment">to</span> <span class="comment">create</span> <span class="comment">an</span> <span class="comment">array</span> <span class="comment">whose</span> <span class="comment">runtime</span> <span class="comment">type</span> <span class="comment">is</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">Object</span><span class="comment">[]</span><span class="comment">}, </span><span class="comment">or</span> <span class="comment">use</span><span class="comment"> {</span><span class="comment">@link</span> <span class="comment">#</span><span class="comment">toArray</span><span class="comment">(</span><span class="comment">IntFunction</span><span class="comment">)} </span><span class="comment">to</span> <span class="comment">control</span> <span class="comment">the</span> <span class="comment">runtime</span> <span class="comment">type</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">array</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;p&gt;</span>
<span class="comment"> * </span><span class="comment">Suppose</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">x</span><span class="comment">}</span> <span class="comment">is</span> <span class="comment">a</span> <span class="comment">collection</span> <span class="comment">known</span> <span class="comment">to</span> <span class="comment">contain</span> <span class="comment">only</span> <span class="comment">strings</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">following</span> <span class="comment">code</span> <span class="comment">can</span> <span class="comment">be</span> <span class="comment">used</span> <span class="comment">to</span> <span class="comment">dump</span> <span class="comment">the</span> <span class="comment">collection</span> <span class="comment">into</span> <span class="comment">a</span> <span class="comment">previously</span> <span class="comment">allocated</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">String</span><span class="comment">}</span> <span class="comment">array</span><span class="comment">:</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;pre&gt;</span>
<span class="comment"> * </span><span class="comment">String</span><span class="comment">[] </span><span class="comment">y</span><span class="comment"> = </span><span class="comment">new</span> <span class="comment">String</span><span class="comment">[</span><span class="comment">SIZE</span><span class="comment">];</span>
<span class="comment"> * </span><span class="comment">.</span><span class="comment">.</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">y</span><span class="comment"> = </span><span class="comment">x</span><span class="comment">.</span><span class="comment">toArray</span><span class="comment">(</span><span class="comment">y</span><span class="comment">);</span><span class="ST2">&lt;/pre&gt;</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;p&gt;</span>
<span class="comment"> * </span><span class="comment">The</span> <span class="comment">return</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">reassigned</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">variable</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">y</span><span class="comment">}</span><span class="comment">, </span><span class="comment">because</span> <span class="comment">a</span> <span class="comment">new</span> <span class="comment">array</span> <span class="comment">will</span> <span class="comment">be</span> <span class="comment">allocated</span> <span class="comment">and</span> <span class="comment">returned</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">collection</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">x</span><span class="comment">}</span> <span class="comment">has</span> <span class="comment">too</span> <span class="comment">many</span> <span class="comment">elements</span> <span class="comment">to</span> <span class="comment">fit</span> <span class="comment">into</span> <span class="comment">the</span> <span class="comment">existing</span> <span class="comment">array</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">y</span><span class="comment">}</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;p&gt;</span>
<span class="comment"> * </span><span class="comment">Note</span> <span class="comment">that</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">toArray</span><span class="comment">(</span><span class="comment">new</span> <span class="comment">Object</span><span class="comment">[0])</span><span class="comment">} </span><span class="comment">is</span> <span class="comment">identical</span> <span class="comment">in</span> <span class="comment">function</span> <span class="comment">to</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">toArray</span><span class="comment">()</span><span class="comment">}</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">&lt;T&gt;</span> <span class="comment">the</span> <span class="comment">component</span> <span class="comment">type</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">to</span> <span class="comment">contain</span> <span class="comment">the</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">a</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">into</span> <span class="comment">which</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">are</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">stored</span><span class="comment">, </span><span class="comment">if</span> <span class="comment">it</span> <span class="comment">is</span> <span class="comment">big</span> <span class="comment">enough</span><span class="comment">; </span><span class="comment">otherwise</span><span class="comment">, </span><span class="comment">a</span> <span class="comment">new</span> <span class="comment">array</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">same</span> <span class="comment">runtime</span> <span class="comment">type</span> <span class="comment">is</span> <span class="comment">allocated</span> <span class="comment">for</span> <span class="comment">this</span> <span class="comment">purpose</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">an</span> <span class="comment">array</span> <span class="comment">containing</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ArrayStoreException</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">runtime</span> <span class="comment">type</span> <span class="comment">of</span> <span class="comment">any</span> <span class="comment">element</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">not</span> <span class="comment">assignable</span> <span class="comment">to</span> <span class="comment">the</span><span class="comment"> {</span><span class="comment">@linkplain</span> <span class="comment">Class</span><span class="comment">#</span><span class="comment">getComponentType</span>
<span class="comment"> * </span><span class="comment">runtime</span> <span class="comment">component</span> <span class="comment">type</span><span class="comment">} </span><span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">array</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">array</span> <span class="comment">is</span> <span class="comment">null</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> &lt;T&gt; T[] <span class="ST4">toArray</span>(T[] a) {
a = Arrays.<span class="ST5">copyOf</span>(a, <span class="literal">t</span><span class="literal">his</span>.size());
Node&lt;E&gt; pointer = <span class="ST1">head</span>;
System.<span class="ST6">out</span>.println(a.getClass());
System.<span class="ST6">out</span>.println(pointer.getClass());
System.<span class="ST6">out</span>.println(pointer.<span class="ST1">e</span>.getClass());
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="literal">this</span>.size(); ++i) {
a[i] = (T) pointer.<span class="ST1">e</span>;
pointer = pointer.<span class="ST1">r</span>;
}
<span class="literal">return</span> a;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">a</span> <span class="comment">single</span> <span class="comment">instance</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">from</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">, </span><span class="comment">if</span> <span class="comment">it</span> <span class="comment">is</span> <span class="comment">present</span><span class="comment"> (</span><span class="comment">optional</span> <span class="comment">operation</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">More</span> <span class="comment">formally</span><span class="comment">, </span><span class="comment">removes</span> <span class="comment">an</span> <span class="comment">element</span> <span class="comment">e</span> <span class="comment">such</span> <span class="comment">that</span><span class="comment"> (</span><span class="comment">o</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> ? </span><span class="comment">e</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> : </span><span class="comment">o</span><span class="comment">.</span><span class="comment">equals</span><span class="comment">(</span><span class="comment">e</span><span class="comment">)), </span><span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">one</span> <span class="comment">or</span> <span class="comment">more</span> <span class="comment">such</span> <span class="comment">elements</span><span class="comment">.</span> <span class="comment">Returns</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contained</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span><span class="comment"> (</span><span class="comment">or</span> <span class="comment">equivalently</span><span class="comment">, </span><span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">changed</span> <span class="comment">as</span> <span class="comment">a</span> <span class="comment">result</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">call</span><span class="comment">)</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">o</span><span class="comment"> - </span><span class="comment">element</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">removed</span> <span class="comment">from</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">, </span><span class="comment">if</span> <span class="comment">present</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ClassCastException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">type</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">is</span> <span class="comment">incompatible</span> <span class="comment">with</span> <span class="comment">this</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">element</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">elements</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">an</span> <span class="comment">element</span> <span class="comment">was</span> <span class="comment">removed</span> <span class="comment">as</span> <span class="comment">a</span> <span class="comment">result</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">call</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">remove</span>(Object o) {
<span class="literal">if</span> (o == <span class="literal">null</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">null vales not allowed</span><span class="string">&quot;</span>);
}
<span class="literal">if</span> (size() == <span class="number">0</span>) {
<span class="literal">return</span> <span class="literal">false</span>;
}
Node&lt;E&gt; p = <span class="literal">this</span>.<span class="ST1">head</span>;
<span class="literal">int</span> pos = <span class="number">0</span>;
<span class="literal">while</span> (p != <span class="literal">this</span>.<span class="ST1">tail</span>) {
<span class="literal">if</span> (p.<span class="ST1">e</span>.equals(o)) {
<span class="literal">if</span> (size() == <span class="number">1</span>) {
<span class="literal">this</span>.<span class="ST1">head</span> = <span class="literal">this</span>.<span class="ST1">tail</span> = <span class="literal">null</span>;
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="literal">this</span>.removeAt(pos, (E) o);
<span class="literal">break</span>;
}
++pos;
p = p.<span class="ST1">r</span>;
}
<span class="literal">if</span> (p == <span class="ST1">tail</span> &amp;&amp; p.<span class="ST1">e</span>.equals(o)) {
<span class="literal">this</span>.removeAt(size() - <span class="number">1</span>, (E) o);
}
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">pos</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">e</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">IndexOutOfBoundsException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">pos</span> <span class="comment">less</span><span class="comment"> 0 </span><span class="comment">OR</span> <span class="comment">pos</span> <span class="comment">greater</span><span class="comment">-</span><span class="comment">equal</span> <span class="comment">size</span><span class="comment">()</span>
<span class="comment"> * </span><span class="comment">@return</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">boolean</span> <span class="ST4">removeAt</span>(<span class="literal">int</span> pos, E <span class="comment">e</span>) {
<span class="literal">if</span> (pos &lt; <span class="number">0</span> || pos &gt;= size()) {
<span class="literal">throw</span> <span class="literal">new</span> IndexOutOfBoundsException(pos + <span class="string">&quot;</span><span class="string"> is out of bounds</span><span class="string">&quot;</span>);
}
<span class="comment">//1.list is empty</span>
<span class="literal">if</span> (isEmpty()) {
<span class="literal">return</span> <span class="literal">false</span>;
} <span class="comment">//2. one node exists</span>
<span class="literal">else</span> <span class="literal">if</span> (size() == <span class="number">1</span>) {
<span class="literal">this</span>.<span class="ST1">head</span> = <span class="literal">this</span>.<span class="ST1">tail</span> = <span class="literal">null</span>;
} <span class="comment">//3. remove in the front( head)</span>
<span class="literal">else</span> <span class="literal">if</span> (pos == <span class="number">0</span>) {
<span class="literal">this</span>.<span class="ST1">head</span> = <span class="literal">this</span>.<span class="ST1">head</span>.<span class="ST1">r</span>;
<span class="ST1">head</span>.<span class="ST1">l</span> = <span class="literal">null</span>;
} <span class="comment">//4. remove in the end ( tail)</span>
<span class="literal">else</span> <span class="literal">if</span> (pos == size() - <span class="number">1</span>) {
<span class="literal">this</span>.<span class="ST1">tail</span> = <span class="literal">this</span>.<span class="ST1">tail</span>.<span class="ST1">l</span>;
<span class="literal">this</span>.<span class="ST1">tail</span>.<span class="ST1">r</span> = <span class="literal">null</span>;
} <span class="comment">//5. remove in the middle ( at least 3 nodes are in the queue)</span>
<span class="literal">else</span> {
Node&lt;E&gt; p = <span class="ST1">head</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; pos - <span class="number">1</span>; ++i) {
p = p.<span class="ST1">r</span>;
}
p.<span class="ST1">r</span> = p.<span class="ST1">r</span>.<span class="ST1">r</span>;
p.<span class="ST1">r</span>.<span class="ST1">l</span> = p;
}
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">true</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">c</span> <span class="comment">collection</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">checked</span> <span class="comment">for</span> <span class="comment">containment</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@return</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">true</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ClassCastException</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">types</span> <span class="comment">of</span> <span class="comment">one</span> <span class="comment">or</span> <span class="comment">more</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">are</span> <span class="comment">incompatible</span> <span class="comment">with</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment"> ({</span><span class="comment">@linkplain</span> <span class="comment">Collection</span><span class="comment">#</span><span class="comment">#</span><span class="comment">optional</span><span class="comment">-</span><span class="comment">restrictions</span> <span class="comment">optional</span><span class="comment">})</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">one</span> <span class="comment">or</span> <span class="comment">more</span> <span class="comment">null</span> <span class="comment">elements</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">elements</span><span class="comment"> ({</span><span class="comment">@linkplain</span> <span class="comment">Collection</span><span class="comment">#</span><span class="comment">#</span><span class="comment">optional</span><span class="comment">-</span><span class="comment">restrictions</span> <span class="comment">optional</span><span class="comment">}) </span><span class="comment">or</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">null</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">@see</span> <span class="comment">#</span><span class="comment">contains</span><span class="comment">(</span><span class="comment">Object</span><span class="comment">)</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">containsAll</span>(Collection&lt;?&gt; c) {
<span class="comment">// Java already throws a CastCastException if you give it the wrong type, so we don&#39;t have to throw that ourselves</span>
<span class="literal">if</span> (c.contains(<span class="literal">n</span><span class="literal">ull</span>) || c == <span class="literal">null</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">The collection you passed to containsAll() contains a null element. Cannot continue.</span><span class="string">&quot;</span>);
}
<span class="comment">// Unpack the collection so we can compare them</span>
Object[] compareArray = c.toArray();
Node&lt;E&gt; pointer = <span class="literal">null</span>;
<span class="literal">int</span> matchCount = <span class="number">0</span>;
<span class="literal">for</span> (Object compare : compareArray) {
pointer = <span class="ST1">head</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; size() - <span class="number">1</span>; ++i) {
<span class="literal">if</span> (pointer.<span class="ST1">e</span>.equals(compare)) {
matchCount++;
}
pointer = pointer.<span class="ST1">r</span>;
}
}
<span class="literal">if</span> (matchCount == compareArray.<span class="ST1">length</span> - <span class="number">1</span>) {
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Adds</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment"> (</span><span class="comment">optional</span> <span class="comment">operation</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">behavior</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">operation</span> <span class="comment">is</span> <span class="comment">undefined</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">modified</span> <span class="comment">while</span> <span class="comment">the</span> <span class="comment">operation</span> <span class="comment">is</span> <span class="comment">in</span> <span class="comment">progress</span><span class="comment">.</span><span class="comment"> (</span><span class="comment">This</span> <span class="comment">implies</span> <span class="comment">that</span> <span class="comment">the</span> <span class="comment">behavior</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">call</span> <span class="comment">is</span> <span class="comment">undefined</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">nonempty</span><span class="comment">.</span><span class="comment">)</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST3">c</span><span class="comment"> - </span><span class="comment">collection</span> <span class="comment">containing</span> <span class="comment">elements</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ClassCastException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">class</span> <span class="comment">of</span> <span class="comment">an</span> <span class="comment">element</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">prevents</span> <span class="comment">it</span> <span class="comment">from</span> <span class="comment">being</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">a</span> <span class="comment">null</span> <span class="comment">element</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">elements</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">null</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">IllegalArgumentException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">some</span> <span class="comment">property</span> <span class="comment">of</span> <span class="comment">an</span> <span class="comment">element</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">prevents</span> <span class="comment">it</span> <span class="comment">from</span> <span class="comment">being</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">IllegalArgumentException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">some</span> <span class="comment">property</span> <span class="comment">of</span> <span class="comment">an</span> <span class="comment">element</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span> <span class="comment">prevents</span> <span class="comment">it</span> <span class="comment">from</span> <span class="comment">being</span> <span class="comment">added</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">collection</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">changed</span> <span class="comment">as</span> <span class="comment">a</span> <span class="comment">result</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">call</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">addAll</span>(Collection&lt;? <span class="literal">extends</span> E&gt; c) {
<span class="literal">int</span> sizeBefore = size();
<span class="literal">for</span> (E e : c) {
add(e);
}
<span class="literal">int</span> sizeAfter = size();
<span class="literal">return</span> sizeAfter &gt; sizeBefore;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">elements</span> <span class="comment">that</span> <span class="comment">are</span> <span class="comment">also</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span><span class="comment"> (</span><span class="comment">optional</span> <span class="comment">operation</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">After</span> <span class="comment">this</span> <span class="comment">call</span> <span class="comment">returns</span><span class="comment">, </span><span class="comment">this</span> <span class="comment">collection</span> <span class="comment">will</span> <span class="comment">contain</span> <span class="comment">no</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">common</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@implSpec</span> <span class="comment">This</span> <span class="comment">implementation</span> <span class="comment">iterates</span> <span class="comment">over</span> <span class="comment">this</span> <span class="comment">collection</span><span class="comment">, </span><span class="comment">checking</span> <span class="comment">each</span> <span class="comment">element</span> <span class="comment">returned</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">iterator</span> <span class="comment">in</span> <span class="comment">turn</span> <span class="comment">to</span> <span class="comment">see</span> <span class="comment">if</span> <span class="comment">it</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">it</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">so</span> <span class="comment">contained</span><span class="comment">, </span><span class="comment">it</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">removed</span> <span class="comment">from</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">iterator</span><span class="comment">&#39;</span><span class="comment">s</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">remove</span><span class="comment">}</span> <span class="comment">method</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST2">&lt;p&gt;</span>
<span class="comment"> * </span><span class="comment">Note</span> <span class="comment">that</span> <span class="comment">this</span> <span class="comment">implementation</span> <span class="comment">will</span> <span class="comment">throw</span> <span class="comment">an</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">UnsupportedOperationException</span><span class="comment">}</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">iterator</span> <span class="comment">returned</span> <span class="comment">by</span> <span class="comment">the</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">iterator</span><span class="comment">}</span> <span class="comment">method</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">implement</span> <span class="comment">the</span><span class="comment"> {</span><span class="comment">@code</span> <span class="comment">remove</span><span class="comment">}</span> <span class="comment">method</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">collection</span> <span class="comment">contains</span> <span class="comment">one</span> <span class="comment">or</span> <span class="comment">more</span> <span class="comment">elements</span> <span class="comment">in</span> <span class="comment">common</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">UnsupportedOperationException</span><span class="comment"> {</span><span class="comment">@inheritDoc</span><span class="comment">}</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">ClassCastException</span><span class="comment"> {</span><span class="comment">@inheritDoc</span><span class="comment">}</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> {</span><span class="comment">@inheritDoc</span><span class="comment">}</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@see</span> <span class="comment">#</span><span class="comment">remove</span><span class="comment">(</span><span class="comment">Object</span><span class="comment">)</span>
<span class="comment"> * </span><span class="comment">@see</span> <span class="comment">#</span><span class="comment">contains</span><span class="comment">(</span><span class="comment">Object</span><span class="comment">)</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">removeAll</span>(Collection&lt;?&gt; c) {
<span class="literal">if</span> (c.contains(<span class="literal">n</span><span class="literal">ull</span>) || c == <span class="literal">null</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">The collection you passed to removeAll() contains a null element. Cannot continue.</span><span class="string">&quot;</span>);
}
<span class="comment">// Unpack the collection so we can remove them</span>
Object[] compareArray = c.toArray();
Node&lt;E&gt; pointer = <span class="literal">null</span>;
<span class="literal">boolean</span> removeSuccessful = <span class="literal">false</span>;
<span class="literal">for</span> (Object compare : compareArray) {
pointer = <span class="ST1">head</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; size() - <span class="number">1</span>; ++i) {
<span class="literal">if</span> (pointer.<span class="ST1">e</span>.equals(compare)) {
remove(pointer.<span class="ST1">e</span>);
removeSuccessful = <span class="literal">true</span>;
}
pointer = pointer.<span class="ST1">r</span>;
}
}
<span class="literal">return</span> removeSuccessful;
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">retainAll</span>(Collection&lt;?&gt; c) {
<span class="literal">if</span> (c.contains(<span class="literal">n</span><span class="literal">ull</span>) || c == <span class="literal">null</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">The collection you passed to retainAll() contains a null element. Cannot continue.</span><span class="string">&quot;</span>);
}
Node&lt;E&gt; pointer = <span class="literal">null</span>;
<span class="literal">boolean</span> removeSuccessful = <span class="literal">false</span>;
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; c.size() - <span class="number">1</span>; ++j) {
pointer = <span class="ST1">head</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; size(); ++i) {
<span class="literal">if</span> (!c.contains(pointer.<span class="ST1">e</span>)) {
remove(pointer.<span class="ST1">e</span>);
removeSuccessful = <span class="literal">true</span>;
}
pointer = pointer.<span class="ST1">r</span>;
}
}
<span class="literal">return</span> removeSuccessful;
}
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">clear</span>() {
<span class="comment">// head = tail = null;</span>
<span class="comment">//extra, no necessary to set the link of every node</span>
Node&lt;E&gt; p = <span class="ST1">head</span>;
<span class="literal">while</span> (p != <span class="ST1">tail</span>) {
p = p.<span class="ST1">r</span>;
p.<span class="ST1">l</span> = <span class="literal">null</span>;
}
<span class="ST1">head</span> = <span class="ST1">tail</span> = <span class="literal">null</span>;
}
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">forEach</span>(Consumer&lt;? <span class="literal">super</span> E&gt; action) {
<span class="comment">//1. use a pointer that points to the head</span>
<span class="comment">//2. while the pointer has not reached the end of the queue </span>
<span class="comment">//consume it</span>
Node&lt;E&gt; p = <span class="ST1">head</span>;
<span class="literal">while</span> (p != <span class="literal">null</span>) {
action.accept(p.<span class="ST1">e</span>);
p = p.<span class="ST1">r</span>;
}
}
@Override
<span class="literal">public</span> String <span class="ST4">toString</span>() {
String s = <span class="string">&quot;</span><span class="string">PriorityQueueASDV {</span><span class="string">&quot;</span>;
Node&lt;E&gt; p = <span class="ST1">head</span>;
<span class="literal">while</span> (p != <span class="literal">null</span>) {
s += p.<span class="ST1">e</span>.toString();
<span class="literal">if</span> (p != <span class="ST1">tail</span>) {
s += <span class="string">&quot;</span><span class="string">, </span><span class="string">&quot;</span>;
}
p = p.<span class="ST1">r</span>;
}
s += <span class="string">&quot;</span><span class="string">}</span><span class="string">&quot;</span>;
<span class="literal">return</span> s;
}
@Override
<span class="literal">protected</span> Object <span class="ST4">clone</span>()
<span class="literal">throws</span> CloneNotSupportedException {
PriorityQueueASDV&lt;E&gt; c = (PriorityQueueASDV&lt;E&gt;) <span class="literal">super</span>.clone();
<span class="literal">return</span> c;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST7">main</span>(String[] args) {
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">--&gt; PriorityQueueASDV testing add</span><span class="string">&quot;</span>);
PriorityQueueASDV&lt;String&gt; pq1 = <span class="literal">new</span> PriorityQueueASDV();
pq1.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">--&gt; Colllections PriorityQueue testing add</span><span class="string">&quot;</span>);
PriorityQueue&lt;String&gt; pq2 = <span class="literal">new</span> PriorityQueue();
pq2.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq2);
<span class="comment">//TEST IT FULLY HERE. FOR ALL METHODS AND ALL CASES.</span>
<span class="comment">//Have the Jzva PriorityQueue below</span>
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">--&gt; PriorityQueueASDV testing remove(object o)</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">remove from front Athens</span><span class="string">&quot;</span>);
pq1.remove(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq2.remove(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">remove from end Paris</span><span class="string">&quot;</span>);
pq1.remove(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq2.remove(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">remove from the middle Lafayette</span><span class="string">&quot;</span>);
pq1.remove(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq2.remove(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">add at the end Stocholm</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Stocholm</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Stocholm</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">remove from the middle London</span><span class="string">&quot;</span>);
pq1.remove(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq2.remove(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">remove from the front Berlin</span><span class="string">&quot;</span>);
pq1.remove(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
pq2.remove(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">remove from the front/end Stocholm</span><span class="string">&quot;</span>);
pq1.remove(<span class="string">&quot;</span><span class="string">Stocholm</span><span class="string">&quot;</span>);
pq2.remove(<span class="string">&quot;</span><span class="string">Stocholm</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string">remove from empty queue</span><span class="string">&quot;</span>);
pq1.remove(<span class="string">&quot;</span><span class="string">Stocholm</span><span class="string">&quot;</span>);
pq2.remove(<span class="string">&quot;</span><span class="string">Stocholm</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">--&gt; PriorityQueueASDV recreate priority queues from empty</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Zurich</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Zurich</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">+++HERE YOU TEST ALL YOUR METHODS FULLY, and the methods of Colleciion PriorityQueue</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> offer New York</span><span class="string">&quot;</span>);
pq1.offer(<span class="string">&quot;</span><span class="string">New York</span><span class="string">&quot;</span>);
pq2.offer(<span class="string">&quot;</span><span class="string">New York</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> offer Miami</span><span class="string">&quot;</span>);
pq1.offer(<span class="string">&quot;</span><span class="string">Miami</span><span class="string">&quot;</span>);
pq2.offer(<span class="string">&quot;</span><span class="string">Miami</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> offer null</span><span class="string">&quot;</span>);
<span class="literal">try</span> {
pq1.offer(<span class="literal">n</span><span class="literal">ull</span>);
} <span class="literal">catch</span> (Exception e) {
System.<span class="ST6">err</span>.println(e);
}
<span class="literal">try</span> {
pq2.offer(<span class="literal">n</span><span class="literal">ull</span>);
} <span class="literal">catch</span> (Exception e) {
System.<span class="ST6">err</span>.println(e);
}
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> offer ClassCastException with Object</span><span class="string">&quot;</span>);
<span class="literal">try</span> {
pq1.offer((String) <span class="literal">new</span> Object());
} <span class="literal">catch</span> (Exception e) {
System.<span class="ST6">err</span>.println(e);
}
<span class="literal">try</span> {
pq2.offer((String) <span class="literal">new</span> Object());
} <span class="literal">catch</span> (Exception e) {
System.<span class="ST6">err</span>.println(e);
}
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> poll suposed to be Athens</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1.poll());
System.<span class="ST6">out</span>.println(pq2.poll());
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> Iterator</span><span class="string">&quot;</span>);
Iterator&lt;String&gt; it1 = pq1.iterator();
Iterator&lt;String&gt; it2 = pq2.iterator();
<span class="literal">while</span> (it1.hasNext()) {
System.<span class="ST6">out</span>.print(it1.next() + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
<span class="literal">while</span> (it2.hasNext()) {
System.<span class="ST6">out</span>.print(it2.next() + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> Iterator NoSuchElementException </span><span class="string">&quot;</span>);
<span class="literal">try</span> {
System.<span class="ST6">out</span>.println(it1.next());
} <span class="literal">catch</span> (NoSuchElementException e) {
System.<span class="ST6">err</span>.println(e);
}
<span class="literal">try</span> {
System.<span class="ST6">out</span>.println(it2.next());
} <span class="literal">catch</span> (NoSuchElementException e) {
System.<span class="ST6">err</span>.println(e);
}
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> Iterator foreach </span><span class="string">&quot;</span>);
it1 = pq1.iterator();
it2 = pq2.iterator();
it1.forEachRemaining(<span class="literal">new</span> Consumer() {
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">accept</span>(Object t) {
System.<span class="ST6">out</span>.print(t + <span class="string">&quot;</span><span class="string">*** </span><span class="string">&quot;</span>);
}
});
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
it2.forEachRemaining(<span class="literal">new</span> Consumer() {
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">accept</span>(Object t) {
System.<span class="ST6">out</span>.print(t + <span class="string">&quot;</span><span class="string">+++ </span><span class="string">&quot;</span>);
}
});
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> addAll Houston Chicago</span><span class="string">&quot;</span>);
List&lt;String&gt; ar1 = Arrays.<span class="ST5">asList</span>(<span class="string">&quot;</span><span class="string">Houston</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Chicago</span><span class="string">&quot;</span>);
pq1.addAll(ar1);
pq2.addAll(ar1);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> clear</span><span class="string">&quot;</span>);
pq1.clear();
pq2.clear();
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">--&gt; PriorityQueueASDV recreate priority queues from empty</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Zurich</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Zurich</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> forEach</span><span class="string">&quot;</span>);
pq1.forEach(<span class="literal">new</span> Consumer() {
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">accept</span>(Object t) {
System.<span class="ST6">out</span>.print(t + <span class="string">&quot;</span><span class="string">*** </span><span class="string">&quot;</span>);
}
});
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
pq2.forEach(<span class="literal">new</span> Consumer() {
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">accept</span>(Object t) {
System.<span class="ST6">out</span>.print(t + <span class="string">&quot;</span><span class="string">+++ </span><span class="string">&quot;</span>);
}
});
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\t</span><span class="string"> clone</span><span class="string">&quot;</span>);
<span class="literal">try</span> {
PriorityQueueASDV&lt;String&gt; pq1Cloned
= (PriorityQueueASDV&lt;String&gt;) pq1.clone();
System.<span class="ST6">out</span>.println(pq1Cloned);
pq1Cloned.add(<span class="string">&quot;</span><span class="string">Las Vegas</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1Cloned);
System.<span class="ST6">out</span>.println(pq1);
} <span class="literal">catch</span> (CloneNotSupportedException e) {
System.<span class="ST6">err</span>.println(e);
}
pq1.clear();
pq2.clear();
pq1.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
pq1.add(<span class="string">&quot;</span><span class="string">Zurich</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Athens</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
pq2.add(<span class="string">&quot;</span><span class="string">Zurich</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">----------------</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Attempt to remove an element.</span><span class="string">&quot;</span>);
pq1.remove();
pq2.remove();
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Get array of the priority queues.</span><span class="string">&quot;</span>);
Object pqArray1[] = pq1.toArray();
Object pqArray2[] = pq2.toArray();
<span class="ST5">printArrays</span>(pqArray1);
<span class="ST5">printArrays</span>(pqArray2);
System.<span class="ST6">out</span>.println();
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">----------------</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Test .toArray(T[])</span><span class="string">&quot;</span>);
String[] pqArray3 = pq1.toArray(<span class="literal">new</span> String[<span class="number">0</span>]);
<span class="ST5">printArrays</span>(pqArray3);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">----------------</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Test containsAll()</span><span class="string">&quot;</span>);
ArrayList&lt;String&gt; testArray = <span class="literal">new</span> ArrayList&lt;&gt;();
testArray.add(<span class="string">&quot;</span><span class="string">Lafayette</span><span class="string">&quot;</span>);
testArray.add(<span class="string">&quot;</span><span class="string">Berlin</span><span class="string">&quot;</span>);
testArray.add(<span class="string">&quot;</span><span class="string">Zurich</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Does pq1 contain Lafayette, Berlin, and Zurich? </span><span class="string">&quot;</span> + (pq1.containsAll(testArray) ? <span class="string">&quot;</span><span class="string">yes</span><span class="string">&quot;</span> : <span class="string">&quot;</span><span class="string">no</span><span class="string">&quot;</span>));
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Does pq1 contain the contents of pq2? </span><span class="string">&quot;</span> + (pq1.containsAll(pq2) ? <span class="string">&quot;</span><span class="string">yes</span><span class="string">&quot;</span> : <span class="string">&quot;</span><span class="string">no</span><span class="string">&quot;</span>));
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Does pq2 contain the contents of pq1? </span><span class="string">&quot;</span> + (pq2.containsAll(pq1) ? <span class="string">&quot;</span><span class="string">yes</span><span class="string">&quot;</span> : <span class="string">&quot;</span><span class="string">no</span><span class="string">&quot;</span>));
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Adding funkytown to testArray...</span><span class="string">&quot;</span>);
testArray.add(<span class="string">&quot;</span><span class="string">Funkytown</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Does pq1 contain Lafayette, Berlin, Zurich, and Funkytown? </span><span class="string">&quot;</span> + (pq1.containsAll(testArray) ? <span class="string">&quot;</span><span class="string">yes</span><span class="string">&quot;</span> : <span class="string">&quot;</span><span class="string">no</span><span class="string">&quot;</span>));
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Test if containsAll() correctly throws a NullPointerException...</span><span class="string">&quot;</span>);
<span class="literal">try</span> {
testArray.add(<span class="literal">n</span><span class="literal">ull</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Does pq1 contain Lafayette, Berlin, Zurich, Funkytown, and null? </span><span class="string">&quot;</span> + (pq1.containsAll(testArray) ? <span class="string">&quot;</span><span class="string">yes</span><span class="string">&quot;</span> : <span class="string">&quot;</span><span class="string">no</span><span class="string">&quot;</span>));
} <span class="literal">catch</span> (NullPointerException ex) {
System.<span class="ST6">out</span>.println(ex);
}
testArray.remove(<span class="literal">n</span><span class="literal">ull</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">That worked! Continuing with tests...</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">----------------</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Testing removeAll(Collection&lt;?&gt;)...</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Removing the elements in the test array...</span><span class="string">&quot;</span>);
pq1.removeAll(testArray);
pq2.removeAll(testArray);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">----------------</span><span class="string">&quot;</span>);
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">Testing retainAll()...</span><span class="string">&quot;</span>);
ArrayList&lt;String&gt; testArray2 = <span class="literal">new</span> ArrayList&lt;&gt;();
testArray2.add(<span class="string">&quot;</span><span class="string">London</span><span class="string">&quot;</span>);
testArray2.add(<span class="string">&quot;</span><span class="string">Paris</span><span class="string">&quot;</span>);
pq1.retainAll(testArray2);
pq2.retainAll(testArray2);
System.<span class="ST6">out</span>.println(pq1);
System.<span class="ST6">out</span>.println(pq2);
}
<span class="literal">static</span> <span class="literal">void</span> <span class="ST7">printArrays</span>(Object[] arr) {
<span class="literal">for</span> (Object element : arr) {
System.<span class="ST6">out</span>.print(element + <span class="string">&quot;</span><span class="string">, </span><span class="string">&quot;</span>);
}
}
}
</pre></body>
</html>

View File

@@ -8,6 +8,7 @@ package edu.slcc.asdv.caleb.mp6_calebfontenot;
*
* @author caleb
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
@@ -201,10 +202,9 @@ public class PriorityQueueASDV<E extends Comparable<E>>
return e;
}
}
/**
* Retrieves, but does not remove, the head of this queue. This method
* differs from {@link #peek peek} only in that it throws an exception
* if this queue is empty.
* Retrieves, but does not remove, the head of this queue. This method differs from {@link #peek peek} only in that it throws an exception if this queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
@@ -217,9 +217,9 @@ public class PriorityQueueASDV<E extends Comparable<E>>
throw new NoSuchElementException("Element does not exist.");
}
}
/**
* Retrieves, but does not remove, the head of this queue,
* or returns {@code null} if this queue is empty.
* Retrieves, but does not remove, the head of this queue, or returns {@code null} if this queue is empty.
*
* @return the head of this queue, or {@code null} if this queue is empty
*/
@@ -233,21 +233,14 @@ public class PriorityQueueASDV<E extends Comparable<E>>
return head == null && tail == null ? true : false;
}
/**
* Returns {@code true} if this collection contains the specified element.
* More formally, returns {@code true} if and only if this collection
* contains at least one element {@code e} such that
* {@code Objects.equals(o, e)}.
* Returns {@code true} if this collection contains the specified element. More formally, returns {@code true} if and only if this collection contains at least one element {@code e} such that {@code Objects.equals(o, e)}.
*
* @param o element whose presence in this collection is to be tested
* @return {@code true} if this collection contains the specified
* element
* @throws ClassCastException if the type of the specified element
* is incompatible with this collection
* ({@linkplain Collection##optional-restrictions optional})
* @throws NullPointerException if the specified element is null and this
* collection does not permit null elements
* ({@linkplain Collection##optional-restrictions optional})
* @return {@code true} if this collection contains the specified element
* @throws ClassCastException if the type of the specified element is incompatible with this collection ({@linkplain Collection##optional-restrictions optional})
* @throws NullPointerException if the specified element is null and this collection does not permit null elements ({@linkplain Collection##optional-restrictions optional})
*/
@Override
public boolean contains(Object o) {
@@ -256,7 +249,7 @@ public class PriorityQueueASDV<E extends Comparable<E>>
if (pointer.equals(o)) {
return true;
} else {
pointer =pointer.r;
pointer = pointer.r;
}
} while (pointer != null);
return false;
@@ -303,68 +296,56 @@ public class PriorityQueueASDV<E extends Comparable<E>>
while (pointer.r != null) {
returnArray[i++] = pointer.e;
pointer = pointer.r;
}
}
returnArray[i++] = pointer.e;
return returnArray;
}
/**
* Returns an array containing all of the elements in this collection;
* the runtime type of the returned array is that of the specified array.
* If the collection fits in the specified array, it is returned therein.
* Otherwise, a new array is allocated with the runtime type of the
* specified array and the size of this collection.
* Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.
*
* <p>If this collection fits in the specified array with room to spare
* (i.e., the array has more elements than this collection), the element
* in the array immediately following the end of the collection is set to
* {@code null}. (This is useful in determining the length of this
* collection <i>only</i> if the caller knows that this collection does
* not contain any {@code null} elements.)
* <p>
* If this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to {@code null}. (This is useful in determining the length of this collection <i>only</i> if the caller knows that this collection does not contain any {@code null} elements.)
*
* <p>If this collection makes any guarantees as to what order its elements
* are returned by its iterator, this method must return the elements in
* the same order.
* <p>
* If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
*
* @apiNote
* This method acts as a bridge between array-based and collection-based APIs.
* It allows an existing array to be reused under certain circumstances.
* Use {@link #toArray()} to create an array whose runtime type is {@code Object[]},
* or use {@link #toArray(IntFunction)} to control the runtime type of
* the array.
* @apiNote This method acts as a bridge between array-based and collection-based APIs. It allows an existing array to be reused under certain circumstances. Use {@link #toArray()} to create an array whose runtime type is {@code Object[]}, or use {@link #toArray(IntFunction)} to control the runtime type of the array.
*
* <p>Suppose {@code x} is a collection known to contain only strings.
* The following code can be used to dump the collection into a previously
* allocated {@code String} array:
* <p>
* Suppose {@code x} is a collection known to contain only strings. The following code can be used to dump the collection into a previously allocated {@code String} array:
*
* <pre>
* String[] y = new String[SIZE];
* ...
* y = x.toArray(y);</pre>
*
* <p>The return value is reassigned to the variable {@code y}, because a
* new array will be allocated and returned if the collection {@code x} has
* too many elements to fit into the existing array {@code y}.
* <p>
* The return value is reassigned to the variable {@code y}, because a new array will be allocated and returned if the collection {@code x} has too many elements to fit into the existing array {@code y}.
*
* <p>Note that {@code toArray(new Object[0])} is identical in function to
* {@code toArray()}.
* <p>
* Note that {@code toArray(new Object[0])} is identical in function to {@code toArray()}.
*
* @param <T> the component type of the array to contain the collection
* @param a the array into which the elements of this collection are to be
* stored, if it is big enough; otherwise, a new array of the same
* runtime type is allocated for this purpose.
* @param a the array into which the elements of this collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
* @return an array containing all of the elements in this collection
* @throws ArrayStoreException if the runtime type of any element in this
* collection is not assignable to the {@linkplain Class#getComponentType
* @throws ArrayStoreException if the runtime type of any element in this collection is not assignable to the {@linkplain Class#getComponentType
* runtime component type} of the specified array
* @throws NullPointerException if the specified array is null
*/
@Override
public <T> T[] toArray(T[] a) {
T[] genericArray = new T[this.size()];
a = Arrays.copyOf(a, this.size());
Node<E> pointer = head;
System.out.println(a.getClass());
System.out.println(pointer.getClass());
System.out.println(pointer.e.getClass());
for (int i = 0; i < this.size(); ++i) {
genericArray[i] = pointer;
a[i] = (T) pointer.e;
pointer = pointer.r;
}
return a;
}
/**
@@ -440,27 +421,39 @@ public class PriorityQueueASDV<E extends Comparable<E>>
}
return true;
}
/**
* Returns {@code true} if this collection contains all of the elements
* in the specified collection.
* Returns {@code true} if this collection contains all of the elements in the specified collection.
*
* @param c collection to be checked for containment in this collection
* @return {@code true} if this collection contains all of the elements
* in the specified collection
* @throws ClassCastException if the types of one or more elements
* in the specified collection are incompatible with this
* collection
* ({@linkplain Collection##optional-restrictions optional})
* @throws NullPointerException if the specified collection contains one
* or more null elements and this collection does not permit null
* elements
* ({@linkplain Collection##optional-restrictions optional})
* or if the specified collection is null.
* @see #contains(Object)
* @param c collection to be checked for containment in this collection
* @return {@code true} if this collection contains all of the elements in the specified collection
* @throws ClassCastException if the types of one or more elements in the specified collection are incompatible with this collection ({@linkplain Collection##optional-restrictions optional})
* @throws NullPointerException if the specified collection contains one or more null elements and this collection does not permit null elements ({@linkplain Collection##optional-restrictions optional}) or if the specified collection is null.
* @see #contains(Object)
*/
@Override
public boolean containsAll(Collection<?> c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// Java already throws a CastCastException if you give it the wrong type, so we don't have to throw that ourselves
if (c.contains(null) || c == null) {
throw new NullPointerException("The collection you passed to containsAll() contains a null element. Cannot continue.");
}
// Unpack the collection so we can compare them
Object[] compareArray = c.toArray();
Node<E> pointer = null;
int matchCount = 0;
for (Object compare : compareArray) {
pointer = head;
for (int i = 0; i < size() - 1; ++i) {
if (pointer.e.equals(compare)) {
matchCount++;
}
pointer = pointer.r;
}
}
if (matchCount == compareArray.length - 1) {
return true;
}
return false;
}
/**
@@ -483,14 +476,61 @@ public class PriorityQueueASDV<E extends Comparable<E>>
return sizeAfter > sizeBefore;
}
/**
* Removes all of this collection's elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.
*
* @implSpec This implementation iterates over this collection, checking each element returned by the iterator in turn to see if it's contained in the specified collection. If it's so contained, it's removed from this collection with the iterator's {@code remove} method.
*
* <p>
* Note that this implementation will throw an {@code UnsupportedOperationException} if the iterator returned by the {@code iterator} method does not implement the {@code remove} method and this collection contains one or more elements in common with the specified collection.
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
*
* @see #remove(Object)
* @see #contains(Object)
*/
@Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
if (c.contains(null) || c == null) {
throw new NullPointerException("The collection you passed to removeAll() contains a null element. Cannot continue.");
}
// Unpack the collection so we can remove them
Object[] compareArray = c.toArray();
Node<E> pointer = null;
boolean removeSuccessful = false;
for (Object compare : compareArray) {
pointer = head;
for (int i = 0; i < size() - 1; ++i) {
if (pointer.e.equals(compare)) {
remove(pointer.e);
removeSuccessful = true;
}
pointer = pointer.r;
}
}
return removeSuccessful;
}
@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
if (c.contains(null) || c == null) {
throw new NullPointerException("The collection you passed to retainAll() contains a null element. Cannot continue.");
}
Node<E> pointer = null;
boolean removeSuccessful = false;
for (int j = 0; j < c.size() - 1; ++j) {
pointer = head;
for (int i = 0; i < size(); ++i) {
if (!c.contains(pointer.e)) {
remove(pointer.e);
removeSuccessful = true;
}
pointer = pointer.r;
}
}
return removeSuccessful;
}
@Override
@@ -817,12 +857,56 @@ public class PriorityQueueASDV<E extends Comparable<E>>
printArrays(pqArray1);
printArrays(pqArray2);
System.out.println();
System.out.println("----------------");
System.out.println("Test .toArray(T[])");
String[] pqArray3 = pq1.toArray(new String[0]);
printArrays(pqArray3);
System.out.println("----------------");
System.out.println("Test containsAll()");
ArrayList<String> testArray = new ArrayList<>();
testArray.add("Lafayette");
testArray.add("Berlin");
testArray.add("Zurich");
System.out.println("Does pq1 contain Lafayette, Berlin, and Zurich? " + (pq1.containsAll(testArray) ? "yes" : "no"));
System.out.println("Does pq1 contain the contents of pq2? " + (pq1.containsAll(pq2) ? "yes" : "no"));
System.out.println("Does pq2 contain the contents of pq1? " + (pq2.containsAll(pq1) ? "yes" : "no"));
System.out.println("Adding funkytown to testArray...");
testArray.add("Funkytown");
System.out.println("Does pq1 contain Lafayette, Berlin, Zurich, and Funkytown? " + (pq1.containsAll(testArray) ? "yes" : "no"));
System.out.println("Test if containsAll() correctly throws a NullPointerException...");
try {
testArray.add(null);
System.out.println("Does pq1 contain Lafayette, Berlin, Zurich, Funkytown, and null? " + (pq1.containsAll(testArray) ? "yes" : "no"));
} catch (NullPointerException ex) {
System.out.println(ex);
}
testArray.remove(null);
System.out.println("That worked! Continuing with tests...");
System.out.println("----------------");
System.out.println(pq1);
System.out.println(pq2);
System.out.println("Testing removeAll(Collection<?>)...");
System.out.println("Removing the elements in the test array...");
pq1.removeAll(testArray);
pq2.removeAll(testArray);
System.out.println(pq1);
System.out.println(pq2);
System.out.println("----------------");
System.out.println("Testing retainAll()...");
ArrayList<String> testArray2 = new ArrayList<>();
testArray2.add("London");
testArray2.add("Paris");
pq1.retainAll(testArray2);
pq2.retainAll(testArray2);
System.out.println(pq1);
System.out.println(pq2);
}
static void printArrays(Object[] arr) {
for (Object element : arr) {
System.out.print(element + ", ");
}
System.out.println();
}
}

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.calebfontenot</groupId>
<artifactId>ProgrammingExam2_CalebFontenot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>com.calebfontenot.programmingexam2_calebfontenot.ProgrammingExam2_CalebFontenot</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,144 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.calebfontenot.programmingexam2_calebfontenot;
/**
*
* @author caleb
*/
/**
*
* @author ar114
* @param <T>
*/
public class ProgrammingExam2_CalebFontenot<T> {
Node<T> head;
Node<T> tail;
class Node<T> {
T t;
Node<T> l;
Node<T> r;
}
public T removeAt(int pos) {
if (pos < 0 || pos > size() - 1) {
throw new IndexOutOfBoundsException();
}
//list is empty
if (size() == 0) {
throw new RuntimeException("The list empty.");
}
T returnT = null;
if (pos == 0)//remove at 0
{
Node<T> pointer = head.r;
returnT = (T) head.t;
pointer.l = null;
head = pointer;
} else if (pos == (size() - 1))//remove at end
{
Node<T> pointer = tail.l.l;
returnT = (T) tail.t;
pointer.r = null;
tail = pointer;
} else//remove in the middle
{
// Iterate to the element position
Node<T> pointer = head;
for (int i = 0; i < pos - 1; ++i) {
pointer = pointer.r;
}
returnT = (T) pointer.r.t;
pointer.r = pointer.r.r;
pointer.l = pointer.r;
}
return returnT;
}
public void clear() {
head = tail = null;
}
public void addAt(T t, int pos) {
if (pos < 0 || pos > size()) {
throw new IndexOutOfBoundsException();
}
Node<T> newNode = new Node<T>();
newNode.t = t;
if (head == null)//list is empty
{
head = tail = newNode;
} else if (pos == 0)//add at the front
{
newNode.r = head;
head.l = newNode;
head = newNode;
} else if (pos == size())//add at the end
{
newNode.l = tail;
tail.r = newNode;
tail = newNode;
} else//middle
{
Node<T> p = head;
for (int i = 0; i < pos - 1; ++i) {
p = p.r;
}
newNode.l = p;
newNode.r = p.r;
p.r.l = newNode;
p.r = newNode;
}
}
public int size() {
Node<T> p = head;
int count = 0;
while (p != null) {
count++;
p = p.r;
}
return count;
}
@Override
public String toString() {
String s = "";
Node<T> p = head;
while (p != null) {
s += p.t.toString() + " ";
p = p.r;
}
return s;
}
public static void main(String[] args) {
ProgrammingExam2_CalebFontenot<Integer> list = new ProgrammingExam2_CalebFontenot<Integer>();
list.addAt(20, 0);
list.addAt(10, 0);
list.addAt(40, 2);
list.addAt(30, 2);
list.addAt(50, 4);
System.out.println(list);
System.out.println(list.removeAt(0));
System.out.println(list);
System.out.println(list.removeAt(2));
System.out.println(list);
System.out.println(list.removeAt(2));
System.out.println(list.toString());
}
}

View File

@@ -0,0 +1,155 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>GenericMergeSortExam.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #000000; background-color: #ffffff; font-family: monospace}
pre {color: #000000; background-color: #ffffff; font-family: monospace}
table {color: #404040; background-color: #e9e8e2; font-family: monospace}
.ST1 {color: #969696; font-family: monospace; font-weight: bold}
.ST2 {font-family: monospace; font-weight: bold}
.comment {color: #969696}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
.ST4 {font-family: monospace; font-style: italic}
.ST6 {color: #ce54b8; font-family: monospace; font-style: italic}
.ST0 {color: #287bde}
.ST5 {color: #ce54b8}
.string {color: #1e9347}
.literal {color: #336bdd}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">C:\Users\ar114\Documents\NetBeansProjects\ProgramingExam2_CalebFontenot\src\main\java\com\mycompany\programingexam2_calebfontenot\GenericMergeSortExam.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.mycompany.programingexam2_calebfontenot;
<span class="literal">import</span> java.lang.reflect.Array;
<span class="literal">import</span> java.util.Comparator;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">ar114</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> <span class="ST2">GenericMergeSortExam</span> {
<span class="literal">public</span> <span class="literal">static</span> &lt;E <span class="literal">extends</span> Comparable&lt;E&gt;&gt; <span class="literal">void</span> <span class="ST3">mergeSort</span>(E[] list) {
<span class="ST4">mergeSort</span>(list,
<span class="literal">new</span> Comparator&lt;E&gt;() {
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">compare</span>(E e1, E e2) {
<span class="literal">int</span> compareResult = ((Comparable&lt;E&gt;) e1).compareTo(e2);
<span class="literal">return</span> compareResult;
}
});
}
<span class="literal">public</span> <span class="literal">static</span> &lt;E&gt; <span class="literal">void</span> <span class="ST3">mergeSort</span>(E[] list,
Comparator&lt;? <span class="literal">super</span> E&gt; comparator) {
<span class="literal">if</span> (list.<span class="ST5">length</span> &gt; 1) {
<span class="comment">// Merge sort the first half</span>
E[] firstHalf = (E[]) <span class="literal">new</span> Object[list.<span class="ST5">length</span> / 2];
<span class="comment">//copies 1st half of list into array firstHalf</span>
System.<span class="ST4">arraycopy</span>(list, 0, firstHalf, 0, list.<span class="ST5">length</span> / 2);
<span class="ST4">mergeSort</span>(firstHalf, comparator);
<span class="comment">// Merge sort the second half</span>
<span class="literal">int</span> secondHalfLength = list.<span class="ST5">length</span> - list.<span class="ST5">length</span> / 2;
E[] secondHalf = (E[]) <span class="literal">new</span> Object[secondHalfLength];
System.<span class="ST4">arraycopy</span>(list, list.<span class="ST5">length</span> / 2,
secondHalf, 0, secondHalfLength);
<span class="ST4">mergeSort</span>(secondHalf, comparator);
<span class="comment">// Merge firstHalf with secondHalf</span>
E[] temp = <span class="ST4">merge1</span>(firstHalf, secondHalf, comparator);
System.<span class="ST4">arraycopy</span>(temp, 0, list, 0, temp.<span class="ST5">length</span>);
}
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="ST1">Merges</span> <span class="ST1">the</span> <span class="ST1">two</span> <span class="ST1">lists</span> <span class="ST1">using</span> <span class="ST1">a</span> <span class="ST1">Comparator</span><span class="ST1">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST1">@param</span> &lt;E&gt; <span class="comment">The</span> <span class="comment">generic</span> <span class="comment">type</span> <span class="comment">the</span> <span class="comment">methods</span> <span class="comment">accepts</span><span class="comment">.</span>
<span class="comment"> * </span><span class="ST1">@param</span> list1 <span class="comment">a</span> <span class="comment">list</span> <span class="comment">of</span> <span class="comment">generic</span> <span class="comment">type</span> <span class="comment">E</span>
<span class="comment"> * </span><span class="ST1">@param</span> list2 <span class="comment">a</span> <span class="comment">list</span> <span class="comment">of</span> <span class="comment">generic</span> <span class="comment">type</span> <span class="comment">E</span>
<span class="comment"> * </span><span class="ST1">@param</span> comparator <span class="comment">Comparator</span>
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">a</span> <span class="comment">sorted</span> <span class="comment">new</span> <span class="comment">list</span> <span class="comment">made</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">merged</span> <span class="comment">list1</span> <span class="comment">and</span> <span class="comment">list2</span><span class="comment">.</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">static</span> &lt;E&gt; E[]
<span class="ST3">merge1</span>(E[] list1, E[] list2, Comparator&lt;? <span class="literal">super</span> E&gt; comparator) {
<span class="literal">int</span> returnArraySize = list1.<span class="ST5">length</span> + list2.<span class="ST5">length</span>;
E[] returnArray = (E[]) <span class="literal">new</span> Object[returnArraySize];
<span class="literal">int</span> list1Iterator = 0, list2Iterator = 0, returnArrayIterator = 0, iterationsRemaining = 0;
<span class="literal">for</span> (; returnArrayIterator &lt; returnArraySize; ++returnArrayIterator) {
iterationsRemaining = (returnArraySize - returnArrayIterator);
<span class="literal">if</span> (comparator.compare(list1[list1Iterator], list2[list2Iterator]) &lt;= 0) {
returnArray[returnArrayIterator] = list1[list1Iterator];
<span class="literal">if</span> (iterationsRemaining &gt; 1)
returnArray[returnArrayIterator + 1] = list2[list2Iterator];
<span class="literal">if</span> ((list1.<span class="ST5">length</span> - 1) &lt; (list1Iterator)) {
++list1Iterator;
} <span class="literal">else</span> {
++list2Iterator;
}
++returnArrayIterator;
} <span class="literal">else</span> {
returnArray[returnArrayIterator] = list2[list2Iterator];
<span class="literal">if</span> (iterationsRemaining &gt; 1)
returnArray[returnArrayIterator + 1] = list1[list1Iterator];
<span class="literal">if</span> ((list2.<span class="ST5">length</span> - 1) &lt; (list2Iterator)) {
++list2Iterator;
} <span class="literal">else</span> {
++list1Iterator;
}
++returnArrayIterator;
}
<span class="comment">/*</span>
<span class="comment"> if (comparator.compare(list1[list1Iterator], list2[list2Iterator]) == 1) {</span>
<span class="comment"> returnArray[returnArrayIterator] = list2[list2Iterator];</span>
<span class="comment"> ++list2Iterator;</span>
<span class="comment"> ++returnArrayIterator;</span>
<span class="comment"> } else {</span>
<span class="comment"> returnArray[returnArrayIterator] = list1[list1Iterator];</span>
<span class="comment"> ++list1Iterator;</span>
<span class="comment"> ++returnArrayIterator;</span>
<span class="comment"> }</span>
<span class="comment"> */</span>
}
<span class="literal">return</span> returnArray;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">main</span>(String[] args) {
Integer[] list
= {
2, 3, 2, 5, 6, 1, -2, 3, 14, 12
};
<span class="ST4">mergeSort</span>(list);
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i &lt; list.<span class="ST5">length</span>; i++) {
System.<span class="ST6">out</span>.print(list[i] + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST6">out</span>.println();
String[] list1
= {
<span class="string">&quot;</span><span class="string">ABC</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">abc</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">abm</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Anf</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Good</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Bad</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">nice</span><span class="string">&quot;</span>
};
<span class="ST4">mergeSort</span>(list1, <span class="literal">new</span> Comparator&lt;String&gt;() {
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">compare</span>(String s1, String s2) {
<span class="literal">return</span> s1.compareToIgnoreCase(s2);
}
});
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i &lt; list1.<span class="ST5">length</span>; i++) {
System.<span class="ST6">out</span>.print(list1[i] + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST6">out</span>.println(<span class="string">&quot;</span><span class="string">&quot;</span>);
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,165 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>ProgramingExam2_CalebFontenot.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #000000; background-color: #ffffff; font-family: monospace}
pre {color: #000000; background-color: #ffffff; font-family: monospace}
table {color: #404040; background-color: #e9e8e2; font-family: monospace}
.ST1 {color: #969696; font-family: monospace; font-weight: bold}
.ST2 {font-family: monospace; font-weight: bold}
.comment {color: #969696}
.ST4 {font-family: monospace; font-weight: bold; font-style: italic}
.ST5 {color: #ce54b8; font-family: monospace; font-style: italic}
.ST0 {color: #287bde}
.ST3 {color: #ce54b8}
.string {color: #1e9347}
.literal {color: #336bdd}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">C:\Users\ar114\Documents\NetBeansProjects\ProgramingExam2_CalebFontenot\src\main\java\com\mycompany\programingexam2_calebfontenot\ProgramingExam2_CalebFontenot.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/$</span><span class="comment">{packagePath}/${mainClassName}.java to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.mycompany.programingexam2_calebfontenot;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">ar114</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> <span class="ST2">ProgramingExam2_CalebFontenot</span>&lt;T&gt; {
Node&lt;T&gt; <span class="ST3">head</span>;
Node&lt;T&gt; <span class="ST3">tail</span>;
<span class="literal">class</span> <span class="ST2">Node</span>&lt;T&gt; {
T <span class="ST3">t</span>;
Node&lt;T&gt; <span class="ST3">l</span>;
Node&lt;T&gt; <span class="ST3">r</span>;
}
<span class="literal">public</span> T <span class="ST2">removeAt</span>(<span class="literal">int</span> pos) {
<span class="literal">if</span> (pos &lt; 0 || pos &gt; size() - 1) {
<span class="literal">throw</span> <span class="literal">new</span> IndexOutOfBoundsException();
}
<span class="comment">//list is empty</span>
<span class="literal">if</span> (size() == 0) {
<span class="literal">throw</span> <span class="literal">new</span> RuntimeException(<span class="string">&quot;</span><span class="string">The list empty.</span><span class="string">&quot;</span>);
}
T returnT = <span class="literal">null</span>;
<span class="literal">if</span> (pos == 0)<span class="comment">//remove at 0</span>
{
Node&lt;T&gt; pointer = <span class="ST3">head</span>.<span class="ST3">r</span>;
returnT = (T) <span class="ST3">head</span>.<span class="ST3">t</span>;
pointer.<span class="ST3">l</span> = <span class="literal">null</span>;
<span class="ST3">head</span> = pointer;
} <span class="literal">else</span> <span class="literal">if</span> (pos == (size() - 1))<span class="comment">//remove at end</span>
{
Node&lt;T&gt; pointer = <span class="ST3">tail</span>.<span class="ST3">l</span>.<span class="ST3">l</span>;
returnT = (T) <span class="ST3">tail</span>.<span class="ST3">t</span>;
pointer.<span class="ST3">r</span> = <span class="literal">null</span>;
<span class="ST3">tail</span> = pointer;
} <span class="literal">else</span><span class="comment">//remove in the middle</span>
{
<span class="comment">// Iterate to the element position</span>
Node&lt;T&gt; pointer = <span class="ST3">head</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i &lt; pos - 1; ++i) {
pointer = pointer.<span class="ST3">r</span>;
}
pointer.<span class="ST3">r</span> = pointer.<span class="ST3">r</span>.<span class="ST3">r</span>;
pointer.<span class="ST3">l</span> = pointer.<span class="ST3">r</span>;
returnT = (T) pointer.<span class="ST3">t</span>;
}
<span class="literal">return</span> returnT;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">clear</span>() {
<span class="ST3">head</span> = <span class="ST3">tail</span> = <span class="literal">null</span>;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">addAt</span>(T t, <span class="literal">int</span> pos) {
<span class="literal">if</span> (pos &lt; 0 || pos &gt; size()) {
<span class="literal">throw</span> <span class="literal">new</span> IndexOutOfBoundsException();
}
Node&lt;T&gt; newNode = <span class="literal">new</span> Node&lt;T&gt;();
newNode.<span class="ST3">t</span> = t;
<span class="literal">if</span> (<span class="ST3">head</span> == <span class="literal">null</span>)<span class="comment">//list is empty</span>
{
<span class="ST3">head</span> = <span class="ST3">tail</span> = newNode;
} <span class="literal">else</span> <span class="literal">if</span> (pos == 0)<span class="comment">//add at the front</span>
{
newNode.<span class="ST3">r</span> = <span class="ST3">head</span>;
<span class="ST3">head</span>.<span class="ST3">l</span> = newNode;
<span class="ST3">head</span> = newNode;
} <span class="literal">else</span> <span class="literal">if</span> (pos == size())<span class="comment">//add at the end</span>
{
newNode.<span class="ST3">l</span> = <span class="ST3">tail</span>;
<span class="ST3">tail</span>.<span class="ST3">r</span> = newNode;
<span class="ST3">tail</span> = newNode;
} <span class="literal">else</span><span class="comment">//middle</span>
{
Node&lt;T&gt; p = <span class="ST3">head</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i &lt; pos - 1; ++i) {
p = p.<span class="ST3">r</span>;
}
newNode.<span class="ST3">l</span> = p;
newNode.<span class="ST3">r</span> = p.<span class="ST3">r</span>;
p.<span class="ST3">r</span>.<span class="ST3">l</span> = newNode;
p.<span class="ST3">r</span> = newNode;
}
}
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">size</span>() {
Node&lt;T&gt; p = <span class="ST3">head</span>;
<span class="literal">int</span> count = 0;
<span class="literal">while</span> (p != <span class="literal">null</span>) {
count++;
p = p.<span class="ST3">r</span>;
}
<span class="literal">return</span> count;
}
@Override
<span class="literal">public</span> String <span class="ST2">toString</span>() {
String s = <span class="string">&quot;&quot;</span>;
Node&lt;T&gt; p = <span class="ST3">head</span>;
<span class="literal">while</span> (p != <span class="literal">null</span>) {
s += p.<span class="ST3">t</span>.toString() + <span class="string">&quot;</span> <span class="string">&quot;</span>;
p = p.<span class="ST3">r</span>;
}
<span class="literal">return</span> s;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">main</span>(String[] args) {
ProgramingExam2_CalebFontenot&lt;Integer&gt; list = <span class="literal">new</span> ProgramingExam2_CalebFontenot&lt;Integer&gt;();
list.addAt(20, 0);
list.addAt(10, 0);
list.addAt(40, 2);
list.addAt(30, 2);
list.addAt(50, 4);
System.<span class="ST5">out</span>.println(list);
System.<span class="ST5">out</span>.println(list.removeAt(0));
System.<span class="ST5">out</span>.println(list);
System.<span class="ST5">out</span>.println(list.removeAt(2));
System.<span class="ST5">out</span>.println(list);
System.<span class="ST5">out</span>.println(list.removeAt(2));
System.<span class="ST5">out</span>.println(list.toString());
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.programmingquiz1_calebfontenot</groupId>
<artifactId>ProgrammingQuiz1_CalebFontenot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<exec.mainClass>com.mycompany.programmingquiz1_calebfontenot.ProgrammingQuiz1_CalebFontenot</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,101 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.programmingquiz1_calebfontenot;
/**
*
* @author ar114
* @param <T>
*/
public class DoubleLinkedListExam<T> {
Node<T> head;
Node<T> tail;
class Node<T> {
T t;
Node<T> l;
Node<T> r;
}
public T removeAt(int pos) {
return null;
}
public void clear() {
head = tail = null;
}
public void addAt(T t, int pos) {
System.out.println("Linked List size: " + this.size());
if (pos == 0) { // add first element
Node<T> newNode = new Node<T>();
newNode.t = t;
head = tail = newNode;
return;
}
if (pos == this.size()) { // add at end
Node<T> tmp = tail;
Node<T> newNode = new Node<T>();
newNode.t = t;
tmp.r = newNode;
newNode.l = tmp;
newNode.r = null;
tail = newNode;
return;
}
if (pos < this.size()) { // add in middle
Node<T> pointer = head;
for (int i = 0; i < this.size() - 1; ++i) {
pointer = pointer.r;
}
Node<T> newNode = new Node<T>();
newNode.t = t;
newNode.l = pointer;
newNode.r = pointer.l;
pointer.r = newNode;
pointer.r.l = newNode;
return;
}
if (pos < 0 || this.size() < pos) {
throw new IndexOutOfBoundsException("Given index is outside the range of acceptible values.");
}
}
public int size() {
Node<T> p = head;
int count = 0;
while (p != null) {
count++;
p = p.r;
}
return count;
}
@Override
public String toString() {
String s = "";
Node<T> p = head;
while (p != null) {
s += p.t.toString() + " ";
p = p.r;
}
return s;
}
public static void main(String[] args) {
DoubleLinkedListExam<Integer> list = new DoubleLinkedListExam<>();
list.addAt(10, 0);
System.out.println(list);
list.addAt(30, 1);
list.addAt(20, 1);
list.addAt(5, 0);
System.out.println(list);
}
}

View File

@@ -0,0 +1,41 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.programmingquiz1_calebfontenot;
/**
*
* @author ar114
*/
public class InsertionSort {
public static void insertionSort(String[] list)
{
int i = 1;
while (i < list.length)
{
if (list[i - 1].compareTo(list[i]) >= 0) {
String tmp = list[i - 1];
list[i - 1] = list[i];
list[i] = tmp;
}
++i;
//1. Loop that opens the hole by shifting to the right
//2. End of loop of shifting ---Insert the current at the opened hole list[j+1]
}
}
public static void printArray(String[] array) {
for (String current: array) {
System.out.print(current + " ");
}
System.out.println();
}
public static void main(String[] args) {
String[] array = {"Zedfrey", "Hello", "Glorious", "World"};
printArray(array);
insertionSort(array);
printArray(array);
}
}

View File

@@ -0,0 +1,17 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template
*/
package com.mycompany.programmingquiz1_calebfontenot;
/**
*
* @author ar114
*/
public class ProgrammingQuiz1_CalebFontenot {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

View File

@@ -0,0 +1,5 @@
#Generated by Maven
#Sun Dec 03 12:52:32 CST 2023
artifactId=ProgrammingQuiz1_CalebFontenot
groupId=com.mycompany.programmingquiz1_calebfontenot
version=1.0-SNAPSHOT

View File

@@ -0,0 +1,4 @@
com/mycompany/programmingquiz1_calebfontenot/InsertionSort.class
com/mycompany/programmingquiz1_calebfontenot/DoubleLinkedListExam.class
com/mycompany/programmingquiz1_calebfontenot/ProgrammingQuiz1_CalebFontenot.class
com/mycompany/programmingquiz1_calebfontenot/DoubleLinkedListExam$Node.class

View File

@@ -0,0 +1,3 @@
/home/caleb/ASDV-Java/Semester 3/Exams/Projects/ProgrammingQuiz1_CalebFontenot/src/main/java/com/mycompany/programmingquiz1_calebfontenot/ProgrammingQuiz1_CalebFontenot.java
/home/caleb/ASDV-Java/Semester 3/Exams/Projects/ProgrammingQuiz1_CalebFontenot/src/main/java/com/mycompany/programmingquiz1_calebfontenot/InsertionSort.java
/home/caleb/ASDV-Java/Semester 3/Exams/Projects/ProgrammingQuiz1_CalebFontenot/src/main/java/com/mycompany/programmingquiz1_calebfontenot/DoubleLinkedListExam.java

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>DAO</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.dao.DAO</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,20 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package DAO;
import java.util.Collection;
/**
*
* @author caleb
*/
public interface DAO<T> {
public void create(T t);
public void delete(T t);
public void update(T t);
public T find (T t);
public Collection<T> findALl();
public boolean updateDBase();
}

View File

@@ -0,0 +1,19 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package DAO;
/**
*
* @author caleb
*/
public interface Exam<T> {
void editExam(T t);
void takeExam(T t);
void submitExam(T t);
void previewExam(T t);
void monitorExam(T t);
void gradeExam(T t);
}

View File

@@ -0,0 +1,13 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package DAO;
/**
*
* @author caleb
*/
public abstract class ExamFactory<T extends Keyable> {
public abstract Exam<T> createExam();
}

View File

@@ -0,0 +1,56 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package DAO;
/**
*
* @author caleb
*/
public class ExamFactoryOutlet<K, V extends Keyable> extends ExamFactory<Keyable> {
@Override
public Exam<Keyable> createExam()
{
return new Exam<Keyable>() {
@Override
public void editExam(Keyable t)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void takeExam(Keyable t)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void submitExam(Keyable t)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void previewExam(Keyable t)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void monitorExam(Keyable t)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void gradeExam(Keyable t)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
};
}
}

View File

@@ -0,0 +1,15 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package DAO;
/**
*
* @author caleb
*/
public interface Keyable<K> {
public K getKey();
public void setKey(K key);
}

View File

@@ -0,0 +1,32 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package DAO;
import java.util.ArrayList;
/**
*
* @author caleb
*/
public class MathExam implements Keyable<MathExam> {
ArrayList<Object> fields = new ArrayList<Object>();
int key;
public MathExam(int key, ArrayList<Object> fields) {
}
@Override
public MathExam getKey()
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void setKey(MathExam key)
{
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
}

View File

@@ -0,0 +1,23 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package DAO;
/**
*
* @author caleb
*/
public class Test {
public static void main(String[] args)
{
ExamFactory<MathExam> efm = new ExamFactory<MathExam>() {
@Override
public Exam<MathExam> createExam()
{
System.out.println("Hello World");
}
};
efm.createExam();
}
}

View File

@@ -0,0 +1,31 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.dao.factory_pizza;
/**
*
* @author caleb
*/
public class CheezePizza implements Pizza {
@Override
public void prepare()
{
System.out.println("preparing Cheeze Pizza");
}
@Override
public void bake()
{
System.out.println("baking Cheeze Pizza");
}
@Override
public void cut()
{
System.out.println("cutting Cheeze Pizza");
}
}

View File

@@ -0,0 +1,31 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.dao.factory_pizza;
/**
*
* @author caleb
*/
public class ChickenPizza implements Pizza {
@Override
public void prepare()
{
System.out.println("preparing Chicken Pizza");
}
@Override
public void bake()
{
System.out.println("baking Chicken Pizza");
}
@Override
public void cut()
{
System.out.println("cutting Chicken Pizza");
}
}

View File

@@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package edu.slcc.asdv.caleb.dao.factory_pizza;
/**
*
* @author caleb
*/
public interface Pizza<T> {
public void prepare(T t);
public void bake(T t);
public void cut(T t);
}

View File

@@ -0,0 +1,29 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.dao.factory_pizza;
/**
*
* @author caleb
*/
public class PizzaFactory {
public PizzaFactory(String cheeze)
{
}
public static <T> Pizza<T> createPizza(T type) throws Exception {
Pizza p = null;
if (type.equals("cheeze"))
p = new CheezePizza();
else if (type.equals("chicken"))
p = new ChickenPizza();
else if (type.equals("veggie"))
p = new VeggiePizza();
if (p == null) {
throw new Exception("Not a valid Pizza.");
}
return p;
}
}

View File

@@ -0,0 +1,19 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.dao.factory_pizza;
/**
*
* @author caleb
*/
public class PizzaStore {
public Pizza orderPizza(String type) throws Exception {
Pizza p = PizzaFactory.createPizza(type);
p.prepare();
p.bake();
p.cut();
return p;
}
}

View File

@@ -0,0 +1,19 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.dao.factory_pizza;
/**
*
* @author caleb
*/
public class TestPizza {
public static void main(String[] args) throws Exception
{
PizzaStore ps = new PizzaStore();
ps.orderPizza("cheeze");
ps.orderPizza("chicken");
ps.orderPizza("veggie");
}
}

View File

@@ -0,0 +1,31 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.dao.factory_pizza;
/**
*
* @author caleb
*/
public class VeggiePizza implements Pizza {
@Override
public void prepare()
{
System.out.println("Preparing Veggie Pizza");
}
@Override
public void bake()
{
System.out.println("Baking Veggie Pizza");
}
@Override
public void cut()
{
System.out.println("Cutting Veggie Pizza");
}
}

View File

@@ -0,0 +1,29 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package factory_generic_pizza;
import edu.slcc.asdv.caleb.dao.factory_pizza.*;
/**
*
* @author caleb
*/
public class Pizza<T> {
public void prepare(Object t)
{
System.out.println("preparing " + t.toString() +" Pizza");
}
public void bake(Object t)
{
System.out.println("baking " + t.toString() +" Pizza");
}
public void cut(Object t)
{
System.out.println("cutting" + t.toString() +"Pizza");
}
}

View File

@@ -0,0 +1,32 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package factory_generic_pizza;
import edu.slcc.asdv.caleb.dao.factory_pizza.*;
/**
*
* @author caleb
*/
public class PizzaFactory {
public static <T> Pizza<T> createPizza(T t)
{
return new Pizza<T>()
{
public void prepare(T t) {
System.out.println("Preparing pizza " + t.toString() + "...");
}
public void bake(T t) {
System.out.println("Baking pizza " + t.toString() + "...");
}
public void cut(T t) {
System.out.println("Cutting pizza " + t.toString() + "...");
}
};
}
}

View File

@@ -0,0 +1,21 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package factory_generic_pizza;
import edu.slcc.asdv.caleb.dao.factory_pizza.*;
/**
*
* @author caleb
*/
public class PizzaStore {
public Pizza orderPizza(String type) throws Exception {
Pizza p = PizzaFactory.createPizza(type);
p.prepare(type);
p.bake(type);
p.cut(type);
return p;
}
}

View File

@@ -0,0 +1,21 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package factory_generic_pizza;
import edu.slcc.asdv.caleb.dao.factory_pizza.*;
/**
*
* @author caleb
*/
public class TestPizza {
public static void main(String[] args) throws Exception
{
PizzaStore ps = new PizzaStore();
ps.orderPizza("cheeze");
ps.orderPizza("chicken");
ps.orderPizza("veggie");
}
}

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.jdkPlatform>JDK_1.8</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>DataStructures</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.datastructures.DataStructures</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.caleb.datastructures;
/**
*
* @author caleb
*/
public class DataStructures {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

View File

@@ -0,0 +1,67 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.datastructures;
import java.util.Collection;
import java.util.Set;
/**
*
* @author caleb
*/
public interface OneToMany<One, Many>
{
/**
* Initialization of Ones. The method should be used first before any other
* methods
*
* @param one - the ones ( i.e, countries, or SelectItem or any Object) to
* use for initialization
* @return true if the initialization succeeded by using the method once,
* false when the method is used more than once.
*/
boolean initializeOne(One... one);
/**
* Initialization of the many for a given one. The method can be used
* multiple times after the method initializeOne has succeeded.
*
* @param one - the one that has the many
* @param many - the many which belong to th eone
* @throws IllegalArgumentException when the one does not exist (i.e. user's
* typing error for the name of one) or when the initialization of the one
* has not occurred.
*
*/
void initializeMany(One one, Many... many)
throws IllegalArgumentException;
/**
* Gets the many of a specific one.
*
* @param one the one to get its many
* @return the many of the parameter one or null if the one does not exist.
*/
Collection<Many> getMany(One one);
/**
* Given a value of the many it gets the one that the many belongs to.
*
* @param many one of the values of the many
* @return the one
*/
One getOne(Many many);
/**
* Gets a set with all the ones
*
* @return the set of ones.
*/
Set<One> getAllOnes();
}

View File

@@ -0,0 +1,185 @@
package edu.slcc.asdv.caleb.datastructures;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class OneToManyFactory {
/**
* Creates an object of type OneToMany
*
* @param <One> a generic parameter can be any object that denotes a One.
* @param <Many> a generic parameter can be any object that denotes a city that belongs to the one generic type.
* @return a OneCity object.
*/
public static <One, Many> //generic types to be used in the method
OneToMany<One, Many> //rturn type
createOneToMany()
{
return new OneToMany<One, Many>() {
private Map<One, Many> oneToMany = new HashMap();
boolean oneInitialized = false;
boolean manyInitialized = false;
@Override
public boolean initializeOne(One... one)
{
if (oneInitialized == false && manyInitialized == false) {
for (int i = 0; i < one.length; ++i) {
oneToMany.put(one[i], (Many) new Boolean(true));
}
oneInitialized = true;
return true;
}
return false;
}
@Override
public void initializeMany(One one, Many... many)
throws IllegalArgumentException
{
if (oneToMany.get(one) == null) { // of the key of the one is null
// the method initializekey has not been used
throw new IllegalArgumentException(one + " is not valid.");
}
oneToMany.put(one, (Many) new ArrayList<Many>(Arrays.asList(many)));
manyInitialized = true;
}
@Override
public Collection<Many> getMany(One one)
throws IllegalArgumentException
{
if (oneInitialized == true && manyInitialized == true) {
if (oneToMany.get(one) == null) {
throw new IllegalArgumentException(one + " is not a valid One");
}
Collection c1 = (Collection) oneToMany.get(one);
return c1;
}
return null;
}
@Override
public One getOne(Many many)
{
Set< Entry<One, Many>> set = oneToMany.entrySet();
for (Map.Entry<One, Many> entry : oneToMany.entrySet()) {
One key = (One) entry.getKey();
Collection<Many> value = (Collection<Many>) oneToMany.get(key);
if (value.contains(many)) {
return key;
}
}
return null;
}
@Override
public Set<One> getAllOnes()
{
return (Set<One>) oneToMany.keySet();
}
};
}
public static void main(String[] args)
{
OneToMany cc = OneToManyFactory.createOneToMany();
try {
cc.initializeMany("France", "Paris");
} catch (Exception e) {
System.err.println(e);
}
boolean b1 = cc.initializeOne("USA", "Greece");
System.out.println(b1);
boolean b2 = cc.initializeOne("USA", "Greece");
System.out.println(b2);
cc.initializeMany("USA", "Lafayette", "New Orleans");
cc.initializeMany("Greece", "Athens", "Sparta");
Collection<String> cities1 = cc.getMany("USA");
System.out.println(cities1);
Collection<String> cities2 = cc.getMany("Greece");
System.out.println(cities2);
System.out.println(cc.getOne("Athens"));
System.out.println(cc.getOne("Lafayette"));
System.out.println(cc.getOne("France"));
try {
System.out.println(cc.getMany("Germany"));
} catch (Exception e) {
System.err.println(e);
}
System.out.println(cc.getAllOnes());
System.out.println("----------------------------------------------------------");
OneToMany supplierParts = OneToManyFactory.createOneToMany();
supplierParts.initializeOne(new Supplier("s1"), new Supplier("s2"));
Supplier s1 = new Supplier("s1");
Supplier s2 = new Supplier("s2");
supplierParts.initializeOne(s1, s2);
Part p1 = new Part("p1");
Part p2 = new Part("p2");
Part p3 = new Part("p3");
Part p4 = new Part("p4");
supplierParts.initializeMany(s1, p1, p2);
supplierParts.initializeMany(s2, p3, p4);
}
}
class Supplier {
public Supplier(String name)
{
this.name = name;
}
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public String toString()
{
return "Supplier{" + "name=" + name + '}';
}
}
class Part {
public Part(String name)
{
this.name = name;
}
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public String toString()
{
return "Part{" + "name=" + name + '}';
}
}

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>HashMapASDV</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.hashmapasdv.HashMapASDV</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.caleb.hashmapasdv;
/**
*
* @author caleb
*/
public class HashMapASDV {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

Binary file not shown.

View File

@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="HashMapASDV_Ant" default="default" basedir=".">
<description>Builds, tests, and runs the project HashMapASDV_Ant.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="HashMapASDV_Ant-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,8 @@
build.xml.data.CRC32=2e2f5c1c
build.xml.script.CRC32=57d558c6
build.xml.stylesheet.CRC32=f85dc8f2@1.108.0.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=2e2f5c1c
nbproject/build-impl.xml.script.CRC32=4a925e01
nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.108.0.48

View File

@@ -0,0 +1,99 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
application.title=HashMapASDV_Ant
application.vendor=caleb
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.modulepath=\
${run.modulepath}
debug.test.classpath=\
${run.test.classpath}
debug.test.modulepath=\
${run.test.modulepath}
# Files in build.classes.dir which should be excluded from distribution jar
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/HashMapASDV_Ant.jar
dist.javadoc.dir=${dist.dir}/javadoc
dist.jlink.dir=${dist.dir}/jlink
dist.jlink.output=${dist.jlink.dir}/HashMapASDV_Ant
endorsed.classpath=
excludes=
file.reference.ListASDV.jar=ListASDV.jar
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.modulepath=
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
javac.source=20
javac.target=20
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
javac.test.modulepath=\
${javac.modulepath}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.html5=false
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
# The jlink additional root modules to resolve
jlink.additionalmodules=
# The jlink additional command line parameters
jlink.additionalparam=
jlink.launcher=true
jlink.launcher.name=HashMapASDV_Ant
main.class=hashmapasdv_ant.HashMapASDV_Ant
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
${build.classes.dir}:\
${file.reference.ListASDV.jar}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.modulepath=\
${javac.modulepath}
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
run.test.modulepath=\
${javac.test.modulepath}
source.encoding=UTF-8
src.dir=src
test.src.dir=test

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>HashMapASDV_Ant</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
</configuration>
</project>

View File

@@ -0,0 +1,91 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package hashmapasdv_ant;
import java.util.Map.Entry;
import java.util.Objects;
/**
*
* @author caleb
*/
class EntryASDV<K, V> implements Entry<K, V>, Comparable<K>
{
K key;
V value;
public EntryASDV(K key, V value)
{
this.key = key;
this.value = value;
}
@Override
public K getKey()
{
return key;
}
@Override
public V getValue()
{
return value;
}
@Override
public V setValue(V value)
{
V oldValue = this.value;
this.value = value;
return oldValue;
}
@Override
public String toString()
{
return "EntryASDV{" + "key=" + key + ", value=" + value + '}';
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final EntryASDV<?, ?> other = (EntryASDV<?, ?>) obj;
if (!Objects.equals(this.key, other.key))
{
return false;
}
return true;
}
/**
*
* @param o
* @return throws IllegalArgumentException if parameter class is not K
*/
@Override
public int compareTo(K o)
{
if (getClass() != o.getClass())
{
throw new IllegalArgumentException("ellegal parameter " + o);
}
return ((Comparable) key).compareTo(o);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>Hashing_CalebFontenot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.hashing_calebfontenot.Hashing_CalebFontenot</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,791 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.caleb.hashing_calebfontenot;
/**
*
* @author caleb
*/
import asdv.ListASDV;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;//needed in putAll
public class MapASDV<K, V> implements Map<K, V>, Cloneable
{
private int capacity = 4;
private double loadFactor = 0.75;
private ArrayList<ListASDV<EntryASDV<K, V>>> map = new ArrayList<ListASDV<EntryASDV<K, V>>>();
private Set<K> sharedKeySet = new SharedSet<K>();
private Set<Entry<K, V>> sharedEntrySet = new SharedSet<Entry<K, V>>();
private Collection<V> sharedValuesCollection = new SharedCollection<V>();
private class SharedCollection<V> extends ArrayList<V>
{
public boolean addValue(V v)
{
return this.add(v);
}
@Override
public boolean remove(Object o)
{
//> The parameter is key not entry if we are here
//>>remove value) and key) from the map
return MapASDV.this.removeFirstValue(o);
//>>remove key from shared values set
//return super.remove(o);
}
/**
* Removes one value from the collection. This method is meant to be
* called from out class. The overridden remove(Object o) of this inner
* class, calls the remove of the outer class(MapASDV), and the remove
* of the outer class, calls remove(V v, boolean callFromOuterClass)
* instead of remove(Object o) to avoid Stack Overflow when remover of
* inner calls remove of outer and vice versa.
*
* @param o - the key
* @param callFromOuterClass - dummy variable.
* @return true if the key was removed from the Set
*/
public boolean remove(V v, boolean callFromOuterClass)
{
//remove key from shared keys set
boolean b = super.remove(v);
return b;
}
@Override
public Object clone()
{
return super.clone();
}
@Override
public void clear()
{
super.clear();
}
}
private class SharedSet<E> extends HashSet<E>
{
@Override
public boolean add(E e)
{
throw new UnsupportedOperationException("Not supported....");
}
@Override
public boolean removeAll(Collection<?> c)
{
throw new UnsupportedOperationException("Not supported....");
}
/**
* Adds an EntryASDV to the set. It is private and cannot be used by the
* user of the Set. It is used from the MapASDV to add EntriesASDV to
* the SharedSet. Without this method we wouldn't be able to create a
* Set of keys or a Set of entries to give to the user via methods
* keySet() and entrySet() of this Map
*
* @param e EntryASDV
* @return true if the entry was added false otherwise
*/
private boolean addEntry(E e)
{
return super.add(e);
}
@Override
public boolean remove(Object o)
{
//>if parameter oo is EntryASDV call auxiliary method removeEntry
if (o instanceof EntryASDV)
{
return removeEntry((EntryASDV) o);
}
//> The parameter is key not entry if we are here
//>>remove key from the map
MapASDV.this.remove(o);
//>>remove key from shared keys set
return super.remove(o);
}
/**
* Removes the entry for the shared set
*
* @param entry the entry to be removed
* @return true if the entry was removed, false otherwise
*/
private boolean removeEntry(EntryASDV<K, V> entry)
{
MapASDV.this.remove(entry.getKey());
return super.remove(entry);
}
/**
* Removes the key from the set. This method is meant to be called from
* out class. The overridden remove(Object o) of this inner class calls
* the remove of the out class, and the remove of the outer class calls
* remove(K o, boolean callFromOuterClass) instead of remove(Object o)
* to avoid Stack Overflow when remover of inner calls remove of outer
* and vice versa.
*
* @param o - the key
* @param callFromOuterClass - dummy variable.
* @return true if the key was removed from the Set
*/
public boolean remove(E o, boolean callFromOuterClass)
{
//remove key from shared keys set
return super.remove(o);
}
@Override
public Object clone()
{
return super.clone();
}
@Override
public void clear()
{
super.clear();
}
}
public MapASDV()
{
for (int i = 0; i < capacity; ++i)
{
map.add(new ListASDV<EntryASDV<K, V>>());
}
}
/**
* Double the size of the Map and rehashes the entries of the Map
*/
private void doubleTheSizeOfTheMapAndRehash()
{
capacity *= 2;
//>create a new arrayList of ListsASDV
ArrayList<ListASDV<EntryASDV<K, V>>> newMap = new ArrayList<ListASDV<EntryASDV<K, V>>>();
//>Add at every enetry of the arrayList a new ASDVList
for (int i = 0; i < capacity; ++i)
{
newMap.add(new ListASDV<EntryASDV<K, V>>());
}
//>for the size of the OLD arrayList
for (int i = 0; i < map.size(); ++i)//array list
{
//>> get The ASDVlist at i
ListASDV<EntryASDV<K, V>> list = map.get(i);
//>>for the size() of the ASDVlist
for (int j = 0; j < list.size(); ++j)
{
///>>>hash and put in the the new array
int index = hash( list.get(j).getKey().hashCode() );
newMap.get(index).add(list.get(j));
}
}
map = newMap;
}
class EntryASDV<K, V> implements Entry<K, V>, Comparable<K>
{
K key;
V value;
public EntryASDV(K key, V value)
{
this.key = key;
this.value = value;
}
@Override
public K getKey()
{
return key;
}
@Override
public V getValue()
{
return value;
}
@Override
public V setValue(V value)
{
V oldValue = this.value;
this.value = value;
return oldValue;
}
@Override
public String toString()
{
return "EntryASDV{" + "key=" + key + ", value=" + value + '}';
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final EntryASDV<?, ?> other = (EntryASDV<?, ?>) obj;
if (!Objects.equals(this.key, other.key))
{
return false;
}
return true;
}
/**
*
* @param o
* @return throws IllegalArgumentException if parameter class is not K
*/
@Override
public int compareTo(K o)
{
if (getClass() != o.getClass())
{
throw new IllegalArgumentException("ellegal parameter " + o);
}
return ((Comparable) key).compareTo(o);
}
}
@Override
public int size()
{
return this.keySet().size();
}
@Override
public boolean isEmpty()
{
throw new UnsupportedOperationException("Not supported....");
}
@Override
public boolean containsKey(Object key)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Return an entry of the map
*
* @param key the key of the entry to be returned
* @return the entry, or null if the key is not in the map
*/
private EntryASDV<K, V> getEntryForKey(Object key)
{
for (ListASDV<EntryASDV<K, V>> list : map)
{
Iterator<EntryASDV<K, V>> it = list.iterator();
while (it.hasNext())
{
EntryASDV<K, V> entry = it.next();
if (key.equals(entry.getKey()))
{
return entry;
}
}
}
return null;
}
/**
* Returns the index of the given key
*
* @param key a key of the map
* @return the index of a key in the map or -1, if the key is not in the map
*/
private int getIndexForKey(Object key)
{
for (int i = 0; i < map.size(); ++i)
{
Iterator<EntryASDV<K, V>> it = map.get(i).iterator();
while (it.hasNext())
{
EntryASDV<K, V> entry = it.next();
if (key.equals(entry.getKey()))
{
return i;
}
}
}
return -1;
}
/**
* Returns true if this map maps one or more keys to the specified value.
* More formally, returns true if and only if this map contains at least one
* mapping to a value v such that (value==null ? v==null : value.equals(v)).
* This operation will probably require time linear in the map size for most
* implementations of the Map interface.
*
* Parameters: value - value whose presence in this map is to be tested
* Returns: true if this map maps one or more keys to the specified value
* Throws: ClassCastException - if the value is of an inappropriate type for
* this map (optional) NullPointerException - if the specified value is null
* and this map does not permit null values (optional)
*
* @param value - value whose presence in this map is to be tested
* @return true if this map maps one or more keys to the specified value
* @throws NullPointerException - if the specified value is null and this
* map does not permit null values (optional)
*/
@Override
public boolean containsValue(Object value)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Returns the value to which the specified key is mapped, or null if this
* map contains no mapping for the key.
*
* More formally, if this map contains a mapping from a key k to a value v
* such that (key==null ? k==null : key.equals(k)), then this method returns
* v; otherwise it returns null. (There can be at most one such mapping.)
*
* If this map permits null values, then a return value of null does not
* necessarily indicate that the map contains no mapping for the key; it's
* also possible that the map explicitly maps the key to null. The
* containsKey operation may be used to distinguish these two cases.
*
* @param key - the key whose associated value is to be returned
* @return the value to which the specified key is mapped, or null if this
* map contains no mapping for the key
* @throws NullPointerException - if the specified key is null and this map
* does not permit null keys (optional)
*/
@Override
public V get(Object key)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Associates the specified value with the specified key in this map
* (optional operation). If the map previously contained a mapping for the
* key, the old value is replaced by the specified value. (A map m is said
* to contain a mapping for a key k if and only if m.containsKey(k) would
* return true.)
*
* @param key - key with which the specified value is to be associated
* @param value - value to be associated with the specified key
* @return the previous value associated with key, or null if there was no
* mapping for key. (A null return can also indicate that the map previously
* associated null with key, if the implementation supports null values.)
* @throws NullPointerException - if specified key or value is null and this
* map does not permit null keys
*/
@Override
public V put(K key, V value)
{
if (key == null || value == null)
{
throw new NullPointerException("parm(s) null");
}
//>if contains the key, replace the key's value
EntryASDV<K, V> entry = getEntryForKey(key);
if (entry != null)
{
V oldValue = entry.value;
entry.value = value;
return oldValue;
}
///>>hash and put in the array
int code = this.hash(key.hashCode());
int index = hash(code);
ListASDV<EntryASDV<K, V>> list = map.get(index);
EntryASDV e = new EntryASDV(key, value);
list.add(e);
//>>add the key to the shared keys-set
((SharedSet<K>) this.sharedKeySet).addEntry(key);
((SharedSet<Entry<K, V>>) this.sharedEntrySet).addEntry(e);
//>>get the value of this entry
V v = list.get(list.size() - 1).getValue();
//>> add value to the value collection
((SharedCollection<V>) this.sharedValuesCollection).addValue(v);
//>> if reach 75% capacity double the size
if ((double) this.size() / capacity >= 0.75)
{
this.doubleTheSizeOfTheMapAndRehash();
}
//>>return the value of Entry just added
return v;
}
int hash(int keyHashCode)
{
int h = hashHash(keyHashCode);
return Math.abs(h % capacity - 1);
}
/**
* Removes the first entry with the given values.
*
* @param value - the value to be removed
* @return true if removed, false otherwise
* @throws NullPointerException if the value is null
*/
private boolean removeFirstValue(Object value)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Ensure hash code is evenly distributed
*
* @param h - hash code
* @return evenly distributed hash code
*/
private static int hashHash(int h)
{
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
/**
* Removes the mapping for a key from this map if it is present (optional
* operation). More formally, if this map contains a mapping from key k to
* value v such that (key==null ? k==null : key.equals(k)), that mapping is
* removed. (The map can contain at most one such mapping.)
*
* @param key - key whose mapping is to be removed from the map
* @return the previous value associated with key, or null if there was no
* mapping for key.
* @throws NullPointerException - if the specified key is null and this map
* does not permit null keys
*/
@Override
public V remove(Object key)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Copies all of the mappings from the specified map to this map (optional
* operation). The effect of this call is equivalent to that of calling
* put(k, v) on this map once for each mapping from key k to value v in the
* specified map. The behavior of this operation is undefined if the
* specified map is modified while the operation is in progress.
*
* @param m - mappings to be stored in this map
* @throws NullPointerException - if the specified map is null, or if this
* map does not permit null keys or values, and the specified map contains
* null keys
*/
@Override
public void putAll(Map<? extends K, ? extends V> m)
{
if (m == null)
{
throw new NullPointerException("null parameter");
}
BiConsumer consumeEachEntry = new BiConsumer<K, V>()
{
MapASDV<K, V> mapForConsumer = MapASDV.this;
@Override
public void accept(K k, V v)
{
mapForConsumer.put(k, v);
}
};
m.forEach(consumeEachEntry);
}
/**
* Removes all of the mappings from this map (optional operation). The map
* will be empty after this call returns. Any shared sets are also cleared.
*/
@Override
public void clear()
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Returns a Set view of the keys contained in this map. The set is backed
* by the map, so changes to the map are reflected in the set, and
* vice-versa. If the map is modified while an iteration over the set is in
* progress (except through the iterator's own remove operation), the
* results of the iteration are undefined. The set supports element removal,
* which removes the corresponding mapping from the map, via the
* Iterator.remove, Set.remove, removeAll, retainAll, and clear operations.
* It does not support the add or addAll operations.
*
* @return a set view of the keys contained in this map
*/
@Override
public Set<K> keySet()
{
return this.sharedKeySet;
}
/**
* Returns a Collection view of the values contained in this map. The
* collection is backed by the map, so changes to the map are reflected in
* the collection, and vice-versa. If the map is modified while an iteration
* over the collection is in progress (except through the iterator's own
* remove operation), the results of the iteration are undefined. The
* collection supports element removal, which removes the corresponding
* mapping from the map, via the Iterator.remove, Collection.remove,
* removeAll, retainAll and clear operations. It does not support the add or
* addAll operations.
*
* @return
*/
@Override
public Collection<V> values()
{
return sharedValuesCollection;
}
/**
* Returns a Set view of the mappings contained in this map. The set is
* backed by the map, so changes to the map are reflected in the set, and
* vice-versa. If the map is modified while an iteration over the set is in
* progress (except through the iterator's own remove operation, or through
* the setValue operation on a map entry returned by the iterator) the
* results of the iteration are undefined. The set supports element removal,
* which removes the corresponding mapping from the map, via the
* Iterator.remove, Set.remove, removeAll, retainAll and clear operations.
* It does not support the add or addAll operations.
*
*
* @return a set view of the mappings contained in this map
*/
@Override
public Set<Entry<K, V>> entrySet()
{
return this.sharedEntrySet;
}
@Override
public String toString()
{
String s = "[ ";
for (int i = 0; i < capacity; ++i)
{
s += map.get(i).toString() + "\n";
}
s += " ]";
return s;
}
/**
* Created a deep copy of the MapASDV
*
* @return the deep copy of the map
*/
@Override
public Object clone()
{
/*
MapASDV<K, V> clonedMap = new MapASDV<K, V>();
//clonedMap.putAll(this);
for (ListASDV< EntryASDV<K, V>> list : this.map)
{
ListASDV< EntryASDV<K, V>> l = (ListASDV< EntryASDV<K, V>>) list.clone();
clonedMap.map.add(l);
}
return clonedMap;
*/
MapASDV<K, V> clonedMap = new MapASDV<K, V>();
clonedMap.putAll(this);
return clonedMap;
}
public static void main(String[] args)
{
MapASDV<String, Integer> map = new MapASDV();
System.out.println("---------------------------testing put(K, V)");
map.put("ann", 20);
map.put("coco", 25);
System.out.println(map);
mapASDV.MapASDV<String, Integer> clonedMap = ( mapASDV.MapASDV<String, Integer>) map.clone();
System.out.println("\n\n---------------------------testing double-the-size-and-rehash by reaching 0.75 load factor with another put(K, V)");
map.put("Jonathan", 30);
System.out.println(map);
map.put("Jonhathan", 45);
System.out.println(map);
map.put("Alexander", 33);
System.out.println(map);
System.out.println("\n\n---------------------------testing putAll(Map<K,V>)");
Map<String, Integer> anotherJavaMap = new HashMap();
anotherJavaMap.put("lion king", 45);
anotherJavaMap.put("HYENA", 6);
map.putAll(anotherJavaMap);
System.out.println(map);
System.out.println("\n\n---------------------------testing containsKey");
System.out.println(map.containsKey("Alexander"));
System.out.println(map.containsKey("alexander"));
System.out.println("\n\n---------------------------testing containsValue");
System.out.println(map.containsValue(33));
System.out.println(map.containsValue(34));
System.out.println("\n\n---------------------------testing getEntryForKey");
Entry<String, Integer> e = map.getEntryForKey("Alexander");
System.out.println( map.getEntryForKey("Alexander"));
System.out.println( map.getEntryForKey("Alex"));
System.out.println("\n\n---------------------------testing get");
System.out.println(map.get( "Alexander") );
System.out.println(map.get( "Alex") );
try{ map.get( null);}catch (NullPointerException ex ){System.out.println(ex.getMessage());}
System.out.println("\n\n---------------------------testing getIndexForKey");
System.out.println(map.getIndexForKey("Alexander"));
System.out.println(map.getIndexForKey("Alex"));
System.out.println("\n\n---------------------------testing isEmpty");
System.out.println( map.isEmpty());
System.out.println("\n\n---------------------------testing size");
System.out.println( map.size());
System.out.println( map);
System.out.println("\n\n---------------------------testing entrySet()");
Set<Entry<String, Integer>> entries = map.entrySet();
System.out.println( entries);
System.out.println("\n\n---------------------------testing keySet()");
Set<String> keys = map.keySet();
System.out.println( keys );
System.out.println("\n\n---------------------------testing values()");
Collection<Integer> values = map.values();
System.out.println( values);
System.out.println("\n\n---------------------------testing remove( K) coco 25");
map.remove("coco");
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println(values);
System.out.println("\n\n---------------------------testing Entry-Collection remove ");
entries.remove( e);
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println(values);
System.out.println("\n\n---------------------------testing Set Keys remove ");
keys.remove( "ann");
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println(values);
System.out.println("\n\n---------------------------testing Set Values remove ");
values.remove( 45);
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println(values);
System.out.println("\n\n---------------------------testing clear ");
map.clear();
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println("\n\n---------------------------testing add of sets and collections ");
try{ keys.add( "a");}catch (Exception ex ){System.out.println(ex.getMessage());}
try{ values.add( 33);}catch (Exception ex ){System.out.println(ex.getMessage());}
try{ entries.add( e);}catch (Exception ex ){System.out.println(ex.getMessage());}
System.out.println("\n\n---------------------------testing clone");
System.out.println( clonedMap);
System.out.println("---------------------------testing put(K, V) AGAIN");
map.put("Nicholas", 100);
map.put("a", 200);
map.put("b", -20);
System.out.println( map);
}
}

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.jdkPlatform>Graal_JDK_20</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>MP1_ManyToMany_CalebFontenot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.mp1_manytomany_calebfontenot.MP1_ManyToMany_CalebFontenot</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,114 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.mp1_manytomany_calebfontenot;
/**
*
* @author caleb
*/
import java.util.List;
import java.util.Set;
/**
*
* @author A. V. Markou
* @param <Many1>
* @param <Many2>
*/
public interface ManyToMany<Many1, Many2>
{
/**
* Creates a Many to Many relationship between the parentLeft and the
* childrenRight. Example for ( Many1 a, Many2 1, 2, 3) it creates --> (a 1)
* ( a 2 ) ( a 3 ) and ( 1, a ), ( 2, a ), ( 3, a). No duplicate values of
* Many2 are allowed.
*
* @param parentLeft - exactly one Many1 object.
* @param childrenRight - one or more Many2 objects.
* @return the previous value associated with Many1, or null if there was no
* Many1 with the same "key".
* @throws ClassCastException - if the class of the specified Many1, or
* Many2 or value prevents it from being stored in this map( i,e Many1 is
* String and you pass an object)
* @throws NullPointerException - if the specified Many1 or Many2 is null
* and ManyToMany does not permit nulls as values.
* @throw IllegalArgumentException - if a duplicate exists in childrenRight
* list ( s1 --> p1,p2,p2 is not allowed).
* @return the previous value associated with parentLeft, or null if there
* was no childrenRight for parentLeft.
*/
List<Many2> add(Many1 parentLeft, Many2... childrenRight);
/**
* Returns the List of all left children of the parentRight.
*
* @param parentRight a parent at the RHS of the many to many relationship.
* @return the List of all left children of the parentRight.
* @throw IllegalArgumentException if the value of parameter parentRight
* does not exist in the RHS of the many to many relationship.
*/
List<Many1> getChildrenLeft(Many2 parentRight);
/**
* Returns the List of all right children of the parentLeft.
*
* @param parentLeft a parent at the LHS of the many to many relationship.
* @return the List of all right children of the parentLeft.
* @throws IllegalArgumentException if the value of parameter parentLeft
* does not exist on the LHS of the many to many relationship.
*/
List<Many2> getChildrenRight(Many1 parentLeft);
/**
* Returns a set of the Many1 elements that exist on the LHS of the many to
* many relationship.
*
* @return Set of Many1
*/
Set<Many1> getParentsLeft();
/**
* Returns a set of the Many2 elements that exist on the RHS of the many to
* many relationship.
*
* @return Set of Many2
*/
Set<Many2> getParentsRight();
/**
* Removes the many1 parameter from the LHS of the many relationship AND all
* its corresponding values that exist in the RHS of the many to many
* relationship. For example given: ( LHS e1: p1, p2 e2: p2, p3 RHS: p1: e1
* p2: e1, e2 p3: e2 after removing e1 from the LHS will results into: ( LHS
* e2: p2, p3 RHS: p2: e2 p3: e2
*
* @param many1 the unique element on the LHS to be removed.
* @throws NullPointerException if parameter many1 is null.
* @return true if the removal occurred, false if many1 does not exist in
* the LHS of the many to many relationship.
*/
boolean removeLeft(Many1 many1);
/**
* Removes the many1 parameter from the RHS of the many relationship AND all
* its corresponding values that exist in the LHS of the many to many
* relationship. For example given: LHS e2: p2, p3 RHS: p2: e2 p3: e2 after
* removing p2 from the RHS will results into: LHS e2: p3 RHS p3: e2
*
*
* @param many2 the unique element on the LHS to be removed.
* @throws NullPointerException if parameter many1 is null.
* @return true if the removal occurred, false if many1 does not exist in
* the LHS of the many to many relationship.
*/
boolean removeRight(Many2 many2);
/**
* Clears all.
*
*/
void clear();
}

View File

@@ -0,0 +1,233 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.mp1_manytomany_calebfontenot;
/**
*
* @author caleb
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
*
* @author asdv5
*/
public class ManyToManyFactory {
public static <Many1, Many2> //generic types to be used in the method
ManyToMany< Many1, Many2>//return type
createManyToMany() {
return new ManyToMany<Many1, Many2>() {
private HashSet<Many1> parents = new HashSet<>();
private Map<Object, Object> left = new HashMap<>();
private Map<Object, Object> right = new HashMap<>();
@Override
public String toString() {
return "{" + "left=" + left + ", right=" + right + '}';
}
/**
* Creates a Many to Many relationship between the parentLeft and the childrenRight. Example for ( Many1 a, Many2 1, 2, 3) it creates --> (a 1) ( a 2 ) ( a 3 ) and ( 1, a ), ( 2, a ), ( 3, a). No duplicate values of Many2 are allowed.
*
* @param parentLeft - exactly one Many1 object.
* @param childrenRight - one or more Many2 objects.
* @return the previous value associated with Many1, or null if there was no Many1 with the same "key".
* @throws ClassCastException - if the class of the specified Many1, or Many2 or value prevents it from being stored in this map( i,e Many1 is String and you pass an object)
* @throws NullPointerException - if the specified Many1 or Many2 is null and ManyToMany does not permit nulls as values.
* @throw IllegalArgumentException - if a duplicate exists in childrenRight list ( s1 --> p1,p2,p2 is not allowed).
* @return the previous value associated with parentLeft, or null if there was no childrenRight for parentLeft.
*/
@Override
public List<Many2> add(Many1 parentLeft, Many2... childrenRight) {
HashSet<Many2> verifyUnique = new HashSet<>();
List<Many2> returnList = new ArrayList<>();
// Check for exceptions
if (!childrenRight.equals(parentLeft.getClass())) {
//throw new ClassCastException();
}
if (parentLeft == null || childrenRight == null) {
throw new NullPointerException();
}
// Try to add all elements from childrenRight into HashSet. If we're unable to, through an IllegalArgumentException, assuming HashSet doesn't throw one already.
verifyUnique.addAll(Arrays.asList(childrenRight));
if (left.size() > 0 && childrenRight.length > 0) {
for (Object e : left.values()) {
returnList.add((Many2) e);
}
//returnList.addAll((<? extends Many2>) left.values());
}
// Keep track of the parents so we can manipulate the RHS
parents.add(parentLeft);
// Handle LHS
//for (Many2 e : childrenRight) {
left.put(parentLeft, new ArrayList<Many2>(Arrays.asList(childrenRight)));
//}
//Handle RHS
/*
for (Many1 e: parents) {
right.put(, e );
}
*/
if (returnList.size() == 0) {
return null;
}
return returnList;
}
/**
* Returns the List of all left children of the parentRight.
*
* @param parentRight a parent at the RHS of the many to many relationship.
* @return the List of all left children of the parentRight.
* @throw IllegalArgumentException if the value of parameter parentRight does not exist in the RHS of the many to many relationship.
*/
@Override
public List<Many1> getChildrenLeft(Many2 parentRight) {
List<Many1> returnList = new ArrayList<Many1>();
for (Entry<Object, Object> ee : left.entrySet()) {
System.out.println(ee.getKey() + ", " + parentRight);
if (ee.getKey().equals(parentRight)) {
returnList.add((Many1) ee.getValue());
}
}
if (returnList.size() > 0) {
return null;
}
return returnList;
}
/**
* Returns the List of all right children of the parentLeft.
*
* @param parentLeft a parent at the LHS of the many to many relationship.
* @return the List of all right children of the parentLeft.
* @throws IllegalArgumentException if the value of parameter parentLeft does not exist on the LHS of the many to many relationship.
*/
@Override
public List<Many2> getChildrenRight(Many1 parentLeft) {
List<Many2> returnList = new ArrayList<>();
for (Entry<Object, Object> ee : right.entrySet()) {
if (ee.getKey() == parentLeft) {
returnList.add((Many2) ee.getValue());
}
}
if (returnList.size() > 0) {
return null;
}
return returnList;
}
/**
* Returns a set of the Many1 elements that exist on the LHS of the many to many relationship.
*
* @return Set of Many1
*/
@Override
public Set<Many1> getParentsLeft() {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public Set<Many2> getParentsRight() {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public boolean removeLeft(Many1 many1) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public boolean removeRight(Many2 many2) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void clear() {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
};
}
public static void main(String[] args) throws InterruptedException {
ManyToMany<String, String> mm = ManyToManyFactory.createManyToMany();
//mm.add(1, 1);///will not compile, we have Many1, Many2 as String
System.out.println("add(e1, p1, p2)returns: " + mm.add("e1", "p1", "p2"));
System.out.println("add(e2, p2, p3)returns: " + mm.add("e2", "p2", "p3"));
//System.out.println("getParentsLeft returns: " + mm.getParentsLeft());
//System.out.println("getParentsRight returns: " + mm.getParentsRight());
System.out.println("getChildrenLeft(p1) returns: " + mm.getChildrenLeft("p2"));
System.out.println("getChildrenLeft(p2) returns: " + mm.getChildrenLeft("p3"));
System.out.println("getChildrenRight(e1) returns: " + mm.getChildrenRight("e1"));
System.out.println("getChildrenRight(e1) returns: " + mm.getChildrenRight("e2"));
System.out.println("-----------------------------------------------");
System.out.println("The internal hash maps of ManyToMany after insertions: " + mm);
System.out.println("----------AFTER REMOVAL of e1 LEFT----------------------------------------");
System.out.println("removeLeft(e1) returns: " + mm.removeLeft("e1"));
System.out.println("getParentsLeft returns: " + mm.getParentsLeft());
System.out.println("getParentsRight returns: " + mm.getParentsRight());
System.out.println("getChildrenRight(e2) returns: " + mm.getChildrenRight("e2"));
System.out.println("getChildrenLeft(p2) returns: " + mm.getChildrenLeft("p2"));
System.out.println("getChildrenLeft(p3) returns: " + mm.getChildrenLeft("p3"));
System.out.println("The internal hash maps of ManyToMany after removal LEFT: " + mm);
System.out.println("\n----------AFTER REMOVAL of p2 RIGHT----------------------------------------");
System.out.println("removeLeft(p2) returns: " + mm.removeRight("p2"));
System.out.println("getParentsLeft returns: " + mm.getParentsLeft());
System.out.println("getParentsRight returns: " + mm.getParentsRight());
System.out.println("getChildrenRight(e2) returns: " + mm.getChildrenRight("e2"));
System.out.println("getChildrenLeft(p3) returns: " + mm.getChildrenLeft("p3"));
System.out.println("--------------------------------------------------");
System.out.println("mm.removeLeft(e1) returns: " + mm.removeLeft("e1"));
System.out.println("The internal hash maps of ManyToMany after removal RIGHT: " + mm);
Thread.sleep(5000, 0);
System.out.println("---------------CATCHING EXCEPTIONS -----------------------------------");
try {
mm.getChildrenRight("e10");//e10 dos not exist
} catch (RuntimeException e) {
System.err.println("getChildrenRight for e10 throws exception:" + e);
}
try {
System.out.println(mm.add("e6", new String[]{
"value1", null
}));
} catch (RuntimeException e) {
System.err.println("add(e6, new String[]{null } ) throws exception: " + e);
}
try {
System.out.println(mm.add(null, "p1"));
} catch (RuntimeException e) {
System.err.println("add(null, p1)returns throws exception: " + e);
}
try {
System.out.println(mm.getChildrenLeft("p1"));
} catch (RuntimeException e) {
System.err.println("getChildrenRight(p1) throws exception: " + e);
}
try {
mm.add((String) new Object(), (String) new Object());
} catch (RuntimeException e) {
System.err.println("add((String) new Object(), (String) new Object()) throws exception: " + e);
}
mm.clear();
System.out.println("-----------------------------------------------");
System.out.println("The internal hash maps of ManyToMany after clear: " + mm);
}
}

View File

@@ -0,0 +1,172 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Matrices.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.number {color: #6897bb}
.string {color: #6a8759}
.ST4 {color: #9876aa}
.ST5 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST3 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST2 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 4/Assignments/MP2_CalebFontenot/src/main/java/com/calebfontenot/mp2_calebfontenot/Matrices.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp2_calebfontenot;
<span class="literal">import</span> java.math.BigInteger;
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> java.util.concurrent.ForkJoinPool;
<span class="literal">import</span> java.util.concurrent.RecursiveTask;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> Matrices {
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
Matrices.<span class="ST2">multiplyParallel</span>();
}
<span class="literal">public</span> <span class="literal">static</span> ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; <span class="ST1">createRandomMatrix</span>(<span class="literal">int</span> rows, <span class="literal">int</span> columns) {
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; matrix = <span class="literal">new</span> ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt;(rows);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; rows; ++i) {
ArrayList&lt;BigInteger&gt; row = <span class="literal">new</span> ArrayList&lt;BigInteger&gt;();
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; columns; ++j) {
row.add(<span class="literal">new</span> BigInteger(Integer.<span class="ST2">toString</span>(<span class="number">1</span> + (<span class="literal">int</span>) (Math.<span class="ST2">random</span>() * <span class="number">9</span>))));
}
matrix.add(row);
}
<span class="literal">return</span> matrix;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">multiplyParallel</span>() {
<span class="comment">/*</span>
<span class="comment"> ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; A = new ArrayList&lt;&gt;();</span>
<span class="comment"> ArrayList&lt;BigInteger&gt; row1 = new ArrayList&lt;&gt;();</span>
<span class="comment"> row1.add(new BigInteger(&quot;6&quot;));</span>
<span class="comment"> row1.add(new BigInteger(&quot;1&quot;));</span>
<span class="comment"> ArrayList&lt;BigInteger&gt; row2 = new ArrayList&lt;&gt;();</span>
<span class="comment"> row2.add(new BigInteger(&quot;7&quot;));</span>
<span class="comment"> row2.add(new BigInteger(&quot;2&quot;));</span>
<span class="comment"> A.add(row1);</span>
<span class="comment"> A.add(row2);</span>
<span class="comment"> ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; B = new ArrayList&lt;&gt;();</span>
<span class="comment"> row1 = new ArrayList&lt;BigInteger&gt;();</span>
<span class="comment"> row1.add(new BigInteger(&quot;4&quot;));</span>
<span class="comment"> row1.add(new BigInteger(&quot;1&quot;));</span>
<span class="comment"> row1.add(new BigInteger(&quot;2&quot;));</span>
<span class="comment"> row2 = new ArrayList&lt;BigInteger&gt;();</span>
<span class="comment"> row2.add(new BigInteger(&quot;1&quot;));</span>
<span class="comment"> row2.add(new BigInteger(&quot;9&quot;));</span>
<span class="comment"> row2.add(new BigInteger(&quot;5&quot;));</span>
<span class="comment"> B.add(row1);</span>
<span class="comment"> B.add(row2);</span>
<span class="comment"> */</span>
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; A = Matrices.<span class="ST2">createRandomMatrix</span>(<span class="number">2</span>, <span class="number">2</span>);
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; B = Matrices.<span class="ST2">createRandomMatrix</span>(<span class="number">2</span>, <span class="number">3</span>);
RecursiveTask&lt;ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt;&gt; rt
= <span class="literal">new</span> Matrices.MatricesMultiplication(<span class="number">0</span>, A.size() - <span class="number">1</span>, A, B);
ForkJoinPool pool = <span class="literal">new</span> ForkJoinPool();
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; mul = pool.invoke(rt);
System.<span class="ST3">out</span>.println(<span class="string">&quot;</span><span class="string">MATRIX A</span><span class="string">&quot;</span>);
<span class="ST2">printMatrix</span>(A);
System.<span class="ST3">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">MATRIX B</span><span class="string">&quot;</span>);
<span class="ST2">printMatrix</span>(B);
System.<span class="ST3">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string">MATRIX AxB</span><span class="string">&quot;</span>);
<span class="ST2">printMatrix</span>(mul);
}
<span class="literal">private</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">printMatrix</span>(ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; matrix) {
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; matrix.size(); ++i) {
System.<span class="ST3">out</span>.println(matrix.get(i));
}
}
<span class="literal">static</span> <span class="literal">class</span> <span class="ST2">MatricesMultiplication</span> <span class="literal">extends</span> RecursiveTask&lt;ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt;&gt; {
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; <span class="ST4">A</span>;
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; <span class="ST4">B</span>;
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; <span class="comment">AxB</span>;
<span class="literal">final</span> <span class="literal">int</span> <span class="ST4">HOW_MANY_ROWS_IN_PARALLEL</span> = <span class="number">3</span>;<span class="comment">// threshold</span>
<span class="literal">int</span> <span class="ST4">startIndex</span>;
<span class="literal">int</span> <span class="ST4">endIndex</span>;
<span class="literal">public</span> MatricesMultiplication(<span class="literal">int</span> startIndex, <span class="literal">int</span> endIndex, ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; A, ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; B) {
<span class="literal">this</span>.<span class="ST4">startIndex</span> = startIndex;
<span class="literal">this</span>.<span class="ST4">endIndex</span> = endIndex;
<span class="literal">this</span>.<span class="ST4">A</span> = A;
<span class="literal">this</span>.<span class="ST4">B</span> = B;
}
@Override
<span class="literal">protected</span> ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; <span class="ST5">compute</span>() {
<span class="comment">// Base case: if the number of rows in A is less than or equal to the threshold,</span>
<span class="comment">// perform matrix multiplication sequentially</span>
<span class="literal">if</span> (<span class="ST4">endIndex</span> - <span class="ST4">startIndex</span> + <span class="number">1</span> &lt;= <span class="ST4">HOW_MANY_ROWS_IN_PARALLEL</span>) {
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; result = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="ST4">startIndex</span>; i &lt;= <span class="ST4">endIndex</span>; i++) {
ArrayList&lt;BigInteger&gt; rowResult = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; <span class="ST4">B</span>.get(<span class="number">0</span>).size(); j++) {
BigInteger sum = BigInteger.<span class="ST3">ZERO</span>;
<span class="literal">for</span> (<span class="literal">int</span> k = <span class="number">0</span>; k &lt; <span class="ST4">A</span>.get(<span class="number">0</span>).size(); k++) {
sum = sum.add(<span class="ST4">A</span>.get(i).get(k).multiply(<span class="ST4">B</span>.get(k).get(j)));
}
rowResult.add(sum);
}
result.add(rowResult);
}
<span class="literal">return</span> result;
} <span class="literal">else</span> {
<span class="comment">// Split the task into smaller subtasks</span>
<span class="literal">int</span> middle = (<span class="ST4">startIndex</span> + <span class="ST4">endIndex</span>) / <span class="number">2</span>;
<span class="ST2">MatricesMultiplication</span> leftTask = <span class="literal">new</span> MatricesMultiplication(<span class="ST4">startIndex</span>, middle, <span class="ST4">A</span>, <span class="ST4">B</span>);
<span class="ST2">MatricesMultiplication</span> rightTask = <span class="literal">new</span> MatricesMultiplication(middle + <span class="number">1</span>, <span class="ST4">endIndex</span>, <span class="ST4">A</span>, <span class="ST4">B</span>);
<span class="comment">// Fork the subtasks</span>
leftTask.fork();
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; rightResult = rightTask.compute();
<span class="comment">// Join the results of the subtasks</span>
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; leftResult = leftTask.join();
<span class="comment">// Merge the results</span>
ArrayList&lt;ArrayList&lt;BigInteger&gt;&gt; result = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; leftResult.size(); i++) {
result.add(<span class="literal">new</span> ArrayList&lt;&gt;(leftResult.get(i)));
}
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; rightResult.size(); i++) {
result.add(<span class="literal">new</span> ArrayList&lt;&gt;(rightResult.get(i)));
}
<span class="literal">return</span> result;
}
}
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.calebfontenot</groupId>
<artifactId>MP2_CalebFontenot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<exec.mainClass>com.calebfontenot.mp2_calebfontenot.MP2_CalebFontenot</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.calebfontenot.mp2_calebfontenot;
/**
*
* @author caleb
*/
public class MP2_CalebFontenot {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

View File

@@ -0,0 +1,142 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.mp2_calebfontenot;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
/**
*
* @author caleb
*/
public class Matrices {
public static void main(String[] args) {
Matrices.multiplyParallel();
}
public static ArrayList<ArrayList<BigInteger>> createRandomMatrix(int rows, int columns) {
ArrayList<ArrayList<BigInteger>> matrix = new ArrayList<ArrayList<BigInteger>>(rows);
for (int i = 0; i < rows; ++i) {
ArrayList<BigInteger> row = new ArrayList<BigInteger>();
for (int j = 0; j < columns; ++j) {
row.add(new BigInteger(Integer.toString(1 + (int) (Math.random() * 9))));
}
matrix.add(row);
}
return matrix;
}
public static void multiplyParallel() {
/*
ArrayList<ArrayList<BigInteger>> A = new ArrayList<>();
ArrayList<BigInteger> row1 = new ArrayList<>();
row1.add(new BigInteger("6"));
row1.add(new BigInteger("1"));
ArrayList<BigInteger> row2 = new ArrayList<>();
row2.add(new BigInteger("7"));
row2.add(new BigInteger("2"));
A.add(row1);
A.add(row2);
ArrayList<ArrayList<BigInteger>> B = new ArrayList<>();
row1 = new ArrayList<BigInteger>();
row1.add(new BigInteger("4"));
row1.add(new BigInteger("1"));
row1.add(new BigInteger("2"));
row2 = new ArrayList<BigInteger>();
row2.add(new BigInteger("1"));
row2.add(new BigInteger("9"));
row2.add(new BigInteger("5"));
B.add(row1);
B.add(row2);
*/
ArrayList<ArrayList<BigInteger>> A = Matrices.createRandomMatrix(2, 2);
ArrayList<ArrayList<BigInteger>> B = Matrices.createRandomMatrix(2, 3);
RecursiveTask<ArrayList<ArrayList<BigInteger>>> rt
= new Matrices.MatricesMultiplication(0, A.size() - 1, A, B);
ForkJoinPool pool = new ForkJoinPool();
ArrayList<ArrayList<BigInteger>> mul = pool.invoke(rt);
System.out.println("MATRIX A");
printMatrix(A);
System.out.println("\nMATRIX B");
printMatrix(B);
System.out.println("\nMATRIX AxB");
printMatrix(mul);
}
private static void printMatrix(ArrayList<ArrayList<BigInteger>> matrix) {
for (int i = 0; i < matrix.size(); ++i) {
System.out.println(matrix.get(i));
}
}
static class MatricesMultiplication extends RecursiveTask<ArrayList<ArrayList<BigInteger>>> {
ArrayList<ArrayList<BigInteger>> A;
ArrayList<ArrayList<BigInteger>> B;
ArrayList<ArrayList<BigInteger>> AxB;
final int HOW_MANY_ROWS_IN_PARALLEL = 3;// threshold
int startIndex;
int endIndex;
public MatricesMultiplication(int startIndex, int endIndex, ArrayList<ArrayList<BigInteger>> A, ArrayList<ArrayList<BigInteger>> B) {
this.startIndex = startIndex;
this.endIndex = endIndex;
this.A = A;
this.B = B;
}
@Override
protected ArrayList<ArrayList<BigInteger>> compute() {
// Base case: if the number of rows in A is less than or equal to the threshold,
// perform matrix multiplication sequentially
if (endIndex - startIndex + 1 <= HOW_MANY_ROWS_IN_PARALLEL) {
ArrayList<ArrayList<BigInteger>> result = new ArrayList<>();
for (int i = startIndex; i <= endIndex; i++) {
ArrayList<BigInteger> rowResult = new ArrayList<>();
for (int j = 0; j < B.get(0).size(); j++) {
BigInteger sum = BigInteger.ZERO;
for (int k = 0; k < A.get(0).size(); k++) {
sum = sum.add(A.get(i).get(k).multiply(B.get(k).get(j)));
}
rowResult.add(sum);
}
result.add(rowResult);
}
return result;
} else {
// Split the task into smaller subtasks
int middle = (startIndex + endIndex) / 2;
MatricesMultiplication leftTask = new MatricesMultiplication(startIndex, middle, A, B);
MatricesMultiplication rightTask = new MatricesMultiplication(middle + 1, endIndex, A, B);
// Fork the subtasks
leftTask.fork();
ArrayList<ArrayList<BigInteger>> rightResult = rightTask.compute();
// Join the results of the subtasks
ArrayList<ArrayList<BigInteger>> leftResult = leftTask.join();
// Merge the results
ArrayList<ArrayList<BigInteger>> result = new ArrayList<>();
for (int i = 0; i < leftResult.size(); i++) {
result.add(new ArrayList<>(leftResult.get(i)));
}
for (int i = 0; i < rightResult.size(); i++) {
result.add(new ArrayList<>(rightResult.get(i)));
}
return result;
}
}
}
}

View File

@@ -0,0 +1,813 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>MapASDV.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.number {color: #6897bb}
.string {color: #6a8759}
.ST1 {color: #9876aa}
.ST3 {color: #ffc66d}
.ST4 {color: #8a653b}
.comment {color: #808080}
.whitespace {color: #505050}
.ST5 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST6 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST2 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 4/Assignments/MapASDV_CalebFontenot/src/mapasdv_calebfontenot/MapASDV.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> mapasdv_calebfontenot;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="comment">//import ListASDV;</span>
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> java.util.Collection;
<span class="literal">import</span> java.util.HashMap;
<span class="literal">import</span> java.util.HashSet;
<span class="literal">import</span> java.util.Iterator;
<span class="literal">import</span> java.util.Map;
<span class="literal">import</span> java.util.Objects;
<span class="literal">import</span> java.util.Set;
<span class="literal">import</span> java.util.function.BiConsumer;<span class="comment">//needed in putAll</span>
<span class="literal">public</span> <span class="literal">class</span> MapASDV&lt;K, V&gt; <span class="literal">implements</span> Map&lt;K, V&gt;, Cloneable {
<span class="literal">private</span> <span class="literal">int</span> <span class="ST1">capacity</span> = <span class="number">4</span>;
<span class="literal">private</span> <span class="literal">double</span> <span class="comment">loadFactor</span> = <span class="number">0.75</span>;
<span class="literal">private</span> ArrayList&lt;ListASDV&lt;EntryASDV&lt;K, V&gt;&gt;&gt; <span class="ST1">map</span> = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="literal">private</span> Set&lt;K&gt; <span class="ST1">sharedKeySet</span> = <span class="literal">new</span> SharedSet&lt;&gt;();
<span class="literal">private</span> Set&lt;<span class="ST2">Entry</span>&lt;K, V&gt;&gt; <span class="ST1">sharedEntrySet</span> = <span class="literal">new</span> SharedSet&lt;&gt;();
<span class="literal">private</span> Collection&lt;V&gt; <span class="ST1">sharedValuesCollection</span> = <span class="literal">new</span> SharedCollection&lt;&gt;();
<span class="literal">private</span> <span class="literal">class</span> SharedCollection&lt;V&gt; <span class="literal">extends</span> ArrayList&lt;V&gt; {
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">addValue</span>(V v) {
<span class="literal">return</span> <span class="literal">this</span>.add(v);
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">remove</span>(Object o) {
<span class="comment">//&gt; The parameter is key not entry if we are here</span>
<span class="comment">//&gt;&gt;remove value) and key) from the map</span>
<span class="literal">return</span> MapASDV.<span class="literal">this</span>.removeFirstValue(o);
<span class="comment">//&gt;&gt;remove key from shared values set</span>
<span class="comment">//return super.remove(o);</span>
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">one</span> <span class="comment">value</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">collection</span><span class="comment">.</span> <span class="comment">This</span> <span class="comment">method</span> <span class="comment">is</span> <span class="comment">meant</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">called</span> <span class="comment">from</span> <span class="comment">out</span> <span class="comment">class</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">overridden</span> <span class="comment">remove</span><span class="comment">(</span><span class="comment">Object</span> <span class="comment">o</span><span class="comment">) </span><span class="comment">of</span> <span class="comment">this</span> <span class="comment">inner</span> <span class="comment">class</span><span class="comment">, </span><span class="comment">calls</span> <span class="comment">the</span> <span class="comment">remove</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">outer</span> <span class="comment">class</span><span class="comment">(</span><span class="comment">MapASDV</span><span class="comment">), </span><span class="comment">and</span> <span class="comment">the</span> <span class="comment">remove</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">outer</span> <span class="comment">class</span><span class="comment">, </span><span class="comment">calls</span> <span class="comment">remove</span><span class="comment">(</span><span class="comment">V</span> <span class="comment">v</span><span class="comment">, </span><span class="comment">boolean</span> <span class="comment">callFromOuterClass</span><span class="comment">) </span><span class="comment">instead</span> <span class="comment">of</span> <span class="comment">remove</span><span class="comment">(</span><span class="comment">Object</span> <span class="comment">o</span><span class="comment">) </span><span class="comment">to</span> <span class="comment">avoid</span> <span class="comment">Stack</span> <span class="comment">Overflow</span> <span class="comment">when</span> <span class="comment">remover</span> <span class="comment">of</span> <span class="comment">inner</span> <span class="comment">calls</span> <span class="comment">remove</span> <span class="comment">of</span> <span class="comment">outer</span> <span class="comment">and</span> <span class="comment">vice</span> <span class="comment">versa</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">o</span><span class="comment"> - </span><span class="comment">the</span> <span class="comment">key</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">callFromOuterClass</span><span class="comment"> - </span><span class="comment">dummy</span> <span class="comment">variable</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">key</span> <span class="comment">was</span> <span class="comment">removed</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">Set</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">remove</span>(V v, <span class="literal">boolean</span> callFromOuterClass) {
<span class="comment">//remove key from shared keys set</span>
<span class="literal">boolean</span> b = <span class="literal">super</span>.remove(v);
<span class="literal">return</span> b;
}
@Override
<span class="literal">public</span> Object <span class="ST3">clone</span>() {
<span class="literal">return</span> <span class="literal">super</span>.clone();
}
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">clear</span>() {
<span class="literal">super</span>.clear();
}
}
<span class="literal">private</span> <span class="literal">class</span> SharedSet&lt;E&gt; <span class="literal">extends</span> HashSet&lt;E&gt; {
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">add</span>(E e) {
<span class="literal">return</span> <span class="literal">super</span>.add(e);
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">removeAll</span>(Collection&lt;?&gt; c) {
<span class="literal">return</span> <span class="literal">super</span>.removeAll(c);
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Adds</span> <span class="comment">an</span> <span class="comment">EntryASDV</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">set</span><span class="comment">.</span> <span class="comment">It</span> <span class="comment">is</span> <span class="comment">private</span> <span class="comment">and</span> <span class="comment">cannot</span> <span class="comment">be</span> <span class="comment">used</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">user</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">Set</span><span class="comment">.</span> <span class="comment">It</span> <span class="comment">is</span> <span class="comment">used</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">MapASDV</span> <span class="comment">to</span> <span class="comment">add</span> <span class="comment">EntriesASDV</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">SharedSet</span><span class="comment">.</span> <span class="comment">Without</span> <span class="comment">this</span> <span class="comment">method</span> <span class="comment">we</span> <span class="comment">wouldn</span><span class="comment">&#39;</span><span class="comment">t</span> <span class="comment">be</span> <span class="comment">able</span> <span class="comment">to</span> <span class="comment">create</span> <span class="comment">a</span> <span class="comment">Set</span> <span class="comment">of</span> <span class="comment">keys</span> <span class="comment">or</span> <span class="comment">a</span> <span class="comment">Set</span> <span class="comment">of</span> <span class="comment">entries</span> <span class="comment">to</span> <span class="comment">give</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">user</span> <span class="comment">via</span> <span class="comment">methods</span> <span class="comment">keySet</span><span class="comment">() </span><span class="comment">and</span> <span class="comment">entrySet</span><span class="comment">() </span><span class="comment">of</span> <span class="comment">this</span> <span class="comment">Map</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">e</span> <span class="comment">EntryASDV</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">entry</span> <span class="comment">was</span> <span class="comment">added</span> <span class="comment">false</span> <span class="comment">otherwise</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">boolean</span> <span class="ST3">addEntry</span>(E e) {
<span class="literal">return</span> <span class="literal">super</span>.add(e);
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">remove</span>(Object o) {
<span class="comment">//&gt;if parameter oo is EntryASDV call auxiliary method removeEntry</span>
<span class="literal">if</span> (o <span class="literal">instanceof</span> EntryASDV) {
<span class="literal">return</span> removeEntry((EntryASDV) o);
}
<span class="comment">//&gt; The parameter is key not entry if we are here</span>
<span class="comment">//&gt;&gt;remove key from the map</span>
MapASDV.<span class="literal">this</span>.remove(o);
<span class="comment">//&gt;&gt;remove key from shared keys set</span>
<span class="literal">return</span> <span class="literal">super</span>.remove(o);
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">the</span> <span class="comment">entry</span> <span class="comment">for</span> <span class="comment">the</span> <span class="comment">shared</span> <span class="comment">set</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">entry</span> <span class="comment">the</span> <span class="comment">entry</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">removed</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">entry</span> <span class="comment">was</span> <span class="comment">removed</span><span class="comment">, </span><span class="comment">false</span> <span class="comment">otherwise</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">boolean</span> <span class="ST3">removeEntry</span>(EntryASDV&lt;K, V&gt; entry) {
MapASDV.<span class="literal">this</span>.remove(entry.getKey());
<span class="literal">return</span> <span class="literal">super</span>.remove(entry);
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">the</span> <span class="comment">key</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">set</span><span class="comment">.</span> <span class="comment">This</span> <span class="comment">method</span> <span class="comment">is</span> <span class="comment">meant</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">called</span> <span class="comment">from</span> <span class="comment">out</span> <span class="comment">class</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">overridden</span> <span class="comment">remove</span><span class="comment">(</span><span class="comment">Object</span> <span class="comment">o</span><span class="comment">) </span><span class="comment">of</span> <span class="comment">this</span> <span class="comment">inner</span> <span class="comment">class</span> <span class="comment">calls</span> <span class="comment">the</span> <span class="comment">remove</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">out</span> <span class="comment">class</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">the</span> <span class="comment">remove</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">outer</span> <span class="comment">class</span> <span class="comment">calls</span> <span class="comment">remove</span><span class="comment">(</span><span class="comment">K</span> <span class="comment">o</span><span class="comment">, </span><span class="comment">boolean</span> <span class="comment">callFromOuterClass</span><span class="comment">) </span><span class="comment">instead</span> <span class="comment">of</span> <span class="comment">remove</span><span class="comment">(</span><span class="comment">Object</span> <span class="comment">o</span><span class="comment">) </span><span class="comment">to</span> <span class="comment">avoid</span> <span class="comment">Stack</span> <span class="comment">Overflow</span> <span class="comment">when</span> <span class="comment">remover</span> <span class="comment">of</span> <span class="comment">inner</span> <span class="comment">calls</span> <span class="comment">remove</span> <span class="comment">of</span> <span class="comment">outer</span> <span class="comment">and</span> <span class="comment">vice</span> <span class="comment">versa</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">o</span><span class="comment"> - </span><span class="comment">the</span> <span class="comment">key</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">callFromOuterClass</span><span class="comment"> - </span><span class="comment">dummy</span> <span class="comment">variable</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">key</span> <span class="comment">was</span> <span class="comment">removed</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">Set</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">remove</span>(E o, <span class="literal">boolean</span> callFromOuterClass) {
<span class="comment">//remove key from shared keys set</span>
<span class="literal">return</span> <span class="literal">super</span>.remove(o);
}
@Override
<span class="literal">public</span> Object <span class="ST3">clone</span>() {
<span class="literal">return</span> <span class="literal">super</span>.clone();
}
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">clear</span>() {
<span class="literal">super</span>.clear();
}
}
<span class="literal">public</span> MapASDV() {
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">capacity</span>; ++i) {
<span class="ST1">map</span>.add(<span class="literal">new</span> ListASDV&lt;EntryASDV&lt;K, V&gt;&gt;());
}
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Double</span> <span class="comment">the</span> <span class="comment">size</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">Map</span> <span class="comment">and</span> <span class="comment">rehashes</span> <span class="comment">the</span> <span class="comment">entries</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">Map</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">void</span> <span class="ST3">doubleTheSizeOfTheMapAndRehash</span>() {
<span class="ST1">capacity</span> *= <span class="number">2</span>;
<span class="comment">//&gt;create a new arrayList of ListsASDV</span>
ArrayList&lt;ListASDV&lt;EntryASDV&lt;K, V&gt;&gt;&gt; newMap = <span class="literal">new</span> ArrayList&lt;ListASDV&lt;EntryASDV&lt;K, V&gt;&gt;&gt;();
<span class="comment">//&gt;Add at every enetry of the arrayList a new ASDVList</span>
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">capacity</span>; ++i) {
newMap.add(<span class="literal">new</span> ListASDV&lt;EntryASDV&lt;K, V&gt;&gt;());
}
<span class="comment">//&gt;for the size of the OLD arrayList</span>
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">map</span>.size(); ++i)<span class="comment">//array list</span>
{
<span class="comment">//&gt;&gt; get The ASDVlist at i</span>
ListASDV&lt;EntryASDV&lt;K, V&gt;&gt; list = <span class="ST1">map</span>.get(i);
<span class="comment">//&gt;&gt;for the size() of the ASDVlist</span>
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; list.size(); ++j) {
<span class="comment">///&gt;&gt;&gt;hash and put in the the new array </span>
<span class="literal">int</span> index = hash(list.get(j).getKey().hashCode());
newMap.get(index).add(list.get(j));
}
}
<span class="ST1">map</span> = newMap;
}
<span class="literal">class</span> EntryASDV&lt;K, V&gt; <span class="literal">implements</span> <span class="ST2">Entry</span>&lt;K, V&gt;, Comparable&lt;K&gt; {
K <span class="ST1">key</span>;
V <span class="ST1">value</span>;
<span class="literal">public</span> EntryASDV(K key, V value) {
<span class="literal">this</span>.<span class="ST1">key</span> = key;
<span class="literal">this</span>.<span class="ST1">value</span> = value;
}
@Override
<span class="literal">public</span> K <span class="ST3">getKey</span>() {
<span class="literal">return</span> <span class="ST1">key</span>;
}
@Override
<span class="literal">public</span> V <span class="ST3">getValue</span>() {
<span class="literal">return</span> <span class="ST1">value</span>;
}
@Override
<span class="literal">public</span> V <span class="ST3">setValue</span>(V value) {
V oldValue = <span class="literal">this</span>.<span class="ST1">value</span>;
<span class="literal">this</span>.<span class="ST1">value</span> = value;
<span class="literal">return</span> oldValue;
}
@Override
<span class="literal">public</span> String <span class="ST3">toString</span>() {
<span class="literal">return</span> <span class="string">&quot;</span><span class="string">EntryASDV{</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">key=</span><span class="string">&quot;</span> + <span class="ST1">key</span> + <span class="string">&quot;</span><span class="string">, value=</span><span class="string">&quot;</span> + <span class="ST1">value</span> + <span class="string">&#39;</span><span class="string">}</span><span class="string">&#39;</span>;
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">equals</span>(Object obj) {
<span class="literal">if</span> (<span class="literal">this</span> == obj) {
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="literal">if</span> (obj == <span class="literal">null</span>) {
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="literal">if</span> (getClass() != obj.getClass()) {
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="literal">final</span> EntryASDV&lt;?, ?&gt; other = (EntryASDV&lt;?, ?&gt;) obj;
<span class="literal">if</span> (!Objects.<span class="ST2">equals</span>(<span class="literal">t</span><span class="literal">his</span>.<span class="ST1">key</span>, other.<span class="ST1">key</span>)) {
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">o</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">throws</span> <span class="comment">IllegalArgumentException</span> <span class="comment">if</span> <span class="comment">parameter</span> <span class="comment">class</span> <span class="comment">is</span> <span class="comment">not</span> <span class="comment">K</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST3">compareTo</span>(K o) {
<span class="literal">if</span> (getClass() != o.getClass()) {
<span class="literal">throw</span> <span class="literal">new</span> IllegalArgumentException(<span class="string">&quot;</span><span class="string">ellegal parameter </span><span class="string">&quot;</span> + o);
}
<span class="literal">return</span> ((Comparable) <span class="ST1">key</span>).compareTo(o);
}
}
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST3">size</span>() {
<span class="literal">return</span> <span class="literal">this</span>.keySet().size();
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">isEmpty</span>() {
<span class="literal">if</span> (<span class="ST1">map</span>.size() == <span class="number">0</span>) {
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="comment">// done</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">containsKey</span>(Object key) {
String dashes = <span class="string">&quot;</span><span class="string">---------</span><span class="string">&quot;</span>;
System.<span class="ST5">out</span>.println(dashes + <span class="string">&quot;</span><span class="string"> containsKey(</span><span class="string">&quot;</span> + key.toString() + <span class="string">&quot;</span><span class="string">) </span><span class="string">&quot;</span> + dashes);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">map</span>.size(); ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; <span class="ST1">map</span>.get(i).size(); ++j) {
<span class="literal">if</span> (<span class="ST1">map</span>.get(i).get(j).<span class="ST1">key</span>.equals(key)) {
<span class="literal">return</span> <span class="literal">true</span>;
}
}
}
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Return</span> <span class="comment">an</span> <span class="comment">entry</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">map</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">key</span> <span class="comment">the</span> <span class="comment">key</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">entry</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">returned</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">entry</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">null</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">key</span> <span class="comment">is</span> <span class="comment">not</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">map</span>
<span class="comment">*/</span>
<span class="literal">private</span> EntryASDV&lt;K, V&gt; <span class="ST3">getEntryForKey</span>(Object key) {
<span class="literal">for</span> (ListASDV&lt;EntryASDV&lt;K, V&gt;&gt; list : <span class="ST1">map</span>) {
Iterator&lt;EntryASDV&lt;K, V&gt;&gt; it = list.iterator();
<span class="literal">while</span> (it.hasNext()) {
EntryASDV&lt;K, V&gt; entry = it.next();
<span class="literal">if</span> (key.equals(entry.getKey())) {
<span class="literal">return</span> entry;
}
}
}
<span class="literal">return</span> <span class="literal">null</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">the</span> <span class="comment">index</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">given</span> <span class="comment">key</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">key</span> <span class="comment">a</span> <span class="comment">key</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">map</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">index</span> <span class="comment">of</span> <span class="comment">a</span> <span class="comment">key</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">or</span><span class="comment"> -1, </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">key</span> <span class="comment">is</span> <span class="comment">not</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">map</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">int</span> <span class="ST3">getIndexForKey</span>(Object key) {
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">map</span>.size(); ++i) {
Iterator&lt;EntryASDV&lt;K, V&gt;&gt; it = <span class="ST1">map</span>.get(i).iterator();
<span class="literal">while</span> (it.hasNext()) {
EntryASDV&lt;K, V&gt; entry = it.next();
<span class="literal">if</span> (key.equals(entry.getKey())) {
<span class="literal">return</span> i;
}
}
}
<span class="literal">return</span> -<span class="number">1</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">maps</span> <span class="comment">one</span> <span class="comment">or</span> <span class="comment">more</span> <span class="comment">keys</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span><span class="comment">.</span> <span class="comment">More</span> <span class="comment">formally</span><span class="comment">, </span><span class="comment">returns</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">and</span> <span class="comment">only</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">contains</span> <span class="comment">at</span> <span class="comment">least</span> <span class="comment">one</span> <span class="comment">mapping</span> <span class="comment">to</span> <span class="comment">a</span> <span class="comment">value</span> <span class="comment">v</span> <span class="comment">such</span> <span class="comment">that</span><span class="comment"> (</span><span class="comment">value</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> ? </span><span class="comment">v</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> : </span><span class="comment">value</span><span class="comment">.</span><span class="comment">equals</span><span class="comment">(</span><span class="comment">v</span><span class="comment">))</span><span class="comment">.</span> <span class="comment">This</span> <span class="comment">operation</span> <span class="comment">will</span> <span class="comment">probably</span> <span class="comment">require</span> <span class="comment">time</span> <span class="comment">linear</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">size</span> <span class="comment">for</span> <span class="comment">most</span> <span class="comment">implementations</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">Map</span> <span class="comment">interface</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">Parameters</span><span class="comment">: </span><span class="comment">value</span><span class="comment"> - </span><span class="comment">value</span> <span class="comment">whose</span> <span class="comment">presence</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">is</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">tested</span> <span class="comment">Returns</span><span class="comment">: </span><span class="comment">true</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">maps</span> <span class="comment">one</span> <span class="comment">or</span> <span class="comment">more</span> <span class="comment">keys</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span> <span class="comment">Throws</span><span class="comment">: </span><span class="comment">ClassCastException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">of</span> <span class="comment">an</span> <span class="comment">inappropriate</span> <span class="comment">type</span> <span class="comment">for</span> <span class="comment">this</span> <span class="comment">map</span><span class="comment"> (</span><span class="comment">optional</span><span class="comment">) </span><span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">values</span><span class="comment"> (</span><span class="comment">optional</span><span class="comment">)</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">value</span><span class="comment"> - </span><span class="comment">value</span> <span class="comment">whose</span> <span class="comment">presence</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">is</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">tested</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">maps</span> <span class="comment">one</span> <span class="comment">or</span> <span class="comment">more</span> <span class="comment">keys</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">values</span><span class="comment"> (</span><span class="comment">optional</span><span class="comment">)</span>
<span class="comment">*/</span>
<span class="comment">//done</span>
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">containsValue</span>(Object value) {
String dashes = <span class="string">&quot;</span><span class="string">---------</span><span class="string">&quot;</span>;
System.<span class="ST5">out</span>.println(dashes + <span class="string">&quot;</span><span class="string"> containsValue(</span><span class="string">&quot;</span> + value.toString() + <span class="string">&quot;</span><span class="string">) </span><span class="string">&quot;</span> + dashes);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">map</span>.size(); ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; <span class="ST1">map</span>.get(i).size(); ++j) {
<span class="literal">if</span> (<span class="ST1">map</span>.get(i).get(j).<span class="ST1">value</span>.equals(value)) {
<span class="literal">return</span> <span class="literal">true</span>;
}
}
}
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">to</span> <span class="comment">which</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">key</span> <span class="comment">is</span> <span class="comment">mapped</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">null</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">contains</span> <span class="comment">no</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">the</span> <span class="comment">key</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">More</span> <span class="comment">formally</span><span class="comment">, </span><span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">contains</span> <span class="comment">a</span> <span class="comment">mapping</span> <span class="comment">from</span> <span class="comment">a</span> <span class="comment">key</span> <span class="comment">k</span> <span class="comment">to</span> <span class="comment">a</span> <span class="comment">value</span> <span class="comment">v</span> <span class="comment">such</span> <span class="comment">that</span><span class="comment"> (</span><span class="comment">key</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> ? </span><span class="comment">k</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> : </span><span class="comment">key</span><span class="comment">.</span><span class="comment">equals</span><span class="comment">(</span><span class="comment">k</span><span class="comment">)), </span><span class="comment">then</span> <span class="comment">this</span> <span class="comment">method</span> <span class="comment">returns</span> <span class="comment">v</span><span class="comment">; </span><span class="comment">otherwise</span> <span class="comment">it</span> <span class="comment">returns</span> <span class="comment">null</span><span class="comment">.</span><span class="comment"> (</span><span class="comment">There</span> <span class="comment">can</span> <span class="comment">be</span> <span class="comment">at</span> <span class="comment">most</span> <span class="comment">one</span> <span class="comment">such</span> <span class="comment">mapping</span><span class="comment">.</span><span class="comment">)</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">If</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">permits</span> <span class="comment">null</span> <span class="comment">values</span><span class="comment">, </span><span class="comment">then</span> <span class="comment">a</span> <span class="comment">return</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">null</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">necessarily</span> <span class="comment">indicate</span> <span class="comment">that</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">contains</span> <span class="comment">no</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">the</span> <span class="comment">key</span><span class="comment">; </span><span class="comment">it</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">also</span> <span class="comment">possible</span> <span class="comment">that</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">explicitly</span> <span class="comment">maps</span> <span class="comment">the</span> <span class="comment">key</span> <span class="comment">to</span> <span class="comment">null</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">containsKey</span> <span class="comment">operation</span> <span class="comment">may</span> <span class="comment">be</span> <span class="comment">used</span> <span class="comment">to</span> <span class="comment">distinguish</span> <span class="comment">these</span> <span class="comment">two</span> <span class="comment">cases</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">key</span><span class="comment"> - </span><span class="comment">the</span> <span class="comment">key</span> <span class="comment">whose</span> <span class="comment">associated</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">returned</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">to</span> <span class="comment">which</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">key</span> <span class="comment">is</span> <span class="comment">mapped</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">null</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">contains</span> <span class="comment">no</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">the</span> <span class="comment">key</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">key</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">keys</span><span class="comment"> (</span><span class="comment">optional</span><span class="comment">)</span>
<span class="comment">*/</span>
<span class="comment">//done</span>
@Override
<span class="literal">public</span> V <span class="ST3">get</span>(Object key) {
String dashes = <span class="string">&quot;</span><span class="string">---------</span><span class="string">&quot;</span>;
<span class="literal">try</span> {
System.<span class="ST5">out</span>.println(dashes + <span class="string">&quot;</span><span class="string"> get(</span><span class="string">&quot;</span> + key.toString() + <span class="string">&quot;</span><span class="string">) </span><span class="string">&quot;</span> + dashes);
} <span class="literal">catch</span> (NullPointerException ex) {
<span class="comment">//System.out.println(ex);</span>
System.<span class="ST5">out</span>.println(dashes + <span class="string">&quot;</span><span class="string"> get(</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">null</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">) </span><span class="string">&quot;</span> + dashes);
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">null parameter</span><span class="string">&quot;</span>);
}
Object currentKey = <span class="literal">null</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">map</span>.size(); ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; <span class="ST1">map</span>.get(i).size(); ++j) {
currentKey = <span class="ST1">map</span>.get(i).get(j).<span class="ST1">key</span>;
<span class="literal">if</span> (currentKey.equals(key)) {
<span class="literal">return</span> <span class="ST1">map</span>.get(i).get(j).<span class="ST1">value</span>;
}
}
}
<span class="literal">return</span> <span class="literal">null</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Associates</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">key</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span><span class="comment"> (</span><span class="comment">optional</span> <span class="comment">operation</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">previously</span> <span class="comment">contained</span> <span class="comment">a</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">the</span> <span class="comment">key</span><span class="comment">, </span><span class="comment">the</span> <span class="comment">old</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">replaced</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span><span class="comment">.</span><span class="comment"> (</span><span class="comment">A</span> <span class="comment">map</span> <span class="comment">m</span> <span class="comment">is</span> <span class="comment">said</span> <span class="comment">to</span> <span class="comment">contain</span> <span class="comment">a</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">a</span> <span class="comment">key</span> <span class="comment">k</span> <span class="comment">if</span> <span class="comment">and</span> <span class="comment">only</span> <span class="comment">if</span> <span class="comment">m</span><span class="comment">.</span><span class="comment">containsKey</span><span class="comment">(</span><span class="comment">k</span><span class="comment">) </span><span class="comment">would</span> <span class="comment">return</span> <span class="comment">true</span><span class="comment">.</span><span class="comment">)</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">key</span><span class="comment"> - </span><span class="comment">key</span> <span class="comment">with</span> <span class="comment">which</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">associated</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">value</span><span class="comment"> - </span><span class="comment">value</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">associated</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">key</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">previous</span> <span class="comment">value</span> <span class="comment">associated</span> <span class="comment">with</span> <span class="comment">key</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">null</span> <span class="comment">if</span> <span class="comment">there</span> <span class="comment">was</span> <span class="comment">no</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">key</span><span class="comment">.</span><span class="comment"> (</span><span class="comment">A</span> <span class="comment">null</span> <span class="comment">return</span> <span class="comment">can</span> <span class="comment">also</span> <span class="comment">indicate</span> <span class="comment">that</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">previously</span> <span class="comment">associated</span> <span class="comment">null</span> <span class="comment">with</span> <span class="comment">key</span><span class="comment">, </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">implementation</span> <span class="comment">supports</span> <span class="comment">null</span> <span class="comment">values</span><span class="comment">.</span><span class="comment">)</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">specified</span> <span class="comment">key</span> <span class="comment">or</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">keys</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> V <span class="ST3">put</span>(K key, V value) {
<span class="literal">if</span> (key == <span class="literal">null</span> || value == <span class="literal">null</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">parm(s) null</span><span class="string">&quot;</span>);
}
<span class="comment">//&gt;if contains the key, replace the key&#39;s value </span>
EntryASDV&lt;K, V&gt; entry = getEntryForKey(key);
<span class="literal">if</span> (entry != <span class="literal">null</span>) {
V oldValue = entry.<span class="ST1">value</span>;
entry.<span class="ST1">value</span> = value;
<span class="literal">return</span> oldValue;
}
<span class="comment">///&gt;&gt;hash and put in the array </span>
<span class="literal">int</span> code = <span class="literal">this</span>.hash(key.hashCode());
<span class="literal">int</span> index = hash(code);
ListASDV&lt;EntryASDV&lt;K, V&gt;&gt; list = <span class="ST1">map</span>.get(index);
EntryASDV e = <span class="literal">new</span> EntryASDV(key, value);
list.add(e);
<span class="comment">//&gt;&gt;add the key to the shared keys-set</span>
((SharedSet&lt;K&gt;) <span class="literal">this</span>.<span class="ST1">sharedKeySet</span>).addEntry(key);
((SharedSet&lt;<span class="ST2">Entry</span>&lt;K, V&gt;&gt;) <span class="literal">this</span>.<span class="ST1">sharedEntrySet</span>).addEntry(e);
<span class="comment">//&gt;&gt;get the value of this entry</span>
V v = list.get(list.size() - <span class="number">1</span>).getValue();
<span class="comment">//&gt;&gt; add value to the value collection</span>
((SharedCollection&lt;V&gt;) <span class="literal">this</span>.<span class="ST1">sharedValuesCollection</span>).addValue(v);
<span class="comment">//&gt;&gt; if reach 75% capacity double the size</span>
<span class="literal">if</span> ((<span class="literal">double</span>) <span class="literal">this</span>.size() / <span class="ST1">capacity</span> &gt;= <span class="number">0.75</span>) {
<span class="literal">this</span>.doubleTheSizeOfTheMapAndRehash();
}
<span class="comment">//&gt;&gt;return the value of Entry just added</span>
<span class="literal">return</span> v;
}
<span class="literal">int</span> <span class="ST3">hash</span>(<span class="literal">int</span> keyHashCode) {
<span class="literal">int</span> h = <span class="ST2">hashHash</span>(keyHashCode);
<span class="literal">return</span> Math.<span class="ST2">abs</span>(h % <span class="ST1">capacity</span> - <span class="number">1</span>);
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">the</span> <span class="comment">first</span> <span class="comment">entry</span> <span class="comment">with</span> <span class="comment">the</span> <span class="comment">given</span> <span class="comment">values</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">value</span><span class="comment"> - </span><span class="comment">the</span> <span class="comment">value</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">removed</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">removed</span><span class="comment">, </span><span class="comment">false</span> <span class="comment">otherwise</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">is</span> <span class="comment">null</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">boolean</span> <span class="ST3">removeFirstValue</span>(Object value) {
Iterator&lt;V&gt; iterator = <span class="ST1">sharedValuesCollection</span>.iterator();
<span class="literal">while</span> (iterator.hasNext()) {
Object o = iterator.next();
<span class="literal">if</span> (o.equals(value)) {
iterator.remove();
<span class="literal">return</span> <span class="literal">true</span>;
}
}
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Ensure</span> <span class="comment">hash</span> <span class="comment">code</span> <span class="comment">is</span> <span class="comment">evenly</span> <span class="comment">distributed</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">h</span><span class="comment"> - </span><span class="comment">hash</span> <span class="comment">code</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">evenly</span> <span class="comment">distributed</span> <span class="comment">hash</span> <span class="comment">code</span>
<span class="comment">*/</span>
<span class="literal">private</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST6">hashHash</span>(<span class="literal">int</span> h) {
h ^= (h &gt;&gt;&gt; <span class="number">20</span>) ^ (h &gt;&gt;&gt; <span class="number">12</span>);
<span class="literal">return</span> h ^ (h &gt;&gt;&gt; <span class="number">7</span>) ^ (h &gt;&gt;&gt; <span class="number">4</span>);
}
<span class="comment">// class cast helper method</span>
<span class="literal">public</span> <span class="literal">static</span> &lt;T&gt; T <span class="ST6">cast</span>(Object o, Class&lt;T&gt; clazz) {
<span class="literal">return</span> clazz.isInstance(o) ? clazz.cast(o) : <span class="literal">null</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">the</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">a</span> <span class="comment">key</span> <span class="comment">from</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">if</span> <span class="comment">it</span> <span class="comment">is</span> <span class="comment">present</span><span class="comment"> (</span><span class="comment">optional</span> <span class="comment">operation</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">More</span> <span class="comment">formally</span><span class="comment">, </span><span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">contains</span> <span class="comment">a</span> <span class="comment">mapping</span> <span class="comment">from</span> <span class="comment">key</span> <span class="comment">k</span> <span class="comment">to</span> <span class="comment">value</span> <span class="comment">v</span> <span class="comment">such</span> <span class="comment">that</span><span class="comment"> (</span><span class="comment">key</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> ? </span><span class="comment">k</span><span class="comment">==</span><span class="comment">null</span><span class="comment"> : </span><span class="comment">key</span><span class="comment">.</span><span class="comment">equals</span><span class="comment">(</span><span class="comment">k</span><span class="comment">)), </span><span class="comment">that</span> <span class="comment">mapping</span> <span class="comment">is</span> <span class="comment">removed</span><span class="comment">.</span><span class="comment"> (</span><span class="comment">The</span> <span class="comment">map</span> <span class="comment">can</span> <span class="comment">contain</span> <span class="comment">at</span> <span class="comment">most</span> <span class="comment">one</span> <span class="comment">such</span> <span class="comment">mapping</span><span class="comment">.</span><span class="comment">)</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">key</span><span class="comment"> - </span><span class="comment">key</span> <span class="comment">whose</span> <span class="comment">mapping</span> <span class="comment">is</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">removed</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">map</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">previous</span> <span class="comment">value</span> <span class="comment">associated</span> <span class="comment">with</span> <span class="comment">key</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">null</span> <span class="comment">if</span> <span class="comment">there</span> <span class="comment">was</span> <span class="comment">no</span> <span class="comment">mapping</span> <span class="comment">for</span> <span class="comment">key</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">key</span> <span class="comment">is</span> <span class="comment">null</span> <span class="comment">and</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">keys</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> V <span class="ST3">remove</span>(Object key) {
String dashes = <span class="string">&quot;</span><span class="string">---------</span><span class="string">&quot;</span>;
<span class="literal">try</span> {
System.<span class="ST5">out</span>.println(dashes + <span class="string">&quot;</span><span class="string"> remove(</span><span class="string">&quot;</span> + key.toString() + <span class="string">&quot;</span><span class="string">) </span><span class="string">&quot;</span> + dashes);
} <span class="literal">catch</span> (NullPointerException ex) {
System.<span class="ST5">out</span>.println(ex);
System.<span class="ST5">out</span>.println(dashes + <span class="string">&quot;</span><span class="string"> remove(</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">null</span><span class="string">&quot;</span> + <span class="string">&quot;</span><span class="string">) </span><span class="string">&quot;</span> + dashes);
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">null parameter</span><span class="string">&quot;</span>);
}
K currentKey = <span class="literal">null</span>;
V currentValue = <span class="literal">null</span>;
EntryASDV&lt;K, V&gt; currentEntry = <span class="literal">null</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">map</span>.size(); ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; <span class="ST1">map</span>.get(i).size(); ++j) {
currentEntry = <span class="ST1">map</span>.get(i).get(j);
currentKey = currentEntry.<span class="ST1">key</span>;
currentValue = currentEntry.<span class="ST1">value</span>;
<span class="literal">if</span> (currentKey.equals(key) || currentValue.equals(key)) {
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">key: </span><span class="string">&quot;</span> + currentKey + <span class="string">&quot;</span><span class="string">; value: </span><span class="string">&quot;</span> + currentValue);
<span class="comment">// remove the entry from the map</span>
<span class="ST1">map</span>.remove(i);
<span class="comment">// remove the key from the shared key set</span>
<span class="comment">// duplicate the set so we can iterate through it and not throw a ConcurrentModificationException...</span>
Object[] iterateArray = <span class="ST1">sharedKeySet</span>.toArray();
<span class="literal">for</span> (Object o : iterateArray) {
<span class="literal">if</span> (o.equals(currentKey)) {
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">removing </span><span class="string">&quot;</span> + o);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">remove successful? </span><span class="string">&quot;</span> + <span class="ST1">sharedKeySet</span>.remove(currentKey));
<span class="literal">break</span>; <span class="comment">// we only want to delete the first one</span>
}
}
<span class="comment">// remove the value from the shared key map</span>
<span class="comment">// duplicate again...</span>
iterateArray = <span class="ST1">sharedEntrySet</span>.toArray();
<span class="literal">for</span> (Object o : iterateArray) {
<span class="literal">if</span> (o.equals(currentEntry)) {
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">removing </span><span class="string">&quot;</span> + o);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">remove successful? </span><span class="string">&quot;</span> + <span class="ST1">sharedEntrySet</span>.remove(currentEntry));
<span class="literal">break</span>; <span class="comment">// we only want to delete the first one</span>
}
}
<span class="comment">// Finally, remove the value from sharedValuesCollection</span>
<span class="comment">// duplicate again...</span>
iterateArray = <span class="ST1">sharedValuesCollection</span>.toArray();
<span class="literal">for</span> (Object o : iterateArray) {
<span class="comment">//System.out.println(o);</span>
<span class="literal">if</span> (o.equals(currentValue)) {
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">removing </span><span class="string">&quot;</span> + o);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">remove successful? </span><span class="string">&quot;</span> + <span class="ST1">sharedValuesCollection</span>.remove(o));
<span class="literal">break</span>; <span class="comment">// we only want to delete the first one</span>
}
}
<span class="comment">//for(Object o: sharedValuesCollection.toArray()) System.out.println(o); </span>
<span class="literal">return</span> currentValue;
}
}
}
<span class="literal">return</span> <span class="literal">null</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Copies</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">mappings</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">map</span> <span class="comment">to</span> <span class="comment">this</span> <span class="comment">map</span><span class="comment"> (</span><span class="comment">optional</span> <span class="comment">operation</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">effect</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">call</span> <span class="comment">is</span> <span class="comment">equivalent</span> <span class="comment">to</span> <span class="comment">that</span> <span class="comment">of</span> <span class="comment">calling</span> <span class="comment">put</span><span class="comment">(</span><span class="comment">k</span><span class="comment">, </span><span class="comment">v</span><span class="comment">) </span><span class="comment">on</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">once</span> <span class="comment">for</span> <span class="comment">each</span> <span class="comment">mapping</span> <span class="comment">from</span> <span class="comment">key</span> <span class="comment">k</span> <span class="comment">to</span> <span class="comment">value</span> <span class="comment">v</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">map</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">behavior</span> <span class="comment">of</span> <span class="comment">this</span> <span class="comment">operation</span> <span class="comment">is</span> <span class="comment">undefined</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">map</span> <span class="comment">is</span> <span class="comment">modified</span> <span class="comment">while</span> <span class="comment">the</span> <span class="comment">operation</span> <span class="comment">is</span> <span class="comment">in</span> <span class="comment">progress</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST4">m</span><span class="comment"> - </span><span class="comment">mappings</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">stored</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span>
<span class="comment"> * </span><span class="comment">@throws</span> <span class="comment">NullPointerException</span><span class="comment"> - </span><span class="comment">if</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">map</span> <span class="comment">is</span> <span class="comment">null</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">if</span> <span class="comment">this</span> <span class="comment">map</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">permit</span> <span class="comment">null</span> <span class="comment">keys</span> <span class="comment">or</span> <span class="comment">values</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">map</span> <span class="comment">contains</span> <span class="comment">null</span> <span class="comment">keys</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">putAll</span>(Map&lt;? <span class="literal">extends</span> K, ? <span class="literal">extends</span> V&gt; m) {
<span class="literal">if</span> (m == <span class="literal">null</span>) {
<span class="literal">throw</span> <span class="literal">new</span> NullPointerException(<span class="string">&quot;</span><span class="string">null parameter</span><span class="string">&quot;</span>);
}
BiConsumer consumeEachEntry = <span class="literal">new</span> BiConsumer&lt;K, V&gt;() {
MapASDV&lt;K, V&gt; <span class="ST1">mapForConsumer</span> = MapASDV.<span class="literal">this</span>;
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">accept</span>(K k, V v) {
<span class="ST1">mapForConsumer</span>.put(k, v);
}
};
m.forEach(consumeEachEntry);
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">mappings</span> <span class="comment">from</span> <span class="comment">this</span> <span class="comment">map</span><span class="comment"> (</span><span class="comment">optional</span> <span class="comment">operation</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">map</span> <span class="comment">will</span> <span class="comment">be</span> <span class="comment">empty</span> <span class="comment">after</span> <span class="comment">this</span> <span class="comment">call</span> <span class="comment">returns</span><span class="comment">.</span> <span class="comment">Any</span> <span class="comment">shared</span> <span class="comment">sets</span> <span class="comment">are</span> <span class="comment">also</span> <span class="comment">cleared</span><span class="comment">.</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">clear</span>() {
<span class="comment">// Clear everything out by redefining all internal values, and let Java&#39;s GC take care of the rest</span>
<span class="ST1">map</span> = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="ST1">sharedKeySet</span> = <span class="literal">new</span> SharedSet&lt;K&gt;();
<span class="ST1">sharedEntrySet</span> = <span class="literal">new</span> SharedSet&lt;<span class="ST2">Entry</span>&lt;K, V&gt;&gt;();
<span class="ST1">sharedValuesCollection</span> = <span class="literal">new</span> SharedCollection&lt;V&gt;();
<span class="comment">//capacity = 4;</span>
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">capacity</span>; ++i) {
<span class="ST1">map</span>.add(<span class="literal">new</span> ListASDV&lt;EntryASDV&lt;K, V&gt;&gt;());
}
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">a</span> <span class="comment">Set</span> <span class="comment">view</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">keys</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">set</span> <span class="comment">is</span> <span class="comment">backed</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">map</span><span class="comment">, </span><span class="comment">so</span> <span class="comment">changes</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">are</span> <span class="comment">reflected</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">set</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">vice</span><span class="comment">-</span><span class="comment">versa</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">is</span> <span class="comment">modified</span> <span class="comment">while</span> <span class="comment">an</span> <span class="comment">iteration</span> <span class="comment">over</span> <span class="comment">the</span> <span class="comment">set</span> <span class="comment">is</span> <span class="comment">in</span> <span class="comment">progress</span><span class="comment"> (</span><span class="comment">except</span> <span class="comment">through</span> <span class="comment">the</span> <span class="comment">iterator</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">own</span> <span class="comment">remove</span> <span class="comment">operation</span><span class="comment">), </span><span class="comment">the</span> <span class="comment">results</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">iteration</span> <span class="comment">are</span> <span class="comment">undefined</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">set</span> <span class="comment">supports</span> <span class="comment">element</span> <span class="comment">removal</span><span class="comment">, </span><span class="comment">which</span> <span class="comment">removes</span> <span class="comment">the</span> <span class="comment">corresponding</span> <span class="comment">mapping</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">map</span><span class="comment">, </span><span class="comment">via</span> <span class="comment">the</span> <span class="comment">Iterator</span><span class="comment">.</span><span class="comment">remove</span><span class="comment">, </span><span class="comment">Set</span><span class="comment">.</span><span class="comment">remove</span><span class="comment">, </span><span class="comment">removeAll</span><span class="comment">, </span><span class="comment">retainAll</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">clear</span> <span class="comment">operations</span><span class="comment">.</span> <span class="comment">It</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">support</span> <span class="comment">the</span> <span class="comment">add</span> <span class="comment">or</span> <span class="comment">addAll</span> <span class="comment">operations</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">a</span> <span class="comment">set</span> <span class="comment">view</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">keys</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> Set&lt;K&gt; <span class="ST3">keySet</span>() {
<span class="literal">return</span> <span class="literal">this</span>.<span class="ST1">sharedKeySet</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">a</span> <span class="comment">Collection</span> <span class="comment">view</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">values</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">backed</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">map</span><span class="comment">, </span><span class="comment">so</span> <span class="comment">changes</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">are</span> <span class="comment">reflected</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">collection</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">vice</span><span class="comment">-</span><span class="comment">versa</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">is</span> <span class="comment">modified</span> <span class="comment">while</span> <span class="comment">an</span> <span class="comment">iteration</span> <span class="comment">over</span> <span class="comment">the</span> <span class="comment">collection</span> <span class="comment">is</span> <span class="comment">in</span> <span class="comment">progress</span><span class="comment"> (</span><span class="comment">except</span> <span class="comment">through</span> <span class="comment">the</span> <span class="comment">iterator</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">own</span> <span class="comment">remove</span> <span class="comment">operation</span><span class="comment">), </span><span class="comment">the</span> <span class="comment">results</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">iteration</span> <span class="comment">are</span> <span class="comment">undefined</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">collection</span> <span class="comment">supports</span> <span class="comment">element</span> <span class="comment">removal</span><span class="comment">, </span><span class="comment">which</span> <span class="comment">removes</span> <span class="comment">the</span> <span class="comment">corresponding</span> <span class="comment">mapping</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">map</span><span class="comment">, </span><span class="comment">via</span> <span class="comment">the</span> <span class="comment">Iterator</span><span class="comment">.</span><span class="comment">remove</span><span class="comment">, </span><span class="comment">Collection</span><span class="comment">.</span><span class="comment">remove</span><span class="comment">, </span><span class="comment">removeAll</span><span class="comment">, </span><span class="comment">retainAll</span> <span class="comment">and</span> <span class="comment">clear</span> <span class="comment">operations</span><span class="comment">.</span> <span class="comment">It</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">support</span> <span class="comment">the</span> <span class="comment">add</span> <span class="comment">or</span> <span class="comment">addAll</span> <span class="comment">operations</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> Collection&lt;V&gt; <span class="ST3">values</span>() {
<span class="literal">return</span> <span class="ST1">sharedValuesCollection</span>;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">a</span> <span class="comment">Set</span> <span class="comment">view</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">mappings</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">set</span> <span class="comment">is</span> <span class="comment">backed</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">map</span><span class="comment">, </span><span class="comment">so</span> <span class="comment">changes</span> <span class="comment">to</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">are</span> <span class="comment">reflected</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">set</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">vice</span><span class="comment">-</span><span class="comment">versa</span><span class="comment">.</span> <span class="comment">If</span> <span class="comment">the</span> <span class="comment">map</span> <span class="comment">is</span> <span class="comment">modified</span> <span class="comment">while</span> <span class="comment">an</span> <span class="comment">iteration</span> <span class="comment">over</span> <span class="comment">the</span> <span class="comment">set</span> <span class="comment">is</span> <span class="comment">in</span> <span class="comment">progress</span><span class="comment"> (</span><span class="comment">except</span> <span class="comment">through</span> <span class="comment">the</span> <span class="comment">iterator</span><span class="comment">&#39;</span><span class="comment">s</span> <span class="comment">own</span> <span class="comment">remove</span> <span class="comment">operation</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">through</span> <span class="comment">the</span> <span class="comment">setValue</span> <span class="comment">operation</span> <span class="comment">on</span> <span class="comment">a</span> <span class="comment">map</span> <span class="comment">entry</span> <span class="comment">returned</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">iterator</span><span class="comment">) </span><span class="comment">the</span> <span class="comment">results</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">iteration</span> <span class="comment">are</span> <span class="comment">undefined</span><span class="comment">.</span> <span class="comment">The</span> <span class="comment">set</span> <span class="comment">supports</span> <span class="comment">element</span> <span class="comment">removal</span><span class="comment">, </span><span class="comment">which</span> <span class="comment">removes</span> <span class="comment">the</span> <span class="comment">corresponding</span> <span class="comment">mapping</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">map</span><span class="comment">, </span><span class="comment">via</span> <span class="comment">the</span> <span class="comment">Iterator</span><span class="comment">.</span><span class="comment">remove</span><span class="comment">, </span><span class="comment">Set</span><span class="comment">.</span><span class="comment">remove</span><span class="comment">, </span><span class="comment">removeAll</span><span class="comment">, </span><span class="comment">retainAll</span> <span class="comment">and</span> <span class="comment">clear</span> <span class="comment">operations</span><span class="comment">.</span> <span class="comment">It</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">support</span> <span class="comment">the</span> <span class="comment">add</span> <span class="comment">or</span> <span class="comment">addAll</span> <span class="comment">operations</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">a</span> <span class="comment">set</span> <span class="comment">view</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">mappings</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">this</span> <span class="comment">map</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> Set&lt;<span class="ST2">Entry</span>&lt;K, V&gt;&gt; <span class="ST3">entrySet</span>() {
<span class="literal">return</span> <span class="literal">this</span>.<span class="ST1">sharedEntrySet</span>;
}
@Override
<span class="literal">public</span> String <span class="ST3">toString</span>() {
String s = <span class="string">&quot;</span><span class="string">[ </span><span class="string">&quot;</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; <span class="ST1">map</span>.size(); ++i) {
s += <span class="ST1">map</span>.get(i).toString() + <span class="string">&quot;</span><span class="literal">\n</span><span class="string">&quot;</span>;
}
s += <span class="string">&quot;</span><span class="string"> ]</span><span class="string">&quot;</span>;
<span class="literal">return</span> s;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Created</span> <span class="comment">a</span> <span class="comment">deep</span> <span class="comment">copy</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">MapASDV</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">deep</span> <span class="comment">copy</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">map</span>
<span class="comment">*/</span>
@Override
<span class="literal">public</span> Object <span class="ST3">clone</span>() {
<span class="comment">/*</span>
<span class="comment"> MapASDV&lt;K, V&gt; clonedMap = new MapASDV&lt;K, V&gt;();</span>
<span class="comment"> //clonedMap.putAll(this);</span>
<span class="comment"> for (ListASDV&lt; EntryASDV&lt;K, V&gt;&gt; list : this.map)</span>
<span class="comment"> {</span>
<span class="comment"> ListASDV&lt; EntryASDV&lt;K, V&gt;&gt; l = (ListASDV&lt; EntryASDV&lt;K, V&gt;&gt;) list.clone();</span>
<span class="comment"> clonedMap.map.add(l); </span>
<span class="comment"> }</span>
<span class="comment"> return clonedMap;</span>
<span class="comment"> */</span>
MapASDV&lt;K, V&gt; clonedMap = <span class="literal">new</span> MapASDV&lt;K, V&gt;();
clonedMap.putAll(<span class="literal">t</span><span class="literal">his</span>);
<span class="literal">return</span> clonedMap;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST6">main</span>(String[] args) {
MapASDV&lt;String, Integer&gt; map = <span class="literal">new</span> MapASDV&lt;&gt;();
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">---------------------------testing put(K, V)</span><span class="string">&quot;</span>);
map.put(<span class="string">&quot;</span><span class="string">ann</span><span class="string">&quot;</span>, <span class="number">2</span><span class="number">0</span>);
map.put(<span class="string">&quot;</span><span class="string">coco</span><span class="string">&quot;</span>, <span class="number">2</span><span class="number">5</span>);
System.<span class="ST5">out</span>.println(map);
MapASDV&lt;String, Integer&gt; clonedMap = (MapASDV&lt;String, Integer&gt;) map.clone();
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing double-the-size-and-rehash by reaching 0.75 load factor with another put(K, V)</span><span class="string">&quot;</span>);
map.put(<span class="string">&quot;</span><span class="string">Jonathan</span><span class="string">&quot;</span>, <span class="number">3</span><span class="number">0</span>);
System.<span class="ST5">out</span>.println(map);
map.put(<span class="string">&quot;</span><span class="string">Jonhathan</span><span class="string">&quot;</span>, <span class="number">4</span><span class="number">5</span>);
System.<span class="ST5">out</span>.println(map);
map.put(<span class="string">&quot;</span><span class="string">Alexander</span><span class="string">&quot;</span>, <span class="number">3</span><span class="number">3</span>);
System.<span class="ST5">out</span>.println(map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing putAll(Map&lt;K,V&gt;)</span><span class="string">&quot;</span>);
Map&lt;String, Integer&gt; anotherJavaMap = <span class="literal">new</span> HashMap();
anotherJavaMap.put(<span class="string">&quot;</span><span class="string">lion king</span><span class="string">&quot;</span>, <span class="number">4</span><span class="number">5</span>);
anotherJavaMap.put(<span class="string">&quot;</span><span class="string">HYENA</span><span class="string">&quot;</span>, <span class="number">6</span>);
map.putAll(anotherJavaMap);
System.<span class="ST5">out</span>.println(map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing containsKey</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(map.containsKey(<span class="string">&quot;</span><span class="string">Alexander</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(map.containsKey(<span class="string">&quot;</span><span class="string">alexander</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing containsValue</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(map.containsValue(<span class="number">3</span><span class="number">3</span>));
System.<span class="ST5">out</span>.println(map.containsValue(<span class="number">3</span><span class="number">4</span>));
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing getEntryForKey</span><span class="string">&quot;</span>);
<span class="ST2">Entry</span>&lt;String, Integer&gt; e = map.getEntryForKey(<span class="string">&quot;</span><span class="string">Alexander</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(map.getEntryForKey(<span class="string">&quot;</span><span class="string">Alexander</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(map.getEntryForKey(<span class="string">&quot;</span><span class="string">Alex</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing get</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(map.get(<span class="string">&quot;</span><span class="string">Alexander</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(map.get(<span class="string">&quot;</span><span class="string">Alex</span><span class="string">&quot;</span>));
<span class="literal">try</span> {
map.get(<span class="literal">n</span><span class="literal">ull</span>);
} <span class="literal">catch</span> (NullPointerException ex) {
System.<span class="ST5">out</span>.println(ex.getMessage());
}
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing getIndexForKey</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(map.getIndexForKey(<span class="string">&quot;</span><span class="string">Alexander</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(map.getIndexForKey(<span class="string">&quot;</span><span class="string">Alex</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing isEmpty</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(map.isEmpty());
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing size</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(map.size());
System.<span class="ST5">out</span>.println(map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing entrySet()</span><span class="string">&quot;</span>);
Set&lt;<span class="ST2">Entry</span>&lt;String, Integer&gt;&gt; entries = map.entrySet();
System.<span class="ST5">out</span>.println(entries);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing keySet()</span><span class="string">&quot;</span>);
Set&lt;String&gt; keys = map.keySet();
System.<span class="ST5">out</span>.println(keys);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing values()</span><span class="string">&quot;</span>);
Collection&lt;Integer&gt; values = map.values();
System.<span class="ST5">out</span>.println(values);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing remove( K) coco 25</span><span class="string">&quot;</span>);
map.remove(<span class="string">&quot;</span><span class="string">coco</span><span class="string">&quot;</span>);
values = map.values();
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">map: </span><span class="string">&quot;</span> + map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">entries: </span><span class="string">&quot;</span> + entries);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">keys: </span><span class="string">&quot;</span> + keys);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">values: </span><span class="string">&quot;</span> + values);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing Entry-Collection remove </span><span class="string">&quot;</span>);
entries.remove(e);
values = map.values();
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">map: </span><span class="string">&quot;</span> + map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">entries: </span><span class="string">&quot;</span> + entries);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">keys: </span><span class="string">&quot;</span> + keys);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">values: </span><span class="string">&quot;</span> + values);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing Set Keys remove </span><span class="string">&quot;</span>);
keys.remove(<span class="string">&quot;</span><span class="string">ann</span><span class="string">&quot;</span>);
values = map.values();
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">map: </span><span class="string">&quot;</span> + map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">entries: </span><span class="string">&quot;</span> + entries);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">keys: </span><span class="string">&quot;</span> + keys);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">values: </span><span class="string">&quot;</span> + values);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing Set Values remove </span><span class="string">&quot;</span>);
values.remove(<span class="number">4</span><span class="number">5</span>);
values = map.values();
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">map: </span><span class="string">&quot;</span> + map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">entries: </span><span class="string">&quot;</span> + entries);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">keys: </span><span class="string">&quot;</span> + keys);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">values: </span><span class="string">&quot;</span> + values);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing clear </span><span class="string">&quot;</span>);
map.clear();
values = map.values();
entries = map.entrySet();
keys = map.keySet();
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">map: </span><span class="string">&quot;</span> + map);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">entries: </span><span class="string">&quot;</span> + entries);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">keys: </span><span class="string">&quot;</span> + keys);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">values: </span><span class="string">&quot;</span> + values);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing add of sets and collections </span><span class="string">&quot;</span>);
<span class="literal">try</span> {
keys.add(<span class="string">&quot;</span><span class="string">a</span><span class="string">&quot;</span>);
} <span class="literal">catch</span> (Exception ex) {
System.<span class="ST5">out</span>.println(ex.getMessage());
}
<span class="literal">try</span> {
values.add(<span class="number">3</span><span class="number">3</span>);
} <span class="literal">catch</span> (Exception ex) {
System.<span class="ST5">out</span>.println(ex.getMessage());
}
<span class="literal">try</span> {
entries.add(e);
} <span class="literal">catch</span> (Exception ex) {
System.<span class="ST5">out</span>.println(ex.getMessage());
}
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="literal">\n</span><span class="string">---------------------------testing clone</span><span class="string">&quot;</span>);
System.<span class="ST5">out</span>.println(clonedMap);
System.<span class="ST5">out</span>.println(<span class="string">&quot;</span><span class="string">---------------------------testing put(K, V) AGAIN</span><span class="string">&quot;</span>);
map.put(<span class="string">&quot;</span><span class="string">Nicholas</span><span class="string">&quot;</span>, <span class="number">1</span><span class="number">00</span>);
map.put(<span class="string">&quot;</span><span class="string">a</span><span class="string">&quot;</span>, <span class="number">2</span><span class="number">00</span>);
map.put(<span class="string">&quot;</span><span class="string">b</span><span class="string">&quot;</span>, -<span class="number">20</span>);
System.<span class="ST5">out</span>.println(map);
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="MapASDV_CalebFontenot" default="default" basedir=".">
<description>Builds, tests, and runs the project MapASDV_CalebFontenot.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="MapASDV_CalebFontenot-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,8 @@
build.xml.data.CRC32=4f17a7fa
build.xml.script.CRC32=638d3506
build.xml.stylesheet.CRC32=f85dc8f2@1.109.0.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=4f17a7fa
nbproject/build-impl.xml.script.CRC32=0d9268a8
nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.109.0.48

View File

@@ -0,0 +1,97 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
application.title=MapASDV_CalebFontenot
application.vendor=caleb
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.modulepath=\
${run.modulepath}
debug.test.classpath=\
${run.test.classpath}
debug.test.modulepath=\
${run.test.modulepath}
# Files in build.classes.dir which should be excluded from distribution jar
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/MapASDV_CalebFontenot.jar
dist.javadoc.dir=${dist.dir}/javadoc
dist.jlink.dir=${dist.dir}/jlink
dist.jlink.output=${dist.jlink.dir}/MapASDV_CalebFontenot
endorsed.classpath=
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.modulepath=
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
javac.source=20
javac.target=20
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
javac.test.modulepath=\
${javac.modulepath}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.html5=false
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
# The jlink additional root modules to resolve
jlink.additionalmodules=
# The jlink additional command line parameters
jlink.additionalparam=
jlink.launcher=true
jlink.launcher.name=MapASDV_CalebFontenot
main.class=mapasdv_calebfontenot.MapASDV
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.modulepath=\
${javac.modulepath}
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
run.test.modulepath=\
${javac.test.modulepath}
source.encoding=UTF-8
src.dir=src
test.src.dir=test

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>MapASDV_CalebFontenot</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
</configuration>
</project>

View File

@@ -0,0 +1,782 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package mapasdv_calebfontenot;
/**
*
* @author caleb
*/
//import ListASDV;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;//needed in putAll
public class MapASDV<K, V> implements Map<K, V>, Cloneable {
private int capacity = 4;
private double loadFactor = 0.75;
private ArrayList<ListASDV<EntryASDV<K, V>>> map = new ArrayList<>();
private Set<K> sharedKeySet = new SharedSet<>();
private Set<Entry<K, V>> sharedEntrySet = new SharedSet<>();
private Collection<V> sharedValuesCollection = new SharedCollection<>();
private class SharedCollection<V> extends ArrayList<V> {
public boolean addValue(V v) {
return this.add(v);
}
@Override
public boolean remove(Object o) {
//> The parameter is key not entry if we are here
//>>remove value) and key) from the map
return MapASDV.this.removeFirstValue(o);
//>>remove key from shared values set
//return super.remove(o);
}
/**
* Removes one value from the collection. This method is meant to be called from out class. The overridden remove(Object o) of this inner class, calls the remove of the outer class(MapASDV), and the remove of the outer class, calls remove(V v, boolean callFromOuterClass) instead of remove(Object o) to avoid Stack Overflow when remover of inner calls remove of outer and vice versa.
*
* @param o - the key
* @param callFromOuterClass - dummy variable.
* @return true if the key was removed from the Set
*/
public boolean remove(V v, boolean callFromOuterClass) {
//remove key from shared keys set
boolean b = super.remove(v);
return b;
}
@Override
public Object clone() {
return super.clone();
}
@Override
public void clear() {
super.clear();
}
}
private class SharedSet<E> extends HashSet<E> {
@Override
public boolean add(E e) {
return super.add(e);
}
@Override
public boolean removeAll(Collection<?> c) {
return super.removeAll(c);
}
/**
* Adds an EntryASDV to the set. It is private and cannot be used by the user of the Set. It is used from the MapASDV to add EntriesASDV to the SharedSet. Without this method we wouldn't be able to create a Set of keys or a Set of entries to give to the user via methods keySet() and entrySet() of this Map
*
* @param e EntryASDV
* @return true if the entry was added false otherwise
*/
private boolean addEntry(E e) {
return super.add(e);
}
@Override
public boolean remove(Object o) {
//>if parameter oo is EntryASDV call auxiliary method removeEntry
if (o instanceof EntryASDV) {
return removeEntry((EntryASDV) o);
}
//> The parameter is key not entry if we are here
//>>remove key from the map
MapASDV.this.remove(o);
//>>remove key from shared keys set
return super.remove(o);
}
/**
* Removes the entry for the shared set
*
* @param entry the entry to be removed
* @return true if the entry was removed, false otherwise
*/
private boolean removeEntry(EntryASDV<K, V> entry) {
MapASDV.this.remove(entry.getKey());
return super.remove(entry);
}
/**
* Removes the key from the set. This method is meant to be called from out class. The overridden remove(Object o) of this inner class calls the remove of the out class, and the remove of the outer class calls remove(K o, boolean callFromOuterClass) instead of remove(Object o) to avoid Stack Overflow when remover of inner calls remove of outer and vice versa.
*
* @param o - the key
* @param callFromOuterClass - dummy variable.
* @return true if the key was removed from the Set
*/
public boolean remove(E o, boolean callFromOuterClass) {
//remove key from shared keys set
return super.remove(o);
}
@Override
public Object clone() {
return super.clone();
}
@Override
public void clear() {
super.clear();
}
}
public MapASDV() {
for (int i = 0; i < capacity; ++i) {
map.add(new ListASDV<EntryASDV<K, V>>());
}
}
/**
* Double the size of the Map and rehashes the entries of the Map
*/
private void doubleTheSizeOfTheMapAndRehash() {
capacity *= 2;
//>create a new arrayList of ListsASDV
ArrayList<ListASDV<EntryASDV<K, V>>> newMap = new ArrayList<ListASDV<EntryASDV<K, V>>>();
//>Add at every enetry of the arrayList a new ASDVList
for (int i = 0; i < capacity; ++i) {
newMap.add(new ListASDV<EntryASDV<K, V>>());
}
//>for the size of the OLD arrayList
for (int i = 0; i < map.size(); ++i)//array list
{
//>> get The ASDVlist at i
ListASDV<EntryASDV<K, V>> list = map.get(i);
//>>for the size() of the ASDVlist
for (int j = 0; j < list.size(); ++j) {
///>>>hash and put in the the new array
int index = hash(list.get(j).getKey().hashCode());
newMap.get(index).add(list.get(j));
}
}
map = newMap;
}
class EntryASDV<K, V> implements Entry<K, V>, Comparable<K> {
K key;
V value;
public EntryASDV(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
V oldValue = this.value;
this.value = value;
return oldValue;
}
@Override
public String toString() {
return "EntryASDV{" + "key=" + key + ", value=" + value + '}';
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final EntryASDV<?, ?> other = (EntryASDV<?, ?>) obj;
if (!Objects.equals(this.key, other.key)) {
return false;
}
return true;
}
/**
*
* @param o
* @return throws IllegalArgumentException if parameter class is not K
*/
@Override
public int compareTo(K o) {
if (getClass() != o.getClass()) {
throw new IllegalArgumentException("ellegal parameter " + o);
}
return ((Comparable) key).compareTo(o);
}
}
@Override
public int size() {
return this.keySet().size();
}
@Override
public boolean isEmpty() {
if (map.size() == 0) {
return true;
}
return false;
}
// done
@Override
public boolean containsKey(Object key) {
String dashes = "---------";
System.out.println(dashes + " containsKey(" + key.toString() + ") " + dashes);
for (int i = 0; i < map.size(); ++i) {
for (int j = 0; j < map.get(i).size(); ++j) {
if (map.get(i).get(j).key.equals(key)) {
return true;
}
}
}
return false;
}
/**
* Return an entry of the map
*
* @param key the key of the entry to be returned
* @return the entry, or null if the key is not in the map
*/
private EntryASDV<K, V> getEntryForKey(Object key) {
for (ListASDV<EntryASDV<K, V>> list : map) {
Iterator<EntryASDV<K, V>> it = list.iterator();
while (it.hasNext()) {
EntryASDV<K, V> entry = it.next();
if (key.equals(entry.getKey())) {
return entry;
}
}
}
return null;
}
/**
* Returns the index of the given key
*
* @param key a key of the map
* @return the index of a key in the map or -1, if the key is not in the map
*/
private int getIndexForKey(Object key) {
for (int i = 0; i < map.size(); ++i) {
Iterator<EntryASDV<K, V>> it = map.get(i).iterator();
while (it.hasNext()) {
EntryASDV<K, V> entry = it.next();
if (key.equals(entry.getKey())) {
return i;
}
}
}
return -1;
}
/**
* Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the Map interface.
*
* Parameters: value - value whose presence in this map is to be tested Returns: true if this map maps one or more keys to the specified value Throws: ClassCastException - if the value is of an inappropriate type for this map (optional) NullPointerException - if the specified value is null and this map does not permit null values (optional)
*
* @param value - value whose presence in this map is to be tested
* @return true if this map maps one or more keys to the specified value
* @throws NullPointerException - if the specified value is null and this map does not permit null values (optional)
*/
//done
@Override
public boolean containsValue(Object value) {
String dashes = "---------";
System.out.println(dashes + " containsValue(" + value.toString() + ") " + dashes);
for (int i = 0; i < map.size(); ++i) {
for (int j = 0; j < map.get(i).size(); ++j) {
if (map.get(i).get(j).value.equals(value)) {
return true;
}
}
}
return false;
}
/**
* Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
*
* More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)
*
* If this map permits null values, then a return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.
*
* @param key - the key whose associated value is to be returned
* @return the value to which the specified key is mapped, or null if this map contains no mapping for the key
* @throws NullPointerException - if the specified key is null and this map does not permit null keys (optional)
*/
//done
@Override
public V get(Object key) {
String dashes = "---------";
try {
System.out.println(dashes + " get(" + key.toString() + ") " + dashes);
} catch (NullPointerException ex) {
//System.out.println(ex);
System.out.println(dashes + " get(" + "null" + ") " + dashes);
throw new NullPointerException("null parameter");
}
Object currentKey = null;
for (int i = 0; i < map.size(); ++i) {
for (int j = 0; j < map.get(i).size(); ++j) {
currentKey = map.get(i).get(j).key;
if (currentKey.equals(key)) {
return map.get(i).get(j).value;
}
}
}
return null;
}
/**
* Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)
*
* @param key - key with which the specified value is to be associated
* @param value - value to be associated with the specified key
* @return the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)
* @throws NullPointerException - if specified key or value is null and this map does not permit null keys
*/
@Override
public V put(K key, V value) {
if (key == null || value == null) {
throw new NullPointerException("parm(s) null");
}
//>if contains the key, replace the key's value
EntryASDV<K, V> entry = getEntryForKey(key);
if (entry != null) {
V oldValue = entry.value;
entry.value = value;
return oldValue;
}
///>>hash and put in the array
int code = this.hash(key.hashCode());
int index = hash(code);
ListASDV<EntryASDV<K, V>> list = map.get(index);
EntryASDV e = new EntryASDV(key, value);
list.add(e);
//>>add the key to the shared keys-set
((SharedSet<K>) this.sharedKeySet).addEntry(key);
((SharedSet<Entry<K, V>>) this.sharedEntrySet).addEntry(e);
//>>get the value of this entry
V v = list.get(list.size() - 1).getValue();
//>> add value to the value collection
((SharedCollection<V>) this.sharedValuesCollection).addValue(v);
//>> if reach 75% capacity double the size
if ((double) this.size() / capacity >= 0.75) {
this.doubleTheSizeOfTheMapAndRehash();
}
//>>return the value of Entry just added
return v;
}
int hash(int keyHashCode) {
int h = hashHash(keyHashCode);
return Math.abs(h % capacity - 1);
}
/**
* Removes the first entry with the given values.
*
* @param value - the value to be removed
* @return true if removed, false otherwise
* @throws NullPointerException if the value is null
*/
private boolean removeFirstValue(Object value) {
Iterator<V> iterator = sharedValuesCollection.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
if (o.equals(value)) {
iterator.remove();
return true;
}
}
return false;
}
/**
* Ensure hash code is evenly distributed
*
* @param h - hash code
* @return evenly distributed hash code
*/
private static int hashHash(int h) {
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
// class cast helper method
public static <T> T cast(Object o, Class<T> clazz) {
return clazz.isInstance(o) ? clazz.cast(o) : null;
}
/**
* Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)
*
* @param key - key whose mapping is to be removed from the map
* @return the previous value associated with key, or null if there was no mapping for key.
* @throws NullPointerException - if the specified key is null and this map does not permit null keys
*/
@Override
public V remove(Object key) {
String dashes = "---------";
try {
System.out.println(dashes + " remove(" + key.toString() + ") " + dashes);
} catch (NullPointerException ex) {
System.out.println(ex);
System.out.println(dashes + " remove(" + "null" + ") " + dashes);
throw new NullPointerException("null parameter");
}
K currentKey = null;
V currentValue = null;
EntryASDV<K, V> currentEntry = null;
for (int i = 0; i < map.size(); ++i) {
for (int j = 0; j < map.get(i).size(); ++j) {
currentEntry = map.get(i).get(j);
currentKey = currentEntry.key;
currentValue = currentEntry.value;
if (currentKey.equals(key) || currentValue.equals(key)) {
System.out.println("key: " + currentKey + "; value: " + currentValue);
// remove the entry from the map
map.remove(i);
// remove the key from the shared key set
// duplicate the set so we can iterate through it and not throw a ConcurrentModificationException...
Object[] iterateArray = sharedKeySet.toArray();
for (Object o : iterateArray) {
if (o.equals(currentKey)) {
System.out.println("removing " + o);
System.out.println("remove successful? " + sharedKeySet.remove(currentKey));
break; // we only want to delete the first one
}
}
// remove the value from the shared key map
// duplicate again...
iterateArray = sharedEntrySet.toArray();
for (Object o : iterateArray) {
if (o.equals(currentEntry)) {
System.out.println("removing " + o);
System.out.println("remove successful? " + sharedEntrySet.remove(currentEntry));
break; // we only want to delete the first one
}
}
// Finally, remove the value from sharedValuesCollection
// duplicate again...
iterateArray = sharedValuesCollection.toArray();
for (Object o : iterateArray) {
//System.out.println(o);
if (o.equals(currentValue)) {
System.out.println("removing " + o);
System.out.println("remove successful? " + sharedValuesCollection.remove(o));
break; // we only want to delete the first one
}
}
//for(Object o: sharedValuesCollection.toArray()) System.out.println(o);
return currentValue;
}
}
}
return null;
}
/**
* Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
*
* @param m - mappings to be stored in this map
* @throws NullPointerException - if the specified map is null, or if this map does not permit null keys or values, and the specified map contains null keys
*/
@Override
public void putAll(Map<? extends K, ? extends V> m) {
if (m == null) {
throw new NullPointerException("null parameter");
}
BiConsumer consumeEachEntry = new BiConsumer<K, V>() {
MapASDV<K, V> mapForConsumer = MapASDV.this;
@Override
public void accept(K k, V v) {
mapForConsumer.put(k, v);
}
};
m.forEach(consumeEachEntry);
}
/**
* Removes all of the mappings from this map (optional operation). The map will be empty after this call returns. Any shared sets are also cleared.
*/
@Override
public void clear() {
// Clear everything out by redefining all internal values, and let Java's GC take care of the rest
map = new ArrayList<>();
sharedKeySet = new SharedSet<K>();
sharedEntrySet = new SharedSet<Entry<K, V>>();
sharedValuesCollection = new SharedCollection<V>();
//capacity = 4;
for (int i = 0; i < capacity; ++i) {
map.add(new ListASDV<EntryASDV<K, V>>());
}
}
/**
* Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.
*
* @return a set view of the keys contained in this map
*/
@Override
public Set<K> keySet() {
return this.sharedKeySet;
}
/**
* Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
*
* @return
*/
@Override
public Collection<V> values() {
return sharedValuesCollection;
}
/**
* Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
*
*
* @return a set view of the mappings contained in this map
*/
@Override
public Set<Entry<K, V>> entrySet() {
return this.sharedEntrySet;
}
@Override
public String toString() {
String s = "[ ";
for (int i = 0; i < map.size(); ++i) {
s += map.get(i).toString() + "\n";
}
s += " ]";
return s;
}
/**
* Created a deep copy of the MapASDV
*
* @return the deep copy of the map
*/
@Override
public Object clone() {
/*
MapASDV<K, V> clonedMap = new MapASDV<K, V>();
//clonedMap.putAll(this);
for (ListASDV< EntryASDV<K, V>> list : this.map)
{
ListASDV< EntryASDV<K, V>> l = (ListASDV< EntryASDV<K, V>>) list.clone();
clonedMap.map.add(l);
}
return clonedMap;
*/
MapASDV<K, V> clonedMap = new MapASDV<K, V>();
clonedMap.putAll(this);
return clonedMap;
}
public static void main(String[] args) {
MapASDV<String, Integer> map = new MapASDV<>();
System.out.println("---------------------------testing put(K, V)");
map.put("ann", 20);
map.put("coco", 25);
System.out.println(map);
MapASDV<String, Integer> clonedMap = (MapASDV<String, Integer>) map.clone();
System.out.println("\n\n---------------------------testing double-the-size-and-rehash by reaching 0.75 load factor with another put(K, V)");
map.put("Jonathan", 30);
System.out.println(map);
map.put("Jonhathan", 45);
System.out.println(map);
map.put("Alexander", 33);
System.out.println(map);
System.out.println("\n\n---------------------------testing putAll(Map<K,V>)");
Map<String, Integer> anotherJavaMap = new HashMap();
anotherJavaMap.put("lion king", 45);
anotherJavaMap.put("HYENA", 6);
map.putAll(anotherJavaMap);
System.out.println(map);
System.out.println("\n\n---------------------------testing containsKey");
System.out.println(map.containsKey("Alexander"));
System.out.println(map.containsKey("alexander"));
System.out.println("\n\n---------------------------testing containsValue");
System.out.println(map.containsValue(33));
System.out.println(map.containsValue(34));
System.out.println("\n\n---------------------------testing getEntryForKey");
Entry<String, Integer> e = map.getEntryForKey("Alexander");
System.out.println(map.getEntryForKey("Alexander"));
System.out.println(map.getEntryForKey("Alex"));
System.out.println("\n\n---------------------------testing get");
System.out.println(map.get("Alexander"));
System.out.println(map.get("Alex"));
try {
map.get(null);
} catch (NullPointerException ex) {
System.out.println(ex.getMessage());
}
System.out.println("\n\n---------------------------testing getIndexForKey");
System.out.println(map.getIndexForKey("Alexander"));
System.out.println(map.getIndexForKey("Alex"));
System.out.println("\n\n---------------------------testing isEmpty");
System.out.println(map.isEmpty());
System.out.println("\n\n---------------------------testing size");
System.out.println(map.size());
System.out.println(map);
System.out.println("\n\n---------------------------testing entrySet()");
Set<Entry<String, Integer>> entries = map.entrySet();
System.out.println(entries);
System.out.println("\n\n---------------------------testing keySet()");
Set<String> keys = map.keySet();
System.out.println(keys);
System.out.println("\n\n---------------------------testing values()");
Collection<Integer> values = map.values();
System.out.println(values);
System.out.println("\n\n---------------------------testing remove( K) coco 25");
map.remove("coco");
values = map.values();
System.out.println("map: " + map);
System.out.println("entries: " + entries);
System.out.println("keys: " + keys);
System.out.println("values: " + values);
System.out.println("\n\n---------------------------testing Entry-Collection remove ");
entries.remove(e);
values = map.values();
System.out.println("map: " + map);
System.out.println("entries: " + entries);
System.out.println("keys: " + keys);
System.out.println("values: " + values);
System.out.println("\n\n---------------------------testing Set Keys remove ");
keys.remove("ann");
values = map.values();
System.out.println("map: " + map);
System.out.println("entries: " + entries);
System.out.println("keys: " + keys);
System.out.println("values: " + values);
System.out.println("\n\n---------------------------testing Set Values remove ");
values.remove(45);
values = map.values();
System.out.println("map: " + map);
System.out.println("entries: " + entries);
System.out.println("keys: " + keys);
System.out.println("values: " + values);
System.out.println("\n\n---------------------------testing clear ");
map.clear();
values = map.values();
entries = map.entrySet();
keys = map.keySet();
System.out.println("map: " + map);
System.out.println("entries: " + entries);
System.out.println("keys: " + keys);
System.out.println("values: " + values);
System.out.println("\n\n---------------------------testing add of sets and collections ");
try {
keys.add("a");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
values.add(33);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
entries.add(e);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
System.out.println("\n\n---------------------------testing clone");
System.out.println(clonedMap);
System.out.println("---------------------------testing put(K, V) AGAIN");
map.put("Nicholas", 100);
map.put("a", 200);
map.put("b", -20);
System.out.println(map);
}
}

View File

@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:3.1.0:exec</goal>
</goals>
<properties>
<exec.vmArgs></exec.vmArgs>
<exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args>
<exec.appArgs></exec.appArgs>
<exec.mainClass>${packageClassName}</exec.mainClass>
<exec.executable>java</exec.executable>
</properties>
</action>
<action>
<actionName>debug</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:3.1.0:exec</goal>
</goals>
<properties>
<exec.vmArgs>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}</exec.vmArgs>
<exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args>
<exec.appArgs></exec.appArgs>
<exec.mainClass>${packageClassName}</exec.mainClass>
<exec.executable>java</exec.executable>
<jpda.listen>true</jpda.listen>
</properties>
</action>
<action>
<actionName>profile</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:3.1.0:exec</goal>
</goals>
<properties>
<exec.vmArgs></exec.vmArgs>
<exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args>
<exec.mainClass>${packageClassName}</exec.mainClass>
<exec.executable>java</exec.executable>
<exec.appArgs></exec.appArgs>
</properties>
</action>
</actions>

View File

@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>Multithreading_CalebFontenot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx</artifactId>
<version>20</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>20</version>
<classifier>linux</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>20</version>
<classifier>linux</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.multithreading_calebfontenot.Multithreading_CalebFontenot</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,86 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.multithreading_calebfontenot;
/**
*
* @author caleb
*/
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class FlashText extends Application
{
private String text = "";
@Override // Override the start method in the Application class
public void start(Stage primaryStage)
{
StackPane pane = new StackPane();
Label lblText = new Label("Programming is fun");
pane.getChildren().add(lblText);
Thread t1 = new Thread(
new Runnable()
{
public void run()
{
try
{
while (true)
{
if (lblText.getText().trim().length() == 0)
text = "Welcome";
else
text = "";
Platform.runLater(
new Runnable()
{
@Override
public void run()
{
lblText.setText(text);
}
});
/*
Thread t2 =new Thread( new Runnable() {
@Override
public void run()
{
lblText.setText(text);
}
});
*/
Thread.sleep(300);
}
}
catch (InterruptedException ex)
{
}
}
});
t1.start();
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 200, 50);
primaryStage.setTitle("FlashText"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args)
{
launch(args);
}
}

View File

@@ -0,0 +1,59 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.multithreading_calebfontenot;
/**
*
* @author caleb
*/
public class PrintChar implements Runnable{
private char charToPrint;
private int times;
public PrintChar(char charToPrint, int times)
{
this.charToPrint = charToPrint;
this.times = times;
}
@Override
public void run()
{
for (int i = 0; i < times; ++i) {
System.out.println(charToPrint);
}
}
public static void main(String[] args)
{
for (int i = 0; i < 1000; ++i) {
System.out.println("lmao");
}
}
}
class PrintNum implements Runnable {
private int lastNum;
public PrintNum(int lastNum)
{
this.lastNum = lastNum;
}
public void run()
{
for (int i = 1; i <= lastNum; ++i) {
System.out.print(" " + i);
Thread.yield();
if (i % 10 == 0) {
System.out.println();
}
}
}
}

View File

@@ -0,0 +1,15 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.multithreading_calebfontenot;
/**
*
* @author caleb
*/
public class RunFlash {
public static void main(String[] args) {
FlashText.main(args);
}
}

View File

@@ -0,0 +1,29 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.multithreading_calebfontenot;
/**
*
* @author caleb
*/
public class Threads {
public static void main(String[] args)
{
// Create tasks
Runnable printA = new PrintChar('a', 100);
Runnable printB = new PrintChar('b', 100);
Runnable print100 = new PrintNum(100);
// Create threads
Thread thread1 = new Thread(printA);
Thread thread2 = new Thread(printB);
Thread thread3 = new Thread(print100);
// Start threads
thread1.start();
thread2.start();
thread3.start();
}
}

View File

@@ -0,0 +1,20 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.caleb.multithreading_calebfontenot;
import java.io.IOException;
/**
*
* @author caleb
*/
public class forkbomb
{
public static void main(String[] args) throws IOException
{
Runtime.getRuntime().exec(new String[]{"java", "-cp", System.getProperty("java.class.path"), "forkbomb"});
}
}

View File

@@ -0,0 +1,405 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>TreeASDV.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.ST2 {color: #9876aa}
.ST3 {color: #ffc66d}
.ST1 {color: #8a653b}
.comment {color: #808080}
.whitespace {color: #505050}
.ST4 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST6 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST5 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 4/Assignments/ProjectTrees_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/projecttrees_calebfontenot/TreeASDV.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> edu.slcc.asdv.caleb.projecttrees_calebfontenot;
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> java.util.Iterator;
<span class="literal">import</span> java.util.LinkedList;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">util</span><span class="comment">.</span><span class="comment">ListIterator</span><span class="comment">;</span>
<span class="literal">import</span> java.util.Queue;
<span class="literal">import</span> java.util.Stack;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST1">&lt;T&gt;</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> TreeASDV&lt;T <span class="literal">extends</span> Comparable&gt; <span class="literal">implements</span> Cloneable {
<span class="literal">private</span> Node&lt;T&gt; <span class="ST2">root</span>;
<span class="literal">class</span> Node&lt;T&gt; {
T <span class="ST2">data</span>;
Node&lt;T&gt; <span class="ST2">leftChild</span>;
Node&lt;T&gt; <span class="ST2">rightChild</span>;
}
@Override
<span class="literal">public</span> Object <span class="ST3">clone</span>()
{
System.<span class="ST4">out</span>.println(<span class="string">&quot;</span><span class="string">Cloning...</span><span class="string">&quot;</span>);
TreeASDV&lt;T&gt; clone = <span class="literal">new</span> TreeASDV&lt;T&gt;();
ArrayList&lt;T&gt; oldData = <span class="literal">this</span>.getBreadthFirstArray();
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; oldData.size(); ++i) {
<span class="comment">//System.out.println(oldData.get(i));</span>
clone.insert(oldData.get(i));
}
<span class="literal">return</span> clone;
}
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">insert</span>(T t)
{
Node&lt;T&gt; newNode = <span class="literal">new</span> Node&lt;&gt;();
newNode.<span class="ST2">data</span> = t;
<span class="literal">if</span> (<span class="ST2">root</span> == <span class="literal">null</span>) {
<span class="ST2">root</span> = newNode;
<span class="literal">return</span> <span class="literal">true</span>;
}
Node&lt;T&gt; current = <span class="ST2">root</span>;
Node&lt;T&gt; parent = <span class="literal">null</span>;
<span class="literal">while</span> (current != <span class="literal">null</span>) {
parent = current;
<span class="literal">if</span> (t.compareTo(current.<span class="ST2">data</span>) &gt;= <span class="number">0</span>) {
current = current.<span class="ST2">rightChild</span>;
} <span class="literal">else</span> {
current = current.<span class="ST2">leftChild</span>;
}
}
<span class="comment">// At this point, &#39;parent&#39; is the node where the new node should be inserted as a child</span>
<span class="literal">if</span> (t.compareTo(parent.<span class="ST2">data</span>) &gt;= <span class="number">0</span>) {
parent.<span class="ST2">rightChild</span> = newNode;
} <span class="literal">else</span> {
parent.<span class="ST2">leftChild</span> = newNode;
}
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="literal">private</span> <span class="literal">void</span> <span class="comment">inOrder</span>(Node&lt;T&gt; p)
{
<span class="literal">if</span> (p == <span class="literal">null</span>) {
<span class="literal">return</span>;
}
inOrder(p.<span class="ST2">leftChild</span>);
System.<span class="ST4">out</span>.print(p.<span class="ST2">data</span> + <span class="string">&quot;</span> <span class="string">&quot;</span>);
inOrder(p.<span class="ST2">rightChild</span>);
}
<span class="literal">public</span> Node&lt;T&gt; <span class="ST3">findNode</span>(T t)
{
Node&lt;T&gt; currentNode = <span class="ST2">root</span>;
<span class="literal">while</span> (currentNode != <span class="literal">null</span>) {
<span class="literal">if</span> (t.compareTo(currentNode.<span class="ST2">data</span>) == <span class="number">0</span>) {
<span class="literal">return</span> currentNode;
} <span class="literal">else</span> <span class="literal">if</span> (t.compareTo(currentNode.<span class="ST2">data</span>) &gt; <span class="number">0</span>) {
currentNode = currentNode.<span class="ST2">rightChild</span>;
} <span class="literal">else</span> {
currentNode = currentNode.<span class="ST2">leftChild</span>;
}
}
<span class="literal">return</span> <span class="literal">null</span>;
}
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">remove</span>(T t) {
<span class="comment">// Initialize parent and current nodes for traversal</span>
Node&lt;T&gt; parent = <span class="literal">null</span>;
Node&lt;T&gt; current = <span class="ST2">root</span>;
<span class="comment">// Search for the node to be removed</span>
<span class="literal">while</span> (current != <span class="literal">null</span> &amp;&amp; !current.<span class="ST2">data</span>.equals(t)) {
parent = current;
<span class="literal">if</span> (t.compareTo(current.<span class="ST2">data</span>) &gt; <span class="number">0</span>) {
current = current.<span class="ST2">rightChild</span>;
} <span class="literal">else</span> {
current = current.<span class="ST2">leftChild</span>;
}
}
<span class="comment">// If node not found, return false</span>
<span class="literal">if</span> (current == <span class="literal">null</span>) {
<span class="literal">return</span> <span class="literal">false</span>;
}
<span class="comment">// Case 1: Node with no children</span>
<span class="literal">if</span> (current.<span class="ST2">leftChild</span> == <span class="literal">null</span> &amp;&amp; current.<span class="ST2">rightChild</span> == <span class="literal">null</span>) {
<span class="literal">if</span> (current == <span class="ST2">root</span>) {
<span class="ST2">root</span> = <span class="literal">null</span>; <span class="comment">// Removing root node</span>
} <span class="literal">else</span> <span class="literal">if</span> (parent.<span class="ST2">leftChild</span> == current) {
parent.<span class="ST2">leftChild</span> = <span class="literal">null</span>; <span class="comment">// Removing a left child</span>
} <span class="literal">else</span> {
parent.<span class="ST2">rightChild</span> = <span class="literal">null</span>; <span class="comment">// Removing a right child</span>
}
} <span class="comment">// Case 2: Node with one child</span>
<span class="literal">else</span> <span class="literal">if</span> (current.<span class="ST2">leftChild</span> == <span class="literal">null</span> || current.<span class="ST2">rightChild</span> == <span class="literal">null</span>) {
Node&lt;T&gt; child = (current.<span class="ST2">leftChild</span> != <span class="literal">null</span>) ? current.<span class="ST2">leftChild</span> : current.<span class="ST2">rightChild</span>;
<span class="literal">if</span> (current == <span class="ST2">root</span>) {
<span class="ST2">root</span> = child; <span class="comment">// Replace root with its child</span>
} <span class="literal">else</span> <span class="literal">if</span> (parent.<span class="ST2">leftChild</span> == current) {
parent.<span class="ST2">leftChild</span> = child; <span class="comment">// Replace parent&#39;s left child with the node&#39;s child</span>
} <span class="literal">else</span> {
parent.<span class="ST2">rightChild</span> = child; <span class="comment">// Replace parent&#39;s right child with the node&#39;s child</span>
}
} <span class="comment">// Case 3: Node with two children</span>
<span class="literal">else</span> {
Node&lt;T&gt; successorParent = current;
Node&lt;T&gt; successor = current.<span class="ST2">rightChild</span>;
<span class="literal">while</span> (successor.<span class="ST2">leftChild</span> != <span class="literal">null</span>) {
successorParent = successor;
successor = successor.<span class="ST2">leftChild</span>;
}
current.<span class="ST2">data</span> = successor.<span class="ST2">data</span>; <span class="comment">// Replace data with successor&#39;s data</span>
<span class="literal">if</span> (successorParent == current) {
successorParent.<span class="ST2">rightChild</span> = successor.<span class="ST2">rightChild</span>;
} <span class="literal">else</span> {
successorParent.<span class="ST2">leftChild</span> = successor.<span class="ST2">rightChild</span>;
}
}
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="comment">// Helper method to find in-order successor of a node</span>
<span class="literal">private</span> Node&lt;T&gt; <span class="comment">getSuccessor</span>(Node&lt;T&gt; node)
{
Node&lt;T&gt; current = node.<span class="ST2">rightChild</span>;
Node&lt;T&gt; successorParent = node;
Node&lt;T&gt; successor = node;
<span class="comment">// Find the leftmost node in the right subtree (in-order successor)</span>
<span class="literal">while</span> (current != <span class="literal">null</span>) {
successorParent = successor;
successor = current;
current = current.<span class="ST2">leftChild</span>;
}
<span class="comment">// If the successor is not the right child of the node to be removed,</span>
<span class="comment">// adjust the successor&#39;s parent&#39;s leftChild reference</span>
<span class="literal">if</span> (successor != node.<span class="ST2">rightChild</span>) {
successorParent.<span class="ST2">leftChild</span> = successor.<span class="ST2">rightChild</span>;
successor.<span class="ST2">rightChild</span> = node.<span class="ST2">rightChild</span>;
}
<span class="literal">return</span> successor;
}
<span class="comment">// Inorder ListIterator</span>
<span class="literal">public</span> Iterator&lt;T&gt; <span class="ST3">listIterator</span>()
{
<span class="literal">return</span> <span class="literal">new</span> InorderIterator();
}
<span class="literal">private</span> <span class="literal">class</span> InorderIterator <span class="literal">implements</span> Iterator&lt;T&gt; {
<span class="literal">private</span> Stack&lt;Node&lt;T&gt;&gt; <span class="ST2">stack</span>;
<span class="literal">public</span> InorderIterator()
{
<span class="ST2">stack</span> = <span class="literal">new</span> Stack&lt;&gt;();
pushLeft(<span class="ST2">r</span><span class="ST2">oot</span>);
}
<span class="literal">private</span> <span class="literal">void</span> <span class="ST3">pushLeft</span>(Node&lt;T&gt; node)
{
<span class="literal">while</span> (node != <span class="literal">null</span>) {
<span class="ST2">stack</span>.push(node);
node = node.<span class="ST2">leftChild</span>;
}
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">hasNext</span>()
{
<span class="literal">return</span> !<span class="ST2">stack</span>.isEmpty();
}
@Override
<span class="literal">public</span> T <span class="ST3">next</span>()
{
<span class="literal">if</span> (!hasNext()) {
<span class="literal">throw</span> <span class="literal">new</span> java.util.NoSuchElementException();
}
Node&lt;T&gt; current = <span class="ST2">stack</span>.pop();
pushLeft(current.<span class="ST2">rightChild</span>);
<span class="literal">return</span> current.<span class="ST2">data</span>;
}
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">remove</span>()
{
<span class="literal">throw</span> <span class="literal">new</span> UnsupportedOperationException();
}
}
<span class="literal">public</span> ArrayList&lt;T&gt; <span class="ST3">getBreadthFirstArray</span>()
{
ArrayList&lt;T&gt; returnArray = <span class="literal">new</span> ArrayList&lt;T&gt;();
<span class="literal">if</span> (<span class="ST2">root</span> == <span class="literal">null</span>) {
<span class="literal">return</span> returnArray;
}
Queue&lt;Node&lt;T&gt;&gt; queue = <span class="literal">new</span> LinkedList&lt;&gt;();
queue.offer(<span class="ST2">r</span><span class="ST2">oot</span>);
<span class="literal">while</span> (!queue.isEmpty()) {
Node&lt;T&gt; current = queue.poll();
returnArray.add(current.<span class="ST2">data</span>);
<span class="literal">if</span> (current.<span class="ST2">leftChild</span> != <span class="literal">null</span>) {
queue.offer(current.<span class="ST2">leftChild</span>);
}
<span class="literal">if</span> (current.<span class="ST2">rightChild</span> != <span class="literal">null</span>) {
queue.offer(current.<span class="ST2">rightChild</span>);
}
}
<span class="literal">return</span> returnArray;
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">breadthFirstTraversal</span>()
{
<span class="literal">if</span> (<span class="ST2">root</span> == <span class="literal">null</span>) {
<span class="literal">return</span>;
}
Queue&lt;Node&lt;T&gt;&gt; queue = <span class="literal">new</span> LinkedList&lt;&gt;();
queue.offer(<span class="ST2">r</span><span class="ST2">oot</span>);
<span class="literal">while</span> (!queue.isEmpty()) {
Node&lt;T&gt; current = queue.poll();
System.<span class="ST4">out</span>.print(current.<span class="ST2">data</span> + <span class="string">&quot;</span> <span class="string">&quot;</span>);
<span class="literal">if</span> (current.<span class="ST2">leftChild</span> != <span class="literal">null</span>) {
queue.offer(current.<span class="ST2">leftChild</span>);
}
<span class="literal">if</span> (current.<span class="ST2">rightChild</span> != <span class="literal">null</span>) {
queue.offer(current.<span class="ST2">rightChild</span>);
}
}
}
@Override
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">equals</span>(Object obj) {
<span class="literal">return</span> <span class="literal">this</span>.getBreadthFirstArray().equals(((TreeASDV) obj).getBreadthFirstArray());
}
<span class="literal">public</span> <span class="literal">int</span> <span class="ST3">height</span>()
{
<span class="literal">return</span> calculateHeight(<span class="ST2">r</span><span class="ST2">oot</span>);
}
<span class="literal">private</span> <span class="literal">int</span> <span class="ST3">calculateHeight</span>(Node&lt;T&gt; node)
{
<span class="literal">if</span> (node == <span class="literal">null</span>) {
<span class="literal">return</span> <span class="number">0</span>;
}
<span class="literal">int</span> leftHeight = calculateHeight(node.<span class="ST2">leftChild</span>);
<span class="literal">int</span> rightHeight = calculateHeight(node.<span class="ST2">rightChild</span>);
<span class="literal">return</span> <span class="number">1</span> + Math.<span class="ST5">max</span>(leftHeight, rightHeight);
}
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">isFullBST</span>()
{
<span class="literal">int</span> height = height();
<span class="literal">int</span> nodeCount = countNodes(<span class="ST2">r</span><span class="ST2">oot</span>);
<span class="literal">return</span> nodeCount == (<span class="number">1</span> &lt;&lt; height) - <span class="number">1</span>; <span class="comment">// Formula for full binary tree</span>
}
<span class="literal">private</span> <span class="literal">int</span> <span class="ST3">countNodes</span>(Node&lt;T&gt; node)
{
<span class="literal">if</span> (node == <span class="literal">null</span>) {
<span class="literal">return</span> <span class="number">0</span>;
}
<span class="literal">return</span> <span class="number">1</span> + countNodes(node.<span class="ST2">leftChild</span>) + countNodes(node.<span class="ST2">rightChild</span>);
}
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">inOrder</span>()
{
<span class="literal">if</span> (<span class="ST2">root</span> == <span class="literal">null</span>) {
<span class="literal">return</span>;
}
Stack&lt;Node&lt;T&gt;&gt; stack = <span class="literal">new</span> Stack&lt;&gt;();
Node&lt;T&gt; current = <span class="ST2">root</span>;
<span class="literal">while</span> (current != <span class="literal">null</span> || !stack.isEmpty()) {
<span class="literal">while</span> (current != <span class="literal">null</span>) {
stack.push(current);
current = current.<span class="ST2">leftChild</span>;
}
current = stack.pop();
System.<span class="ST4">out</span>.print(current.<span class="ST2">data</span> + <span class="string">&quot;</span> <span class="string">&quot;</span>);
current = current.<span class="ST2">rightChild</span>;
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST6">main</span>(String[] args) <span class="literal">throws</span> CloneNotSupportedException
{
TreeASDV&lt;Integer&gt; tree = <span class="literal">new</span> TreeASDV&lt;&gt;();
<span class="comment">// Insert some elements into the tree</span>
tree.insert(<span class="number">5</span>);
tree.insert(<span class="number">3</span>);
tree.insert(<span class="number">7</span>);
tree.insert(<span class="number">2</span>);
tree.insert(<span class="number">4</span>);
tree.insert(<span class="number">6</span>);
tree.insert(<span class="number">8</span>);
<span class="comment">// Test breadth-first traversal</span>
System.<span class="ST4">out</span>.println(<span class="string">&quot;</span><span class="string">Breadth-First Traversal:</span><span class="string">&quot;</span>);
tree.breadthFirstTraversal();
System.<span class="ST4">out</span>.println();
<span class="comment">// Test height calculation</span>
System.<span class="ST4">out</span>.println(<span class="string">&quot;</span><span class="string">Height of the tree: </span><span class="string">&quot;</span> + tree.height());
<span class="comment">// Test if the tree is a full binary tree</span>
System.<span class="ST4">out</span>.println(<span class="string">&quot;</span><span class="string">Is the tree a full binary tree? </span><span class="string">&quot;</span> + tree.isFullBST());
<span class="comment">// Test inorder traversal without recursion</span>
System.<span class="ST4">out</span>.println(<span class="string">&quot;</span><span class="string">Inorder Traversal without Recursion:</span><span class="string">&quot;</span>);
tree.inOrder();
System.<span class="ST4">out</span>.println();
System.<span class="ST4">out</span>.println(<span class="string">&quot;</span><span class="string">array list from breadth traversal</span><span class="string">&quot;</span>);
System.<span class="ST4">out</span>.println(tree.getBreadthFirstArray());
System.<span class="ST4">out</span>.println(<span class="string">&quot;</span><span class="string">Cloned Tree:</span><span class="string">&quot;</span>);
TreeASDV&lt;Integer&gt; clonedTree = (TreeASDV&lt;Integer&gt;) tree.clone();
clonedTree.breadthFirstTraversal();
System.<span class="ST4">out</span>.println();
System.<span class="ST4">out</span>.println(<span class="string">&quot;</span><span class="string">Does the original tree and the clone tree equal? </span><span class="string">&quot;</span> + (tree.equals(clonedTree) ? <span class="string">&quot;</span><span class="string">yes</span><span class="string">&quot;</span> : <span class="string">&quot;</span><span class="string">no</span><span class="string">&quot;</span>) );
tree.insert(<span class="number">3</span><span class="number">000000</span>);
System.<span class="ST4">out</span>.println(<span class="string">&quot;</span><span class="string">Does the original tree and the clone tree equal? </span><span class="string">&quot;</span> + (tree.equals(clonedTree) ? <span class="string">&quot;</span><span class="string">yes</span><span class="string">&quot;</span> : <span class="string">&quot;</span><span class="string">no</span><span class="string">&quot;</span>) );
tree.remove(<span class="number">5</span>);
tree.breadthFirstTraversal();
}
}
</pre></body>
</html>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.jdkPlatform>Graal_JDK_20</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>ProjectTrees_CalebFontenot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.projecttrees_calebfontenot.ProjectTrees_CalebFontenot</exec.mainClass>
</properties>
</project>

Some files were not shown because too many files have changed in this diff Show More