双List集合轮询取值

List<String> beginChannels = Arrays.asList("9", "10"); // 变量集合
List<String> channelList = Arrays.asList("9", "10", "11", "12", "13", "14"); // 固定集合
List<String> channels = new ArrayList<>(); // 最终集合

if (Objects.nonNull(beginChannels) && !beginChannels.isEmpty()) {
    String end = beginChannels.get(beginChannels.size() - 1);
    int i = channelList.indexOf(end);
    if (i > 0 && i + 1 != channelList.size()) {
        channelList = channelList.subList(i + 1, channelList.size());
    }
}

Iterator<String> iterator = channelList.iterator();
while (iterator.hasNext() && channels.size() < 2) {
    String next = iterator.next();
    channels.add(next);
}
System.out.println(channels);

你可能感兴趣的:(Java,list,java,轮询)