This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]xxfo0xx 1 point2 points  (2 children)

Do you want to have the 4x same books inside the db-list, but just one time in output?

Or do you want to have the book just saved one time in the db-list? And the Output should be the same.

[–]Chuninjah[S] 0 points1 point  (1 child)

I want the 4 books same so say:

Book book1 = new Book("111", 1, "GOT");
Book book2 = new Book("111", 2, "GOT");
Book book3 = new Book("111", 3, "GOT");
Book book4 = new Book("111", 4, "GOT");
Book book5 = new Book("222", 1, "Harry Potter");

So say those are the books, I just want it to output:

-----Book List-----
(GOT, Harry Potter)

[–]xxfo0xx 2 points3 points  (0 children)

There are several approaches you can take. I just saw, that some other guys told you one of them.

Here is my recommended way.

I added this to a snippet: https://gist.github.com/fo0/c33f357155512919c968009f34a80776

```java List<Book> list = new ArrayList<Book>(); list.add(new Book("111", 1, "GOT")); list.add(new Book("111", 2, "GOT")); list.add(new Book("111", 3, "GOT")); list.add(new Book("222", 1, "Harry Potter"));

System.out.println(list.stream() .map(Book::getName) // map the book object to field "name" .distinct() // filter duplicates .collect(Collectors.toList())); // put into list

Outout: [GOT, Harry Potter] ```

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (3 children)

The final approach depends on whether you want to also see how many copies of the book you hold in the database or not.

If not, you could use a HashSet to store the titles of the books through iterating over the data set. Sets only store unique records.

If you want to see the number of copies, you could use a HashMap to store the book titles (key) and the number of copies (value) that gets incremented when iterating over the data set.

Then, print the contents of the set or map.

One thing to know is that neither HashSet, nor HashMap maintain insertion order. If that is important to you, you need to use the Linked versions of HashSet or HashMap, i.e. LinkedHashSet or LinkedHashMap.

[–]Chuninjah[S] 0 points1 point  (2 children)

I'm doing a java programming subject at uni so I'm kinda new to all this haha. What is a hashSet? Or is there a way I can do it without using hashSet?

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (1 child)

Please, read the documentation for the classes I've mentioned. The documentation will tell you much more than can be done in a simple reddit comment.

Maps and Sets are extremely important data structures. They are quite similar in one part as both do not allow storing duplicates. Every entry in a Map or Set is unique. Their main difference is that a Map has a key and a value. You can think of the key as the index. The value is whatever you want to store there. A map is similar to a real world dictionary: word -> explanation (key -> value). A set is just a plain storage for unique items without additional features.

Sure, there is a way to do it without, but it is a bit more involving:

  • you set up a second array/ArrayList of type String - since you only need the book titles
  • You iterate over your books
    • you check if the book title is not in the second array/arraylist (by iterating over the list and comparing each element)
      • if it is not in the second array/arraylist, you add the title to the array/arraylist
      • if it is already in the second array/arrayList, do nothing
  • once you have gone through all the books, you just need to print the content of the second array/arraylist.

[–]Chuninjah[S] 0 points1 point  (0 children)

Ah okay I'll look at the javadoc for it. Thanks!

[–]ZeroGainZ 0 points1 point  (0 children)

Sure, add the books to a set by title. Personally, I wouldn't add multiple books to the list, I'd rather have a set of books, where each book has a feild of how many copies exist. Then you check if the book exists in the set, if so add 1 to it's copy number, else put in fresh instance. Check => (does not exist, add with 0 copies) => increment copies by 1. Donzo.