If I have an arraylist of strings, and the content of this arraylist is essentially as follows:
[divya, divya1, divya2, divyachan, divyachan1] while inserting a new value into the list, e.g. if i want to insert "divya" i need to loop through the list check if it already exists, if yes, i need to see if there is already a sequence that is being followed and based on that i need to append the next number in the sequence and insert it into the list. In our case, the list already contains divya, divya1 and divya2 so now when i try inserting divya again, it should insert it as divya4. if we try inserting divyachan again, it should loop through the list and since divyachan and divyachan1 already exist, insert it as divyachan2 and so on.
I would like help on this. Any help is appreciated...
A list sounds like a wrong structure. It sounds more like you need to store a count for each value; e.g. for "divya" the count currently is 3 ("divya", "divya1", "divya2") and will be increased to 4, and for "divyachan" the count currently is 2 ("divyachan", "divyachan1") and will be increased to 3.
You can do that with a Map<String,Integer>. Insertion is done as follows:
Retrieval, shown using printing of the values:
An alternative that allows you to work in a similar way is to use a Bag from Apache Commons Collections. The retrieval would be the same; the insertion is handled by the Bag.
Why use ArrayList? Looks like you would like to store unique values in your list, so it's better to use some Set. Set is 'a collection that contains no duplicate elements'. So you could use its 'add' function as indicator - if it returns true, element is successfully added, if false - such element is already exists and was not added. Also you could use collection's 'contains' method to find out whether element is on the list instead of iterating through it.
Thank you all for the solutions, but it doesn't really resolve my problem...
I should have given you all the details in the first place...
The thing is that I get this list from the DB. My logic works on creating a username and if the username I have created already exists, then i need to increment the last digit appended to the username by 1 and send this value to be inserted into the DB. i.e. if the list contains divya1 then the username I need to create would be divya2 and so on.
Is there a way to achieve this?
I am using the contains method to check if the username I have created exists on the list, however I am unable to figure out how to go about the increment thingy...
Joined: May 31, 2011
Could you please give the input & output you need from your program? - might be better useful for others to help you!
Is this what you want? a Name string... or just count occurrence & return an incremented int value
Say this is in the list now [Dhivya, Dhivya1, Dhivya2,Dhivya3]
1. Say what should happen if a "Dhivya" comes?
2. What should be returned if a "Dhivya2" comes?
Initially the list that is coming from the DB contains:
Dhivya -> Dhivya1 -> Dhivya2 -> Dhivya3
Now according to my logic I have created the username Dhivya based on some Business Logic, then i need to check if this exists in the list (this has been handled)
if the username that we have created in the step above exists in the list from DB,
the list should be iterated to check how many names like 'Dhivya_' exist in the list and the username should be modified to Dhivya4 since we already have upto Dhivya3 in the list. (This needs to be implemented) this modified username is then sent to DB (this part has been handled)
That can be done a lot cleaner. Less efficient, but cleaner:
The logic is pretty easy: if the list does not contain the name (equivalent to isNamePresent being false) you return 0. Otherwise you start at 1, checking all the names: Dhivya1, Dhivya2, Dhivya3, etc, until you find the one that's not in the list. You return that number.
Now I said this code is less efficient. That's because it uses a nested loop: the outer loop with the while, and an inner loop for contains. You can make it more efficient by using a Set (most likely HashSet or LinkedHashSet, unless you want case insensitive in which case TreeSet is better) instead of a List, but that would probably requite a change of quite a bit of code.