abc
abc

Reputation: 512

How do I do a drop-down list without duplicate elements?

I have a list of objects and multiple drop-downs. I want to display the data from the list. But my problem is that I didn't want to have something like this:

For example: I don't want to have in drop-down a file name for two times :

file1
file1
file2
file3
file3

I want to have only: file1, file2, file3

This is my controller where I put the list in model

@GetMapping("/filter/functionality/misramessages")
public String filterFunctionality(Model model) {
    model.addAttribute("misraMessages", misraMessagesService.findAllMisraMessagesFromDb());
    return "functionality";
}

And after in html I do this:

 <!-- Filter File Name-->
        <div class="form-group row">
            <label for="fileName" class="col-sm-2 col-form-label">File Name</label>
            <div class="col-sm-10">
                <select class="form-control"  name="file_name" id="fileName">
                    <option th:value="0" text="Please Select"></option>
                    <option  th:each = "misra : ${misraMessages}"
                             th:value="${misra.fileName}"
                             th:text="${misra.fileName}">
                    </option>
                </select>
            </div>
        </div>

<!-- Filter Message Number-->
        <div class="form-group row">
            <label for="messageNumber" class="col-sm-2 col-form-label">Message Number</label>
            <div class="col-sm-10">
                <select class="form-control"  name="message_number" id="messageNumber">
                    <option th:value="0" text="Please Select"></option>
                    <option  th:each = "misra : ${misraMessages}"
                             th:value="${misra.messageNumber}"
                             th:text="${misra.messageNumber}">
                    </option>
                </select>
            </div>
        </div>
        .......
        .......

Upvotes: 0

Views: 2008

Answers (4)

Sabir Khan
Sabir Khan

Reputation: 10142

Its always better not to bring in duplicates to memory if that is causing issues and duplicate removal is needed.

So instead of using repository method - misraMessagesRepository.findAll(); , you need to write a new method in that repository which returns DISTINCT results and use that method in DAO.

@Query("SELECT DISTINCT * FROM MESSAGES_TABLE" , nativeQuery = true)
List<MisraMessages> findDistinctMessages();

You haven't shown your repository so I used dummy table name. Show your repository code and Entity details if still confused and need better answer.

Upvotes: 1

Ashoka
Ashoka

Reputation: 26

Convert your List into Set. It will remove your duplicates then put into model.

Upvotes: 0

Jayesh Choudhary
Jayesh Choudhary

Reputation: 808

Well I expect that misraMessagesService.findAllMisraMessagesFromDb() is returning list of String. So you can do something like this if you don't want to change your findAllMisraMessagesFromDb() method:

List<String> messages = misraMessagesService.findAllMisraMessagesFromDb();
Set<String> uniqueMsgs = new HashSet<String>(messages);

or you can also create a new method in misraMessagesRepository:

@Query("SELECT DISTINCT name FROM MisraMessages")
public List<MisraMessages> findDistinctMisraMessagesFromDb();

either of the two will work.

Upvotes: 3

Alien
Alien

Reputation: 15878

Never go for JAVA way to do the same because it is simply double processing time.

I would avoid processing in Java level code as it is easily possible by query level.

Just use Select distinct keyword and it works like a charm..!!

Keep it simple.

Upvotes: 2

Related Questions