how to find all occurrence with indexes of string in an ArrayList java
Clash Royale CLAN TAG#URR8PPP
how to find all occurrence with indexes of string in an ArrayList java
Having an ArrayList with data like below. My requirement is to find all the indexes where length of data is 4.
values.add("1000");
values.add("10001111");
values.add("45678901");
values.add("1111");
values.add("22222222");
values.add("2222");
values.add("33333333");
If you show minimal effort or tell us what went wrong, we can help you find a solution. This seems like homework and we won´t do it for you.
– Glains
Aug 8 at 10:56
Read the following help topic: stackoverflow.com/help/how-to-ask
– m. vokhm
Aug 8 at 10:59
3 Answers
3
You need to :
Workable demo
: Using Streams
you can inline the solution to obtain a List
or a int
Workable demo
Streams
List
int
List<Integer> indexes = values.stream().filter(s -> s.length() == 4)
.map(values::indexOf)
.collect(Collectors.toList());
int indexesArray = values.stream().filter(s -> s.length() == 4)
.mapToInt(values::indexOf)
.toArray();
Workable demo
: Using a classic for loop
Workable demo
for loop
List<Integer> indexes = new ArrayList<>();
for(int i=0; i<values.size(); i++)
if(values.get(i).length() == 4)
indexes.add(i);
@Lino out of my head, yep
– azro
Aug 8 at 11:11
You can store the matching values and its indices/indexes in a Map<Integer, String>
when you iterate over the values
and output the content of that Map
afterwards:
Map<Integer, String>
values
Map
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test
public static void main(String args)
List<String> values = new ArrayList<String>();
values.add("1000");
values.add("10001111");
values.add("45678901");
values.add("1111");
values.add("22222222");
values.add("2222");
values.add("33333333");
// create a storage structure for index and value
Map<Integer, String> valuesWithLengthFour = new HashMap<Integer, String>();
// iterate over your list in order to get all the matching values and indexes
for (int i = 0; i < values.size(); i++)
String value = values.get(i);
if (value.length() == 4)
valuesWithLengthFour.put(i, value);
System.out.println("The following matching values were found in "values":");
// print out the content of the storage structure
for (int key : valuesWithLengthFour.keySet())
System.out.println("Index: " + key + ", Value: " + valuesWithLengthFour.get(key));
There are different ways to achieve this... For a one liner, try out the code @Lino stated in one of the comments below your question.
You can create an IntStream
of indices:
IntStream
IntStream allIndices = IntStream.range(0, values.size());
Which you then can filter, by the condition you provided:
IntStream filteredIndices = allIndices.filter(i -> values.get(i).length() == 4);
Finally you can transform those indices into any dataStructure you like.
An array:
int indices = filteredIndices.toArray();
Or a List
List<Integer> indices = filteredIndices.boxed().collect(Collectors.toList());
As one statement:
int indices = IntStream.range(0, values.size())
.filter(i -> values.get(i).length() == 4)
.toArray();
This one is quick and sort. Thanks @Lino.
– Kuldeep Rana
Aug 9 at 10:46
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Where is the code that is trying to achieve that?
– B001ᛦ
Aug 8 at 10:54