设置防止重复的URL

It's possible to prevent duplicated URL on Set?

喜欢:

http://www.mywebsite.com/somethings
http://www.mywebsite.com/somethings/

URL come dynamically, so, I don't know what is the url that will come, it can come with and without bar on final. I need to add all url's on Set preventing adding the same URL. I know that bars on final make the URL different, but it's possible to prevent it on Set?

private var urlList: MutableSet<URL> = mutableSetOf()

要么

private var urlList: MutableSet<String> = mutableSetOf()

It is Kotlin, but sample in Java are welcome.

评论
  • 情侣~装
    情侣~装 回复

    好的,这是操作方法(也许)

    由于URL是最终的,因此您需要在URL上创建包装器类,并将URLWRapper的对象添加到您的集合中

    public class URLWrapper {
      private URL url;
      public URLWrapper(URL url) {
        ...
      }
      public URL getURL() {...}
      public boolean equals(Object o) {
        if (!(o instanceof URLWrapper)) return false;
        return ((URLWRapper)o).getURL().sameFile(this.url);
      }
    }
    

    您应该测试sameFile-我实际上并不知道这在您感兴趣的所有情况下都行得通。但是,如果没有,那么您需要自己实现一个类似sameFile的方法。