Commit 49a0b7ed authored by ElaFHNW's avatar ElaFHNW
Browse files

updating Collections

parent 8f201521
package Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.nio.charset.Charset;
public class Queue1 {
// FIFO first in first out
public static void main(String[] arguments) {
Queue queue = new LinkedList<String>();
for (int i=0; i<10; i++) {
byte[] array = new byte[7]; // length is bounded by 7
new Random().nextBytes(array);
String generatedString = new String(array, Charset.forName("Windows-1252"));
queue.offer(generatedString); // add
}
System.out.println("Queue: "+ queue + "\n");
for (int i=0; i<5; i++) {
String next = (String) queue.remove();
System.out.println("removed " + next);
}
System.out.println("Queue after removing last five items: "+ queue + "\n");
queue.offer("Lastone");
System.out.println("Queue now " + queue);
}
}
package Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class Set1 {
public static void main(String[] args) {
Set <Integer> mySet, yourSet;
mySet = new HashSet<Integer> ();
yourSet = new TreeSet<Integer> ();
// add some data
mySet.add(1800000);
yourSet.add(1800000);
mySet.add(180);
yourSet.add(180);
mySet.add(16);
yourSet.add(18);
mySet.add(18);
yourSet.add(18);
mySet.add(4);
yourSet.add(4);
mySet.add(5);
yourSet.add(5);
mySet.add(8);
yourSet.add(8);
System.out.println("hashset " + mySet);
System.out.println("treeset " + yourSet);
//yourSet.addAll(mySet);
// System.out.println("after add all, UNION " + yourSet); // UNION
// a-b retain all
yourSet.removeAll(mySet);
System.out.println("after subtracting myset from yourset " + yourSet); // DIFFERENCE
//INTERSECTION
Set <Integer> setA = new HashSet<Integer>();
setA.add(1);
setA.add(3);
setA.add(5);
Set <Integer> setB = new HashSet<Integer>();
setB.add(2);
setB.add(3);
setB.add(4);
setA.retainAll(setB);
System.out.println("Intersection of A and B is " + setA); // INTERSECTION
}
}
package Collections;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class ShakespeareHashSet {
public static void main(String[] args) {
int noOfWords = 0;
File file = new File("src/shakespeare.txt");
ArrayList <String> text = new ArrayList<String> ();
Set <String> vocabulary = new HashSet<String> ();
try {
Scanner scan = new Scanner(file);
while (scan.hasNext() ) {
String line = scan.nextLine();
line = line.trim();
if (line.length()>0) { // added to skip empty lines
String [] words = line.split("\\s+");
for (int i=0; i<words.length; i++) {
words[i] = words[i].replace(".", "");
words[i] = words[i].replace(",", "");
text.add(words[i]);
vocabulary.add(words[i]);
noOfWords++;
}
}
}
}
catch (Exception e) {
System.out.println("exception " + e.getMessage());
}
System.out.println("We have " + noOfWords + " unique words " + vocabulary.size());
if (text.contains("votarist")) {
System.out.println("votarist found");
}
for (String s : vocabulary)
System.out.println("vocabulary " + s);
}
}
package Collections;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class ShakespeareTreeSet {
public static void main(String[] args) {
int noOfWords = 0;
File file = new File("src/shakespeare.txt");
ArrayList <String> text = new ArrayList<String> ();
Set <String> vocabulary = new TreeSet<String> ();
try {
Scanner scan = new Scanner(file);
while (scan.hasNext() ) {
String line = scan.nextLine();
line = line.trim();
if (line.length()>0) { // added to skip empty lines
String [] words = line.split("\\s+");
for (int i=0; i<words.length; i++) {
words[i] = words[i].replace(".", "");
words[i] = words[i].replace(",", "");
text.add(words[i]);
vocabulary.add(words[i]);
noOfWords++;
}
}
}
}
catch (Exception e) {
System.out.println("exception " + e.getMessage());
}
System.out.println("We have " + noOfWords + " unique words " + vocabulary.size());
if (text.contains("votarist")) {
System.out.println("votarist found");
}
for (String s : vocabulary)
System.out.println("vocabulary " + s);
}
}
package Collections;
import java.util.Stack;
public class Stack1 {
// LIFO last in first out
public static void main(String[] args) {
Stack <String> stringsStack = new Stack<String>();
stringsStack.push("Bach");
stringsStack.push("Wagner");
stringsStack.push("Gershwin");
System.out.println(stringsStack.pop());
System.out.println(stringsStack.peek());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment