开发者

I need to remove duplicates from an Array of items

I'm not really a coder so please have an open mind when it comes to what I call different aspects of the code. I might be dead wrong about what things are actually called...

Anyhow I (believe) I have an array of item containing lots of different data attached to each item. I then iterate over this Array putting the content into my jsp. The problem is that the array contains duplicates and I don't want duplicates in the presentation on the homepage.

I have googled like a freak and I have found out that the way to remove duplicates is to convert the array to a set, since a set can't contain duplicates. Fair enough. I have checked different pages describing how to go about it but I don't understand :(

It's probably very simple but from what I can make of it the sets contain strings and that won't do it for me as my items have a lot of data attached to them item.name, item.type etc.

The code I use is

<c:forEach items="${results.searchResults}" var="foundItem" varStatus="status">
    <c:set var="curItem" value="${f开发者_JAVA技巧oundItem.item}" />
    <%-- get item details --%>
    <xmlfeed:getItemXML item="${curItem}" />
</c:forEach>

And what I want to do is make sure that all curItem's are unique. I bet the best way to do this is to make sure that the "results.searchResults" array is made into a set and then turned back but I just don't understand how to do it.

Perhaps worth mentioning, the above code is actually already inside a tag with the following taglibs availible:

taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"

taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"

along with the custom xmlfeed tablib of course


In your result Object there is a List<Item> searchResults variable? You can change it to Set<String> searchResults; and in the setter or any other method which is setting data for searchResults variable do this.searchResults = new HashSet<String>(resultlist);

following code may not be the exact way for you but it will help to find your own way.....

public class Result {

    private List<String> result;
    private Set<String> searchResults;

    public Set<String> getSearchResults() {
        return searchResults;
    }

    public void setResult(List<String> result) {
        this.result = result;
        this.searchResults = new HashSet<String>(result);
    }
}

Servlet code,

List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("A");
list.add("B");
list.add("A");
Result result = new Result();
result.setResult(list);
request.setAttribute("result", result);

request.getRequestDispatcher("testPage.jsp").forward(request, response);

In JSP,

<c:forEach items="${result.searchResults}" var="foundItem" varStatus="status">
    <c:set var="curItem" value="${foundItem}" />
    ${curItem}<br>
</c:forEach>


    public Set<String> searchResults()
{
    List<String> list = new ArrayList<String>();
    list.add("A");list.add("B"); list.add("A");list.add("B");list.add("A");
    return new HashSet<String>(list);
}

The above code returns the unique list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜