#Hey how would one initialize an arraylist of treesets.

1 messages · Page 1 of 1 (latest)

zenith karma
#

As the title says, I'm writing JUnit tests and I'm struggling to find a way beside using the code I made, which you know, defeats the purpose of using JUnit.

pale forumBOT
#

<@&987246399047479336> please have a look, thanks.

pale forumBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

mild sluice
#

it feels almost like u forgot to post something

zenith karma
# mild sluice don't quite get ur question tbh

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

mild sluice
#

by using a bunch of examples like the one u just shared

zenith karma
mild sluice
#

why would u want that?

zenith karma
mild sluice
#

what does one have to do with the other

zenith karma
#

here I think showing is better than telling

mild sluice
#

besides, chatgpt showed u above how to create a list of sets

zenith karma
#

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]]
pale forumBOT
mild sluice
#

the print is showing toString

#

which doesn't necessarily line up with equals

#

share ur code please

zenith karma
#

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;
    }
pale forumBOT
zenith karma
#

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));



    }
pale forumBOT
mild sluice
#

what was the yellow warning on the assert saying?

zenith karma
mild sluice
#

no i mean in ur screenshot

#

the method was underlined yellow

#

with a warning if u mouse hover

zenith karma
mild sluice
#

remove all ur List.of("").

#

that creates sets that are not empty but contain a single empty text

#

which isnt visible in the toString

zenith karma
#

I'm dumb facepalm that fixed it.

Welp. Thank you

mild sluice