Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to store certain id's , and check if one exists there.
Either i can use concatenated string or array/List, which of them is a better and faster way.
This is how actually data is organized :
Year 1
Month 1
Day 1
Day 2
Day 3
Month 2
Day 6
Day 2
Day 3
Year 2
Month 3
Day 1
Day 3
Day 7
Month 6
Day 6
Day 2
Day 3
I would definitely use a collection of some form. If you only care about containment, you should use a Set<String> of some kind (e.g. HashSet<String> or LinkedHashSet<String>, which will both give O(1) complexity unless you have a significant number of hash collisions) but for goodness' sake don't use a concatenated string.
Your data isn't naturally a concatenated string - it's a collection of strings. Always keep your data in the most natural representation unless you have really good evidence that some alternative form (such as a single string) will bring you a meaningful benefit. Keeping your data in a natural representation almost always leads to clearer code which is easier to work with - and easier to optimize later, when you've found where the real bottlenecks are.
Create a HashSet a use contains method. String or ArrayList will have O(n) complexity where as HashSet will be O(1) complexity.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Pardon me if it's not the right place to post, but I'm so curious about this.
So December 5th is a date. Last year it fell on a Tuesday.
How can modulo be used to work this out?
I know this is a modulo problem, but just don't get how modulo is used here.
A famous algorithm for calculating the day of the week based on the date is Zeller's algorithm. If you follow that link to wikipedia, you will see the algorithm, which uses the modulus operator.
It is used to calculate the year of the century (denoted K) by doing
current year % 100
It's also used at the end of the expression, where we calculate modulo 7 of the formula shown on wikipedia:
https://wikimedia.org/api/rest_v1/media/math/render/svg/0f95195dcc0d98b351294277071736e97053324e
With a little intuition, you will realize 7 is used because there are 7 days in the week.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
My question is, if I want to check a character (only one character to check) of a string in a particular index which method is very efficient charAt() or startsWith(). I mean in comparison of time complexity as far I can guess startsWith() gets more time than charAt(). Because startsWith() needs to check a set of characters, but charAt() just need to check only one character.
Now tell me your opinion… what you think about which is efficient to use to check only one character?
Both methods can be used to check a specific character for its value.
charAt() directly returns the char at the requested index, startsWith(prefix, index) will return true if you provide corresponding arguments.
The major difference is that the second approach has a bit more of overhead.
So, theoretically option 1 has slightly better performance. But beyond that, you rather pick option 1 because that just does what you want in the most clear way.
The really important difference is not about performance, but about your code communicating your intent. So, albeit is possible to use startsWith() to do what you want, it is simply counterintuitive to use it that way.
if I want to check a character(Only one character to check) of a string in a particular index
You answered by yourself. If you need to check that in a particular index, you can't use startsWith() , because you can't choose the index.
They do different jobs, so , based on your question, always use charAt()
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a long with a single bit set and I need to know which it is, e.g. the index. I need to calculate this as fast as possible.
The naive idea is to divide by 2 and check whether the result is 1. But this would need up to 63 iterations (worst case).
My next idea was to make like a binary search, e.g. to look wether it is bit 63-32 or 31-0 then 63 - 48, 47 - 31, 31 - 16, 15 - 0 and so on having many if-else statements, but this gives me hell of a bunch of code...
Furthermore I'd like to minimize object creation and memory used. You might suggest that I'm wrong then with Java and should use perhaps C/C++. Well it's for a school competition and I don't have a choice :)
I'd like to see some sample code!
Use Long.numberOfTrailingZeros - this will be exactly the index you are looking for.
Long.numberOfLeadingZeros can be also useful if you count bits starting from the highest one.
Both methods are JVM intrinsics, i.e. they are treated specially by JIT compiler. These methods are translated to a special CPU instruction (TZCNT / LZCNT) and thus are very efficient.
You could prepare a Map<Int, Int>, holding the number of the set bit for each possible value, but I'm not sure if it is really faster than a loop.
Maybe bit shifting is faster than dividing by 2.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have been storing date records (Date().getTime()) in an SQLite database with an Integer type. I realize now that Date().getTime() is returning Long values, not Integer.
Is there any way I can rescue the date data that is already stored in the database? Going forward I can reduce the resolution of the time, to make it fit into an integer. (ie. divide by 1000, and cast to int)
I expect that forcing a long value into an integer space has truncated the most significant digits - which might work in my favour, as the dates in question have all occurred within the past 6 months, so can probably be calculated.
question
so, how exactly would the long representation of today's date map onto an integer, and how might I use that knowledge (combined with the restricted time range) to build these integers back into their original long values?
Any suggestions?
Turns out the SQLite database integer type is perfectly capable of storing long values. So the data rescue was unnecessary in the end.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have java hashset like this
Java Hashset
7327896542976267 B ON 17 1381960719243
4116229919133022 B ON 7 1381960779618
2467710535609327 T OFF 5 1381960865253
7327896542976267 B OFF 16 1381961008020
7690519232957130 T ON 1 1381961124863
7690519232957130 T OFF 1 1381961364863
4144171091392103 F ON 1 1381961433257
2332355946816642 B ON 2 1381961601782
4270931484327447 B ON 10 1381961881239
6048000070479064 B ON 3 1381961959977
4270931484327447 B OFF 10 1381962121239
6056389598100654 T ON 13 1381962380866
5270503505599671 B ON 8 1381962381419
6048000070479064 B OFF 2 1381962588710
6056389598100654 T OFF 13 1381962620866
3873532220632004 T ON 1 1381962807491
3873532220632004 T OFF 1 1381963047491
3495435380122245 B ON 10 1381963314807
3495435380122245 B OFF 10 1381963554807 ................
Explanation -
This is information about passenger travelling of a public transport system,
3495435380122245(card number) B(transport type) ON(start travelling) 10 (travelling zone) 1381963314807 (time)
3495435380122245(card number) B(transport type) OFF(finish travelling) 15 (travelling zone) 1381963314807 (time)
QUESTION
This hashset has multiple entries for each passenger card number,
As a example, in above explanation that person travel 5 zones. How can I calculate this for each passenger? And how can I make a new hashset with new calculated values?
Working with objects will make your life much easier. For example a Trip object can hold information like card number, transport type, start time, finish time, start zone, finish zone, zone difference etc.
It will take one pass to your hashset to create another collection of such Trip objects. This collection can be a map (with card number as keys and Trip as values) if you want to search trips using card numbers later.
You need to sort the entries by time and replay them to build a state for each customer. With each event you add any charges incurred and at the end you know how much each customer's fee were.
BTW You can't sort a HashSet but you can sort an array or ArrayList.