How to generate unique alphanumeric ID 11 characters long in java [closed] - java

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 3 years ago.
Improve this question
I need to work on a framework for Unique ID generation. Currently, a reference number is 11 characters with 5 characters Julian date, 6th character specific to a datacenter and other 5 - uniquely generated sequence of alpha characters. This make the algorithm to generate 20 million unique records.
I do not want UUID format. Need a more readable format potentially with composition of a date that can represent when it's generated and uniquely generated characters/numbers.
Just wanted to go over the potential algorithms.

Sorry, unlikely to be very unique.
UUID is unbeatable but verbose (database/java). It gives 128 bits. Encode those with URL-safe Base64 and you get 22 almost alphanumeric chars (with two extras like - _).
Rolling your own: System.nanos().

Related

How do I find the most similar string from list [closed]

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 last month.
Improve this question
I have a list of strings in random format:
AppName-ver-1.1.0-data.exe
AppName-ver-1.1.1-secondData.exe
AppName-ver-1.2.0-data.exe
AppName-ver-1.2.1-data.exe
AppName-ver-1.2.3-data.exe
AnotherAppName-ver-1.0.0-data.exe
AnotherAppName-ver-1.0.0-secondData.exe
What would be an efficient way in java to find the closest value to string:
AppName-ver-1.2.4-data.exe
UPD: closest - by the naming not length so AppName-ver-1.2.3-data.exe is the expected result
To emphasize what commenters already pointed out:
If you define 'closest' to be the string length, then
AppName-ver-1.2.4-data.exe has the value 26, and
AppName-ver-1.1.0-data.exe
AppName-ver-1.2.0-data.exe
AppName-ver-1.2.1-data.exe
AppName-ver-1.2.3-data.exe
all resemble 26 as well so they are a direct match.
You could also define 'closest' to have the least Hamming Distance. This will give completely different results and AppName-ver-1.2.0-data.exe might win as it is just one bit off.

Regular Expression for Simple Maths Equation parser in Java [closed]

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
Currently learning Java, and trying to parse a maths equation for valid inputs:
For example, the user has to input integers in the form:
Operand Operator Operand
in that specific order, and the program would then need to be able to tell if the inputs were in that form, and then work out the simple equation.
An example would be:
4 * 8 which the result would give as 32
the program would also reject something like 45.6 * 0.3, or 45 + 3 / 4
For this to work, do I have to use regular expression, or some other method of if loops?
You can use RegEx to on one hand, get rid of the invalid input, and on the other hand make use of the groups to extract the operands and operation and apply the math
/^(\\d+)\\s*([+\\-*\\/])\\s*(\\d+)$/
Edit:
As Rory Daulton pointed out, the signed integers are excluded from the above RegEx, so the below one should be used instead
/^([+\\-]?\\d+)\\s*([+\\-*\\/])\\s*([+\\-]?\\d+)$/
Take look that:
https://stackoverflow.com/a/11009403/8701820
^([-+/*]\d+(\.\d+)?)*
Also you can check this website: https://regex101.com

Reading fixed width text file with different length [closed]

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 6 years ago.
Improve this question
I have a text file that is in fixed width format, except each column has a different length. I have about 100 columns in all.
For example, the first few columns of text have the following width:
30
4
188
Let's assume I read in the first row from the file. How do I seperate/map the text into 100 different size columns?
We just dealt with this at work within the last few weeks. The way we went about solving the problem was to create an enum class with corresponding "indexes" to represent the start and end positions of the fields that needed to be extracted. This enum is loaded into a map of FIELD_NAME --> RANGE (i.e. 0:8) upon instantiation of the class that parses the message.
High-level, upon receipt of a message on the queue:
convert TextMessage to string
read line
for each field, get the corresponding range from the map
split the range on ":" to get the indexes
extract the values from the String using substring(index1,index2)
perform transformations (string to date, string to numbers, etc)
persist to database

Validation using regex in java [closed]

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 7 years ago.
Improve this question
1)I have a field with the value as 11/06-13 (YY/DD-MM) how can i validate this value using regex.
2)I have a field with value 08:00 how can i validate this value using regex.
3)I have a field with value 2015-12-02+03:00 (YYYY-MM-DD+hh:mm) how can i validate this value using regex.
Please help i am new to regex.
Just read the Javadoc about Regular Expressions.
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
You can use
Pattern.matches(regex, Input);
to validate your Strings.
For your first Point it would be:
Pattern.matches("[0-9]{2}/[0-3][0-9]-(0|1)[0-9]", date);
The year can range from 00 to 99 followed by a "/". The day can range from 00 to 39. If you want to check for a valid day you can use:
(0[1-9])|((1|2)[0-9])|(30|1)
Now the day must be between 01 and 31.
The month can range from 00 to 19 in my first example. To avoid that you can use:
(0[1-9])|(1[0-2])
Complete Regex:
[0-9]{2}/((0[1-9])|((1|2)[0-9])|(30|1))-((0[1-9])|(1[0-2]))

Which is better searching in string or array [closed]

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.

Categories

Resources