[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Collcection<String> value

Posted on 2009-04-16
17
Medium Priority
?
257 Views
Last Modified: 2012-05-06
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
   
   
0
Comment
Question by:Mickeys
  • 6
  • 4
  • 3
  • +2
17 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 24155805
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24155812
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 1200 total points
ID: 24155813
you can't do that with collection, you need to convert that to List first!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 24155823
Iterate the List to find the correct index to use for the above
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24155826
for e.g.,

List<String> list = new ArrayList<String>( collection_deployment ) ;
list.add( 2, yourString ) ;
0
 
LVL 92

Expert Comment

by:objects
ID: 24155898
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
 
LVL 20

Assisted Solution

by:ChristoferDutz
ChristoferDutz earned 400 total points
ID: 24155939
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
 

Author Comment

by:Mickeys
ID: 24156058
Oh alot of answers. :-)

How do I search the list? Lets say I want to search the list for a string including "bla2".
0
 
LVL 92

Expert Comment

by:objects
ID: 24156064
you need to iterate through it

0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24156089
myList.indexOf( yourSearchString ) ; would return the index of the element you want to search!
0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 24156110
But only If yourSearchString equals the complete value of the list element, not if it only matches part of the string.
0
 

Author Comment

by:Mickeys
ID: 24156141
so if I only want part of the string? What is used then?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24156145
you need to iterate as objects pointed out!
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24156164
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
 

Author Comment

by:Mickeys
ID: 24156170

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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24156493
>>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
 
LVL 26

Expert Comment

by:ksivananth
ID: 24156506
>>You mean as i pointed out ;-)

I am sorry, it should have been CEHJ!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question