Collcection<String> value

Hi,

I need to be able to put in some values in a collection. How is this done the best way?

My code
Collection<String> collection_deployment = deployment.getDeploy();
this will have this values:
   
collection_deployment->elemntData->
[0]   bla bla bla0
[1]   bla bla bla1
[2]   bla bla bla2
[3]   bla bla bla3

What I want is to add something between 1 and 2. How is this done?
I konw what the [1] string ends with: bla bla bla1. So do I search for that string or what?
Code example would be great
   
   
MickeysAsked:
Who is Participating?
 
ksivananthConnect With a Mentor Commented:
you can't do that with collection, you need to convert that to List first!
0
 
CEHJConnect With a Mentor Commented:
>>Collection<String> collection_deployment = deployment.getDeploy();


could be done as


List<String> collection_deployment = new ArrayList(deployment.getDeploy());

which will allow you to use List.set
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
CEHJCommented:
Iterate the List to find the correct index to use for the above
0
 
ksivananthCommented:
for e.g.,

List<String> list = new ArrayList<String>( collection_deployment ) ;
list.add( 2, yourString ) ;
0
 
objectsCommented:
use add() as ksivananth has posted above, it should do what you need.
Give us a yell if it doesn't do the job.

0
 
ChristoferDutzConnect With a Mentor Commented:
But you have to keep in mind, that even if the collection will have an element order, this is not guaranteed to be the same every time as Collections have no ordering. If the order is important (as I would guess from your "insert at position 2") you have to manally sort the List using a proper comparator and then insert.

You can use the static sort method of the Collections class for this:
Collections.sort(myList);
or:
Collections.sort(myList,myComparator);
for this.

A differen approach would be to have a look at the concrete type of your collection. As there is no Class Collection there is a chance, that the Object allready is of type ArrayList. In this case a simple cast would do the trick:

List<String> myList = (List<String>) deployment.getDeploy();
myList.add(2, myObejct);

Here I suggest adding some more code for safety:
Collection myCollection = deployment.getDeploy();
if(myCollection instanceof List<String>) {
... simply cast the list ...
} else {
... create a new list and sort it ...
}
if(
0
 
MickeysAuthor Commented:
Oh alot of answers. :-)

How do I search the list? Lets say I want to search the list for a string including "bla2".
0
 
objectsCommented:
you need to iterate through it

0
 
ksivananthCommented:
myList.indexOf( yourSearchString ) ; would return the index of the element you want to search!
0
 
ChristoferDutzCommented:
But only If yourSearchString equals the complete value of the list element, not if it only matches part of the string.
0
 
MickeysAuthor Commented:
so if I only want part of the string? What is used then?
0
 
ksivananthCommented:
you need to iterate as objects pointed out!
0
 
ksivananthCommented:
for e.g.,

int index = -1 ;
boolean found = false ;
for( String element : list ){
  index++ ;
  if( element.endsWith( str ) ){
   found = true ;
   break ;
  }
}

if found is true, you can use the index!
0
 
MickeysAuthor Commented:

List<String> collection_deployment = (List<String>)deployment.getDeploy();
Iterator <String>  iterate = collection_deployment.iterator (  ) ;
int i = collection_deployment.indexOf("bla bla bla2");

So how will the code be to find "bla2"?

       
0
 
CEHJCommented:
>>you need to iterate as objects pointed out!

You mean as i pointed out ;-) http:#24155823

>>Oh alot of answers. :-)

Fortunately a lot of them are the same ;-)
0
 
ksivananthCommented:
>>You mean as i pointed out ;-)

I am sorry, it should have been CEHJ!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.