Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

375
Visualizações
Calling method from loop and returning value using Java 8 Lambda \ Stream

I am trying to convert the below traditional for loop in Java 8. I tried iterating over the loop using stream's forEach and used a filter for contains check but I am unable to understand how to make a call to extractForDate() method and return the value. Please can you assist with the approach?

for (int i = 0; i < lines.length; i++) {
            if (lines[i].contains(FOR_DATE)) {
                String regex = "regex expression";
                forDate = extractForDate(lines[i], regex);
                java.sql.Date sd = new java.sql.Date(forDate.getTime())
                break;
            }
        }

Below is the method implementation.

    private static Date extractForDate(String Line, string regex) {
        Matcher m = Pattern.compile(regex).matcher(line);
        Date startDate = null,
        if (m.find()) {
            try {
                startDate = new SimpleDateFormat("Mddyyyy").parse(m.group(1));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return startDate;
    }
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

To use Lambda/Stream api you need to have a List instance, you can convert your lines to ArrayList and do the foreach:

//...    
List<String> linesList = new ArrayList<>(lines);

linesList.stream()
    .filter(line -> line.contains(FOR_DATE)) //filter lines which contains FOR_DATE
    .map(line -> extractForDate(line, "regex")) //returns a list of Date e.g. List<Date>
    .forEach(date -> {
        java.sql.Date sd = new java.sql.Date(forDate.getTime());
    }
});
over 4 years ago · Santiago Trujillo Relatório

0

If I understood your code correctly you only want to create a date for the first line matching your expression. For that you can use filter() and findFirst(), which will give you an Optional on which you can use map() to create your date if anyone was found. I separated each step in its own map function but you can of course merge them into one.

    java.sql.Date date = Arrays.stream(lines)
            .filter(line -> line.contains(FOR_DATE))
            .findFirst()
            .map(line -> extractForDate(line, "regex expression"))
            .map(Date::getTime)
            .map(java.sql.Date::new)
            .orElse(null);
over 4 years ago · Santiago Trujillo Relatório

0

It doesn't stray that much from what you already have. You just need to break each step (filtering, mapping, collecting results) to a stream function. This is what you're looking for:

List<Date> listDates = Arrays.stream(lines)
    .filter(line -> line.contains(FOR_DATE))
    .map(line -> extractForDate(line, regex))
    .collect(Collectors.toList());

Here there's also a Test class where I assumed your regex and array of lines

public class Test {
    public static final String FOR_DATE = "DATE:";

    public static void main(String[] args) throws ParseException {
        String[] lines = new String[]{"DATE: 2152022", "test", "160220", "DATE: 1012001"};
        String regex = "(\\d\\d{2}\\d{4})";

        List<Date> listDates = Arrays.stream(lines)
                .filter(line -> line.contains(FOR_DATE))
                .map(line -> extractForDate(line, regex))
                .collect(Collectors.toList());

        for (Date d : listDates) {
            System.out.println(d);
        }
    }

    private static Date extractForDate(String line, String regex) {
        Matcher m = Pattern.compile(regex).matcher(line);
        Date startDate = null;
        if (m.find()) {
            try {
                startDate = new SimpleDateFormat("Mddyyyy").parse(m.group(1));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return startDate;
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda