#Hey how would one initialize an arraylist of treesets.
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
unit testing. Here's the code I wrote:
ArrayList<TreeSet<Integer>> arrayListOfTreeSets = new ArrayList<>();
TreeSet<Integer> treeSet1 = new TreeSet<>();
treeSet1.add(1);
treeSet1.add(2);
arrayListOfTreeSets.add(treeSet1);
TreeSet<Integer> treeSet2 = new TreeSet<>();
treeSet2.add(3);
treeSet2.add(4);
arrayListOfTreeSets.add(treeSet2);
// ... add more TreeSets if needed
This code initializes an ArrayList of TreeSets and adds some example TreeSets to it. You can add as many TreeSets as needed by repeating the process.
However, if you want a more concise way to initialize the ArrayList with multiple TreeSets, you can use Java 9's collection factory methods:
ArrayList<TreeSet<Integer>> arrayListOfTreeSets = new ArrayList<>(List.of(
new TreeSet<>(List.of(1, 2)),
new TreeSet<>(List.of(3, 4))
// ... add more TreeSets if needed
));
This way, you can directly initialize the ArrayList with multiple TreeSets, reducing the amount of code and making it more readable.
don't quite get ur question tbh
it feels almost like u forgot to post something
so I'm writing JUnit tests for a method that takes a hashmap of a word and an integer, then converts it to an arrayList of treeset containing words assiociated with it's index.
so {{"x", 1}, {"hello", 5}, {"world", "5"}, {"hi", 2}} => [{}, {"x"}, {"hi"}, [], [], {"hello", "world"}]
I think my code is good, but I'm also trying to figure out how to write a Junit test for this
by using a bunch of examples like the one u just shared
okay well I'm trying to figure out how to initialize an arraylist of treesets
why would u want that?
so I can write Junit tests for it?
what does one have to do with the other
here I think showing is better than telling
besides, chatgpt showed u above how to create a list of sets
yeah only issue is JUnit is failing me, but also not failing me?
it keeps giving me this reasoning:
org.opentest4j.AssertionFailedError: expected: java.util.ArrayList@6b0c2d26<[[], [x], [hi], [], [], [hello, world]]> but was: java.util.ArrayList@3d3fcdb0<[[], [x], [hi], [], [], [hello, world]]>
Expected :[[], [x], [hi], [], [], [hello, world]]
Actual :[[], [x], [hi], [], [], [hello, world]]
Detected code, here are some useful tools:
the print is showing toString
which doesn't necessarily line up with equals
share ur code please
okay heres my method I'm writing:
static ArrayList<TreeSet<String>> mapValues(HashMap<String, Integer> m) {
// Create an ArrayList to store the TreeSet values
ArrayList<TreeSet<String>> resultList = new ArrayList<>();
// Find the maximum length value in the HashMap
int maxLength = 0;
for (int length : m.values()) {
maxLength = Math.max(maxLength, length);
}
// Initialize the ArrayList with empty TreeSets up to the maximum length
for (int i = 0; i <= maxLength; i++) {
resultList.add(new TreeSet<>());
}
// Populate the TreeSets with corresponding strings
for (Map.Entry<String, Integer> entry : m.entrySet()) {
String key = entry.getKey();
int length = entry.getValue();
resultList.get(length).add(key);
}
return resultList;
}
Detected code, here are some useful tools:
then my Junit code:
@org.junit.jupiter.api.Test
void mapValues() {
HashMap<String, Integer> hm1 = new HashMap<String, Integer>();
hm1.put("x", 1);
hm1.put("hello", 5);
hm1.put("world", 5);
hm1.put("hi", 2);
//treeset within an arraylist. really? are y'all trying to give me a brain aneurysm
ArrayList<TreeSet<String>> whyGod = new ArrayList<>(List.of(
new TreeSet<>(List.of("")),
new TreeSet<>(List.of("x")),
new TreeSet<>(List.of("hi")),
new TreeSet<>(List.of("")),
new TreeSet<>(List.of("")),
new TreeSet<>(List.of("hello", "world"))
));
assertEquals(whyGod, Lab06.mapValues(hm1));
}
Detected code, here are some useful tools:
what was the yellow warning on the assert saying?
org.opentest4j.AssertionFailedError: expected: java.util.ArrayList@6b0c2d26<[[], [x], [hi], [], [], [hello, world]]> but was: java.util.ArrayList@3d3fcdb0<[[], [x], [hi], [], [], [hello, world]]>
no i mean in ur screenshot
the method was underlined yellow
with a warning if u mouse hover
remove all ur List.of("").
that creates sets that are not empty but contain a single empty text
which isnt visible in the toString
I'm dumb
that fixed it.
Welp. Thank you
