This is a java program that runs on the Eclipse. Java Programming 2-5: Collectio
ID: 3849571 • Letter: T
Question
This is a java program that runs on the Eclipse.
Java Programming 2-5: Collections Part 2 Practice Activities Lesson objectives: Implement a HashMap Implement a stack by using a deque Define a link list Define a queue Implement a comparable interface Vocabulary: Identify the vocabulary word for each definition below. A double-ended queue; a queue that can add and remove elements to the front or back of the list. The links of a LinkedList. An interface used to define a group of objects. This includes lists and sets. Maps that link a Key to a Value and may have duplicate Keys but cannot have duplicate Values. A list of elements that is dynamically stored A list of elements with a first in first out ordering. Try It/Solve it: 1. What is the difference between a Queue and a Stack? Give an example of each. 2. Is it possible to add nodes to the beginning of a LinkedList? If so, how? What about adding a node to the end of a LinkedList? If this can be done, what method would be used? 3. What is the purpose of implementing the Comparable interface in one of our classes?Explanation / Answer
1. Diffrence Between Stack & Queue
package com.sample;
import java.util.*;
public class StackDemoTest {
public static void main(String args[]) {
// creating stack
Stack stack = new Stack();
stack.push("A");
stack.push("B");
stack.push("C");
// removing top object
System.out.println("Removed Item: "+stack.pop());
// elements after remove
System.out.println("Stack after Remove: "+stack);
}
}
package com.sample;
import java.util.*;
public class QueueDemo {
public static void main(String[] args) {
Queue queue = new LinkedList();
queue.add("element 0");
queue.add("element 1");
queue.add("element 2");
//access via Iterator
Iterator iterator = queue.iterator();
while(iterator.hasNext()){
String element = (String) iterator.next();
}
//access via new for-loop
for(Object object : queue) {
String element = (String) object;
}
}
}
2. addFirst (Element e) and addLast (Element e) method of LinkedList can be used respectively to add node in the front and end of any LinkedList. Please find the below progream
package com.sample;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String> ();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
System.out.println("LinkedList Before Addition:"+list);
list.addFirst("X");
list.addLast("Y");
System.out.println("LinkedList Before Addition:"+list);
}
}
Output:
LinkedList Before Addition:[A, B, C, D]
LinkedList Before Addition:[X, A, B, C, D, Y]
3. A class implements Comparable Interface means it can take two objects from that class and compare them. Some classes, like certain collections that keep objects in order rely on them being comparable .
4. HashMap can use to mee this requirement. In HashMap items inserted by key-value pair. After Insertion any value can be fetched by the key. Please find the below example for this
package com.sample;
import java.util.HashMap;
public class CoolectionDemo {
public static void main(String[] args) {
HashMap<String, String> hm = new HashMap<String, String> ();
hm.put("CIT", "Computer Information Technology");
hm.put("CHI", "Childcare and Early Education");
hm.put("MVS", "Motor Vehicle System");
hm.put("BTH", "Beauty Therapy");
hm.put("GDE", "Graphic Design");
System.out.println("Beayty Course Name:"+hm.get("BTH"));
}
}
Output: Beayty Course Name:Beauty Therapy
Stack Queue Stack Data Structure follows Last In First Out (LIFO) principle. Queue Data Structure follows First In First Out (FIFO) principle. In Stack any element inserted first will be removed last In Queue any element inserted first will be removed first Items are inserted and deleted from the same end Items in a queue are inserted and deleted from different ends Stack is a collection of items Queue is a ordered collection of items Stack operations are called push and pop Queue operations are called enqueue and dequeuepackage com.sample;
import java.util.*;
public class StackDemoTest {
public static void main(String args[]) {
// creating stack
Stack stack = new Stack();
stack.push("A");
stack.push("B");
stack.push("C");
// removing top object
System.out.println("Removed Item: "+stack.pop());
// elements after remove
System.out.println("Stack after Remove: "+stack);
}
}
package com.sample;
import java.util.*;
public class QueueDemo {
public static void main(String[] args) {
Queue queue = new LinkedList();
queue.add("element 0");
queue.add("element 1");
queue.add("element 2");
//access via Iterator
Iterator iterator = queue.iterator();
while(iterator.hasNext()){
String element = (String) iterator.next();
}
//access via new for-loop
for(Object object : queue) {
String element = (String) object;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.