在 Flink 中,可以使用 'List.get(index)' 方法按照下标查找列表中的元素。下面是一个示例代码:

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;
import java.util.List;

public class ListIndexExample {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        List<String> list = Arrays.asList('a', 'b', 'c', 'd', 'e');

        env.fromCollection(list)
                .map(element -> {
                    int index = list.indexOf(element);
                    return Tuple2.of(element, index);
                })
                .print();

        env.execute();
    }
}

这个例子中,我们创建了一个包含元素'a', 'b', 'c', 'd', 'e'的列表。然后,我们使用Flink的'fromCollection()'方法将列表作为数据源。在'map()'算子中,我们使用'indexOf()'方法获取元素在列表中的下标,并将元素和下标返回为Tuple2。最后,我们使用'print()'方法将结果打印出来。

输出结果如下:

(a,0)
(b,1)
(c,2)
(d,3)
(e,4)

这样,我们就成功地按照下标查找了列表中的元素。

Flink 列表下标查找:使用 List.get(index) 方法

原文地址: https://www.cveoy.top/t/topic/qeJz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录