Creating books with Sets

Joined
Sep 26, 2022
Messages
1
Reaction score
1
Hey I wanted to ask if you could help me with my code.
I have to create five books and save them in a HashSet. Also they are should be sorted by their price, titel and autor (I shoud use a TreeSet for it and implement Comparable in the class) Also I should give the books sorted by their titel, autor and price out (another TreeSet + Comparator).
The main problem is that I'm not sure how to use Comparator so it would be nice if you could help me a little bit this is what I have until now.

Code:
public static String books(String autor, String titel, String preis) {

        HashSet<String> book = new HashSet<>(Arrays.asList(autor, titel, preis));


        for (int i = 0; i <= 5; i++) {
            book.add(titel);
            Comparable<String> anotherBook = new Comparable<String>() {
                @Override
                public int compareTo(String o) {
                    return 0;
                }
            };
            TreeSet<String> output = new TreeSet<>(Arrays.asList(preis, titel, autor));
        }
        System.out.println(book);
        return null;
    }
 
Joined
Sep 28, 2022
Messages
2
Reaction score
1
Hello Haruka

i guess Book should be a type but from what i see from your code that
HashSet<String> books = new HashSet<>(Arrays.asList(autor, titel, preis)); this will be hashset of String not books

so you need to create the Book class first which i guess would be based on those field author title price
then change the above code to
HashSet<Book> = new HashSet<>(); // another tips better to use the most supertype you can use so
Set<Book> = new HashSet<>(); why cause that make your code work for any set object and it's subclasses
another notice this way will allow you only to call method that defined in set any method defined in hashset can not be called unless you cast it

not sure if you need to make that books as local variable or what

for (int i = 0; i <= 5; i++) {
book.add(titel); // another issue here that set does not allow duplicate so you can not add title 5 times it will be added only once

Comparable<String> anotherBook = new Comparable<String>() { // here you again use String while you should use Book
@Override
public int compareTo(String o) { // this will always make it as both element equal each other by the way String already implement comparable so you can use https://docs.oracle.com/en/java/jav.../lang/String.html#compareTo(java.lang.String)

return 0;
}
};

i will leave how to solve that for you ;) (your Book class could implement the comparable interface also ) but feel free to ask if you stuck by the way you can use lambda if you learned about it
here a tutorial for that https://dev.java/learn/lambda-expressions/ free and by oracle

TreeSet<String> output = new TreeSet<>(Arrays.asList(preis, titel, autor)); // not sure why you create another set again if you want it to be sorted set then why you used hashset earlier
}
System.out.println(book);
return null;// not sure why you make your method return String and you keep return null

hope that put some lights and help you and feel free to ask if i need to replay in better way and have a nice day :)
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,352
Latest member
SherriePet

Latest Threads

Top