简体   繁体   English

自定义链表的调用indexof(java)

[英]customize an indexof call for a linkedlist (java)

I'm working with a very large (custom Object) linkedlist, and I'm trying to determine if an object that I'm trying to add to the list is already in there. 我正在处理一个很大的(自定义对象)链表,并且试图确定要添加到列表中的对象是否已经存在。

The issue is that the item I am searching for is a unique object containing: A 1st String A 2nd String A unique Count # 问题是我要搜索的项目是一个唯一的对象,其中包含:第一个字符串A第二个字符串一个唯一的Count#

I'm trying to find out if there is an item in my linked list that contains the (1st String) and (2nd String), but ignore (the unique Count #). 我试图找出我的链表中是否有包含(第一个字符串)和(第二个字符串)的项目,但是忽略了(唯一的Count#)。

This can be done the dumb way (the way I tried it first) by going through each individual linkedlist item - but this takes way too long. 可以通过遍历每个单独的链表项来完成愚蠢的方式(我先尝试过的方式),但这花费的时间太长。 I'm trying to speed it up! 我正在努力加快速度! I figured using (indexOf) would help, but I don't know how I can customize what it is searching for. 我认为使用(indexOf)会有所帮助,但我不知道如何自定义其搜索内容。

Any ideas? 有任何想法吗?

indexOf() has O(n) performance as well because it progressively scans the List until it finds the element you're looking for. indexOf()也具有O(n)性能,因为它会逐步扫描List直到找到所需的元素。

Is the list sorted? 列表排序了吗? If so, you might be able to search for an element using something like quicksort. 如果是这样,您也许可以使用quicksort之类的元素来搜索元素。

If you need constant time access for random elements, I don't think a Linked List is your best bet. 如果您需要固定时间访问随机元素,那么我认为最好不要选择链接列表。

Do you NEED to use a LinkedList? 您需要使用LinkedList吗? If it's not legacy code, I would recommend either HashSet or LinkedHashMap . 如果不是传统代码,则建议使用HashSetLinkedHashMap Both will give you constant-time lookup, and if you still need insertion-order iteration, LinkedHashMap has an internal LinkedList running through the keys. 两者都将为您提供恒定时间的查找,并且如果您仍然需要插入顺序迭代,则LinkedHashMap会通过键运行内部的LinkedList

Unfortunately the "dumb way" is the most effiecient way to do so, although you could use 不幸的是,“哑巴方式”是最有效的方式,尽管您可以使用

if ( linkedList.contains(objectThatMayBeInList) ) { //do something }

The problem is that a LinkedList has a best case search of O(N) where N is the size of the list. 问题在于,LinkedList的最佳情况是搜索O(N),其中N是列表的大小。 That means that on any given search you have a worst case scenario of N computations. 这意味着在任何给定的搜索中,您都会遇到N次计算的最坏情况。 Linked lists are not the best data structure for that kind of an operation, but at the same time, it's not that bad, and it shouldn't be too slow, computers are good at doing that. 链表并不是那种操作的最佳数据结构,但是同时,它还不错,而且速度也不应该太慢,计算机可以做到这一点。 Is there more specifics you can give us as to the size of the list? 您是否可以提供更多有关列表大小的详细信息?

Basically you want to find out if object A exists in linked list L. This is the search problem, and if the list is unordered you cannot do it faster than O(n). 基本上,您想确定对象A是否存在于链接列表L中。这是搜索问题,如果列表是无序的,则您不能比O(n)快。

If you kept the list sorted (making insertion slower), you could do a binary search to see if A is in the list, which would be much faster. 如果您对列表进行排序(使插入速度变慢),则可以进行二进制搜索以查看列表中是否包含A,这样会更快。

Perhaps you could also keep a Map (HashMap or TreeMap for instance) in addition to the list, where you keep track of what stuff is in the list. 也许除了列表之外,您还可以保留一个Map(例如HashMap或TreeMap),以跟踪列表中的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM