forked from abartlett26/HW3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeProblems.java
More file actions
65 lines (51 loc) · 2.06 KB
/
TreeProblems.java
File metadata and controls
65 lines (51 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* *** Celeste Gonzalez / COMP272 002 ***
*
* This java file contains several simple tree problems that need to be
* codified. These routines must use the TreeMap and TreeSet library
* classes from the Java Collection Framework.
*
*/
import java.util.*;
public class TreeProblems {
/**
* Method different()
*
* Given two TreeSets of integers, return a TreeSet containing all elements
* that are NOT in both sets. In other words, return a TreeSet of all the
* elements that are in one set but not the other.
*/
public static Set<Integer> different(Set<Integer> setA, Set<Integer> setB) {
// INSERT CODE HERE - DO NOT FORGET TO PLACE YOUR NAME ABOVE
//
// This can be done numerous ways, but once such will only that
// *several* lines of code. Hint: create two temporary TreeSets and utilize the
// methods retainAll(), addAll(), and removeAll(). But in the end, get something to work.
Set<Integer> xset = new TreeSet<>(setB); // new treeset w/ all elemnts from setb
xset.removeAll(setA); // remove repeated elements also in setA
setA.removeAll(setB); // remove repeated elements also in setB
setA.addAll(xset); // add the remaining elements from the temporary set to setA
return setA; // return only the changed elements
}
/**
* Method removeEven()
*
* Given a treeMap with the key as an integer, and the value as a String,
* remove all <key, value> pairs where the key is even.
*/
public static void removeEven(Map<Integer, String> treeMap) {
// INSERT CODE HERE.
treeMap.entrySet().removeIf(entry -> entry.getKey() % 2 == 0);
return; //removing even keys
}
/**
* Method treesEqual()
*
* Given two treeMaps, each with the key as an integer, and the value as a String,
* return a boolean value indicating if the two trees are equal or not.
*/
public boolean treesEqual(Map<Integer, String> tree1,Map<Integer, String> tree2 ) {
// INSERT CODE HERE
return tree1.equals(tree2); // this will compare both maps in the equals method
}
} // end treeProblems class