简体   繁体   中英

How do I get all the items from Java list from specified position ignoring all its previous without using IndexOf/sublist() method?

KeyValueAccess kva = KeyValueAccessFactoryForTestConfig.getConnection(); List> keySets = kva.getDistinctPartitionKeys(SavepointDto.class, Integer.valueOf(50));

    List<Pair<String, String>> pairs =
            keySets.stream()
                    .sorted((k1, k2) -> SavepointUtils.compareMinitues(k1.get(1), k2.get(1)))
                    .sorted((k1, k2) -> k1.get(0).compareTo(k2.get(1)))
                    .map(keySet -> Pair.of(keySet.get(0), keySet.get(1)))
                    .collect(Collectors.toList());

    System.out.println("\n");

    int index = pairs.indexOf("2016:6:29:13:27");

    for (int i = index; i < pairs.size(); i++) {
        System.out.println(pairs.get(i));
    }

}

I'm Having a List<Pair<String, String>> when I'm trying to get index of any specific element from list I'm getting ArrayIndexOutofBoundException (See above code) in my case the 1st element is date (ex: 2016:6:29:13:27 ) of String type. so can't use indexOf or substring() method.

Is there any other way to get all the elements from list from specific position by ignoring previous elements???

You can use stream for that ( skip first 2 elements):

Arrays.asList("a","b","c","d","e").stream()
    .skip(2)
    .collect(Collectors.toList())

Or use subList :

    List<String> list = Arrays.asList("a","b","c","d","e");
    list.subList(2, list.size());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM