How search a datum in ArrayList<StringBuilder> with indexOf [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 7 years ago.
Improve this question
How search a datum in ArrayList with indexOf
ArrayList<StringBuilder> state = new ArrayList<StringBuilder>();
state.add(new StringBuilder("A"));
state.add(new StringBuilder("B"));
state.add(new StringBuilder("C"));
state.add(new StringBuilder("D"));
System.out.println(state.indexOf(new StringBuilder("B"))); //Out: -1

You are using an ArrayList of StringBuilderobjects.
As StringBuilder is used for building strings, and not as actually Strings , you can't use indexOf(s), as the whole object itself should be equal to the one you are comparing so that it returns true.
In order to solve this problem, you should build the String however you want with the StringBuilder, and make an ArrayList of already-built Strings only, like this:
ArrayList<String> state = new ArrayList<String>();
StringBuilder a = new StringBuilder("A")
//Do whatever you want to a
state.add(a.toString());
state.add(b.toString());
state.add(c.toString());
state.add(d.toString());
.....
System.out.println(state.indexOf("B")); //Out: 1

This is because it searches by reference and you want to search by the contents of the string builder.
boolean there = state.stream() // use streams API
.anyMatch(sb -> sb.toString().equals("B")); // check string equals
System.out.println(there); // print if it's there

Related

How to stream the contents after Stream.Concat to avoid intermediate 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 2 years ago.
Improve this question
I have two Lists A and B. I would like to merge them and stream again to form an Array.
I'm currently doing:
List<String> aggregate = Stream.concat(A.stream(), B.stream()).collect(Collectors.toList());
String[] final = aggregate.stream().limit(10).collect(Collectors.toList()).toArray(new String[10]);
Here, I'm having two intermediate lists that I'm just using to grab the final Array. Is there a way I can eliminate the two collect(Collectors.toList()) and write this in a single line?
Thank you
You don't need to collect at all you can directly use .toArray()
String[] res = Stream.concat(A.stream(), B.stream()).limit(10).toArray(String[]::new);
And if you want concat the stream and then get as array separately without collecting
Stream<String> aggregate = Stream.concat(A.stream(), B.stream());
String[] res = aggregate.limit(10).toArray(String[]::new);
Use this way
Stream.concat(A.stream(), B.stream()).collect(Collectors.toList());
This will return a list. If you want to be it an array, then do
Stream.concat(A.stream(), B.stream()).toArray(String[]::new);
for multiple streams we can do like
Stream.concat(firstStream, concat(secondStream, concat(thirdStream, fourthStream))).toArray(String[]::new);
I hope it helps

create an array from elements in a string, elemets start with certain key/sign [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 want to abstract an array with text strings that reside in a text, each text element starts with a certain sign or key e.g. $ or &. how can I achieve this?
so "$Huey, $Dewey, and $Louie all live in Duckcity. $Goofy and $Mickey live there too." should result in
string characters = {"Heuy","Dewey", "Louie", "Goofy","Mickey"};
Use Streams and a filter.
String s = "$Huey, $Dewey, and $Louie all live in Duckcity. $Goofy and $Mickey live there too.";
String[] a =
// Split it into words.
Arrays.stream(s.split("[, .]"))
// Pick only the words starting with '$'
.filter(w -> w.startsWith("$"))
// Remove the '$'
.map(w -> w.substring(1))
// Make a list.
.collect(Collectors.toList())
// And turn it into an array.
.toArray(new String[0]);
System.out.println(Arrays.toString(a));
Prints:
[Huey, Dewey, Louie, Goofy, Mickey]
Go through the elements and check whether or not the string starts with the sign/key. If it does, add it to a String[]

java string add comma become array [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 8 years ago.
Improve this question
String items = "bookcupdoll";
I want to add a comma in items variable like below:
book,cup,doll
How should I make the string become an array?
If you are asking how to convert a string to an array...
There is a handy method called "split(String separator)"
If the string looks like : String items = "book,cup,doll";
you can use it like this
String[] array = items.split(",");
And you will end up with an array of size 3 with the 3 different elements.

What happens when an integer element in a list is changed to string in java? [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 8 years ago.
Improve this question
I have an array list. In that list I have a couple of string elements and few integer elements. I need to change all the elements in my arraylist to string so that I can modify the string elements. So, when I am doing this, even the integer elements in my arraylist are being changed to string. Would that be a problem if tried to access the integer elements after they are changed to string?
It is possible to use parseInt() so no it is not a problem. You can read more here: http://www.tutorialspoint.com/java/number_parseint.htm
You shouldn't attempt to alter the types of elements contained in a list. Instead, create a new list of strings, and add to it appropriately:
List<String> strings = new ArrayList<>(integers.size());
for (Integer a : integers)
strings.add(a.toString());

identify input is from particular list within a nested 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 8 years ago.
Improve this question
List<List<String>> listOflists = new List<List<String>>();
List<String> firstList = new List<String>();
firstList.add("NameX");
firstList.add("AgeX");
firstList.add("DesignationX");
listOflists.add(firstList);
List<String> secondList = new List<String>();
secondList.add("NameY");
secondList.add("AgeY");
secondList.add("DesignationY");
listOflists.add(secondList);
List<String> thirdList = new List<String>();
thirdList.add("NameZ");
thirdList.add("AgeZ");
thirdList.add("DesignationZ");
listOflists.add(thirdList);
Input:
My requirement is like "If i give Name? or Age? or Designation?" as input. I need to know that this particular input is from that list.
Example:
If i give "NameZ" as input, I need some clue that the input is from the thirdList.
Java collections have a method called, contains(item) which will search the collection for the specified item.
In this case the best thing to do is just go over these lists and use this method.
String searchStr = "age";
for (List<String> list : listOflists){
if (list.contains(searchStr)){
// inside this list, do something
}
}
If you are just looking for if an item is inside of collections, the fastest is by using a Set rather than a List, and would be more appropriate here.

Categories

Resources