1

I'm having a config entry, from which I'm loading into an String array like

String s = "abc$#def$#ghi";
String[] scbHLNewArray = s.split("\\$\\#");

Here I'm comparing a string with the array values after splitting it like ,

for(String arrNewErrorInfo : scbHLNewArray) {
   LOG.info("SCB HL New Error Value :"+arrNewErrorInfo+"\n");
   if(errorInfo.equals(arrNewErrorInfo)) {
       LOG.info("SCB HL Matched New value is :"+arrNewErrorInfo);
       newState = ApplicationState.NEW;
       addApplicationEvent(application.getId(),comment, ApplicationEventType.COMMENT,BBConstants.AUTOBOT);
       scbHLNewStatus = "Matched";
       break;
       }
}

I want to use some util classes like List.. Any idea on append to list and compare the string with the list objecT?

Thanks, Nizam

3
  • 1
    I really don't see what your question is, sorry. You append to a list via add. And you can ask if a String is inside your list with contains... What exactly is your question? Commented Mar 30, 2015 at 10:05
  • @FlorianSchaetz, My question is instead of assigning it to an Array, can we assign it to a list and compare some string with the list Commented Mar 30, 2015 at 10:06
  • you can do it with contains Commented Mar 30, 2015 at 10:08

3 Answers 3

1

you can do this with List contains method.

ArrayList<Integer> arrlist = new ArrayList<Integer<(8);

  // use add() method to add elements in the list
  arrlist.add(20);
  arrlist.add(25);
  arrlist.add(10);
  arrlist.add(15);        


  // list contains element 10
  boolean retval = arrlist.contains(10); // It will return true.
Sign up to request clarification or add additional context in comments.

Comments

0

Ok, let's try... First of all, you can create a List Object, wrapping your array very easily:

List<String> myList = Arrays.asList( scbHLNewArray );

Be carefull, because you can NOT add to this list, as it only wraps your array. If you want a list you can add to, you would have to create a new one, for example:

List<String> myModifiableList = new ArrayList<String>( myList );

This will create a new List that contains all the Strings from the first one but is also modifiable (you can add Strings, if you want).

In any case, you can use "contains", as Pratik has already shown, to test if a String is inside your list:

if (myList.contains("someString")) { ... }

This works because the String class already has well implemented equals(...) and hashCode() methods. If you want to put other Object than Strings into your list, you would have to make sure that these methods are implemented well, otherwise contains might not work as expected.

3 Comments

Cool explanation Florian. I'm doing in this way.. Please check it .. String errorInfo = bankBridgeServiceResponse.getResponseString(); List<String> listSCBHLNewErrMsgs= new ArrayList<String>(Arrays.asList(SCBHL_NEW_ERRORMESSAGES.split("\\$\\#"))); boolean newErrMsgStatus = listSCBHLNewErrMsgs.contains(errorInfo);
Sounds about right, except you don't need the ArrayList, the one returned by Arrays.asList should suffice as long as you only want to know if the String is inside the original array. And, as a personal note: I would not combine many operations into one line (like you are doing with new ArrayList, asList and split), it makes the code harder to read and doesn't really improve performance.
My question is.. I don't want an array.. then convert it into list object.
0

Yes you can use a list of course, you need to :

1. Take the result of split as an array.

2. Then convert this array to a list.

String s = "abc$#def$#ghi";
String[] scbHLNewArray = s.split("\\$\\#");
List<String> list=Arrays.asList(scbHLNewArray); //convert the array to a list

Take a look at Arrays.asList(Array a) and this Tutorial for further information about it.

And then to search the wanted String object you can use indexOf(Object o) or contains(Object o) List methods

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.