GVKun编程网logo

Collections.sort()和通过添加到TreeSet中以获得排序后的集合之间的区别?(collection.sort方法)

38

在本文中,我们将带你了解Collections.sort在这篇文章中,我们将为您详细介绍Collections.sort的方方面面,并解答和通过添加到TreeSet中以获得排序后的集合之间的区别?常见

在本文中,我们将带你了解Collections.sort在这篇文章中,我们将为您详细介绍Collections.sort的方方面面,并解答和通过添加到TreeSet中以获得排序后的集合之间的区别?常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的@OneToMany 和 @ElementCollection 之间的区别?、@OneToMany和@ElementCollection之间的区别?、asp.net-mvc – RouteCollection.Ignore和RouteCollection.IgnoreRoute之间的区别?、Beautifulsoup:.find()和.select()之间的区别

本文目录一览:

Collections.sort()和通过添加到TreeSet中以获得排序后的集合之间的区别?(collection.sort方法)

Collections.sort()和通过添加到TreeSet中以获得排序后的集合之间的区别?(collection.sort方法)

 Set<Student> ts = new TreeSet<Student>();    for(Student s : studentInfo){         ts.add(s);    }    System.out.println(ts);

我已经在我的一个case块中编写了以上代码片段,以便对Student
Objects的集合进行排序。我的问题是:使用这种方法和使用Collections.sort();方法有什么区别?

答案1

小编典典

区别在于,a TreeSet可以使您始终对数据进行排序,而在上Collections.sort()调用时,该方法将对数据进行排序Set

的时间复杂度Collections.sort()O(n*log(n))同时TreeSetadd()的复杂性log(n)。如果您使用相同大小的数据,则TreeSet的情况下的复杂度将相同,因为您需要重复add操作n时间。

因此,您只需要决定是要一直Set订购还是在某个时候订购。如果您的代码中有某种情况下不需要排序,则不需要,TreeSet但如果始终需要对其进行排序,则应使用TreeSet

请记住 ,如果要排序,则Set必须List先从中创建一个,这可能会带来一些开销!

另一个警告: 正如其他人所提到的,TreeSet您只能为1
Comparator提供1,而您可以为提供不同ComparatorCollections.sort()。因此,这 取决于您的用法
。您应该向我们提供有关您的用例的更多信息,以便为您提供完整的答案。

@OneToMany 和 @ElementCollection 之间的区别?

@OneToMany 和 @ElementCollection 之间的区别?

@OneToMany使用 a和annotation有什么区别,@ElementCollection因为两者都适用于一对多关系?

@OneToMany和@ElementCollection之间的区别?

@OneToMany和@ElementCollection之间的区别?

如何解决@OneToMany和@ElementCollection之间的区别??

我相信@ElementCollection主要用于映射非实体(可嵌入或基本),而@OnetoMany用于映射实体。因此,使用哪种取决于您要实现的目标。

解决方法

由于使用a @OneToMany@ElementCollection批注之间都存在一对多关系,因此两者之间有什么区别?

asp.net-mvc – RouteCollection.Ignore和RouteCollection.IgnoreRoute之间的区别?

asp.net-mvc – RouteCollection.Ignore和RouteCollection.IgnoreRoute之间的区别?

RouteCollection.Ignore(url,constraints)和RouteCollection.IgnoreRoute(url,constraints)之间有什么区别?

背景

新的MVC项目在Global.asax RegisterRoutes方法中包含此IgnoreRoute调用,以跳过对ASP.NET系统其他地方处理的.axd位置的请求的路由.

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

我想添加一个额外的被忽略的路由到一个项目,我开始输出新的行.路线之后.I,Intellisense弹出.Ignore和.IgnoreRoute,听起来都一样.

根据MSDN文档,您可以看到一个是System.Web.Routing.RouteCollection类的实例方法,另一个是System.Web.Mvc.RouteCollectionExtensions的该类的扩展方法.

> RouteCollection.Ignore:“如果请求URL满足指定的约束,则定义不应该检查与路由匹配的URL模式”(MSDN docs).
> RouteCollection.IgnoreRoute:“忽略可用路由的给定列表的指定URL路由和约束列表”(MSDN docs).

两者都采用路由URL模式和一组限制在该URL模式上的路由的应用的约束.

解决方法

在 source for System.Web.Mvc.RouteCollectionExtensions on CodePlex之间,在我的本地GAC上运行一个 ILSpy,在System.Web.Routing.RouteCollection中,它似乎并没有区别,尽管他们似乎有完全独立的代码做同样的事情.

RouteCollection.IgnoreRoute(via CodePlex source)

public static void IgnoreRoute(this RouteCollection routes,string url,object constraints) {
    if (routes == null) {
        throw new ArgumentNullException("routes");
    }
    if (url == null) {
        throw new ArgumentNullException("url");
    }

    IgnoreRouteInternal route = new IgnoreRouteInternal(url) {
        Constraints = new RouteValueDictionary(constraints)
    };

    routes.Add(route);
}

RouteCollection.Ignore(通过ILSpy反编译)

public void Ignore(string url,object constraints) {
    if (url == null) {
        throw new ArgumentNullException("url");
    }
    RouteCollection.IgnoreRouteInternal item = new RouteCollection.IgnoreRouteInternal(url) {
        Constraints = new RouteValueDictionary(constraints)
    };
    base.Add(item);
}

差异

唯一真正的区别是位置明显的区别,一个是RouteCollection类本身的一个实例方法,一个是该类的一个扩展方法.考虑到实例与扩展执行之间的代码差异(例如扩展实例中的重要的空值检查),它们看起来是相同的.

在他们的核心,他们都使用完全相同的StopRoutingHandler类.两者都有自己的版本的密码IgnoreRouteInternal类,但这些版本在代码中是相同的.

private sealed class IgnoreRouteInternal : Route {
    public IgnoreRouteInternal(string url)
        : base(url,new StopRoutingHandler()) {
    }
    public override VirtualPathData GetVirtualPath(RequestContext requestContext,RouteValueDictionary routeValues) {
        return null;
    }
}

Beautifulsoup:.find()和.select()之间的区别

Beautifulsoup:.find()和.select()之间的区别

当您使用
BeautifulSoup

抓取网站的特定部分时,您可以使用

  • soup.find()soup.findAll()
  • soup.select()

.find().select()方法之间有区别吗?(例如,性能或灵活性等)还是相同?

答案1

小编典典

总结评论:

  • select 查找多个实例并返回一个列表, find 查找第一个实例,因此它们不会执行相同的操作。 select_one 将等同于 find
  • 我链接时,标签或使用几乎总是使用CSS选择 tag.classname ,如果寻找一个单一的元素没有一个类我用 找到 。本质上,它取决于用例和个人喜好。
  • 就灵活性而言,我认为您知道答案,soup.select("div[id=foo] > div > div > div[class=fee] > span > span > a")使用多个链接的 find / find_all 调用看起来很难看。
  • bs4中的css选择器唯一的问题是对它的支持非常有限, nth-of-type 是唯一实现的伪类,并且像c [sref ]的许多其他部分一样,也不支持链接属性,例如a [href] [src]。但是像 a [href = ..] *, a [href ^ =]a [href $ =] 等之类的东西我认为要好得多,find("a", href=re.compile(....))但这又是个人喜好。

为了提高性能,我们可以运行一些测试,我修改了此处答案的代码,该答案在从此处获取的800多个html文件上运行,虽然并不详尽,但应为某些选项的可读性和性能提供线索:

修改后的功能为:

from bs4 import BeautifulSoupfrom glob import iglobdef parse_find(soup):    author = soup.find("h4", class_="h12 talk-link__speaker").text    title = soup.find("h4", class_="h9 m5").text    date = soup.find("span", class_="meta__val").text.strip()    soup.find("footer",class_="footer").find_previous("data", {        "class": "talk-transcript__para__time"}).text.split(":")    soup.find_all("span",class_="talk-transcript__fragment")def parse_select(soup):    author = soup.select_one("h4.h12.talk-link__speaker").text    title = soup.select_one("h4.h9.m5").text    date = soup.select_one("span.meta__val").text.strip()    soup.select_one("footer.footer").find_previous("data", {        "class": "talk-transcript__para__time"}).text    soup.select("span.talk-transcript__fragment")def  test(patt, func):    for html in iglob(patt):        with open(html) as f:            func(BeautifulSoup(f, "lxml")

现在是时候了:

In [7]: from testing import test, parse_find, parse_selectIn [8]: timeit test("./talks/*.html",parse_find)1 loops, best of 3: 51.9 s per loopIn [9]: timeit test("./talks/*.html",parse_select)1 loops, best of 3: 32.7 s per loop

就像我说的并不详尽,但我认为我们可以肯定地说CSS选择器绝对有效。

关于Collections.sort和通过添加到TreeSet中以获得排序后的集合之间的区别?的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于@OneToMany 和 @ElementCollection 之间的区别?、@OneToMany和@ElementCollection之间的区别?、asp.net-mvc – RouteCollection.Ignore和RouteCollection.IgnoreRoute之间的区别?、Beautifulsoup:.find()和.select()之间的区别的相关信息,请在本站寻找。

本文标签: