Example usage for java.util.stream Collectors toList

List of usage examples for java.util.stream Collectors toList

Introduction

In this page you can find the example usage for java.util.stream Collectors toList.

Prototype

public static <T> Collector<T, ?, List<T>> toList() 

Source Link

Document

Returns a Collector that accumulates the input elements into a new List .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Dish> menu = Arrays.asList(new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("rice", true, 350, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("prawns", false, 400, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));
    // map/*from w ww .  j ava 2  s.  co m*/
    List<String> dishNames = menu.stream().map(Dish::getName).collect(Collectors.toList());
    System.out.println(dishNames);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Dish> menu = Arrays.asList(new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("rice", true, 350, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("prawns", false, 400, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));
    // Filtering with predicate
    List<Dish> vegetarianMenu = menu.stream().filter(Dish::isVegetarian).collect(Collectors.toList());

    vegetarianMenu.forEach(System.out::println);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Dish> menu = Arrays.asList(new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("rice", true, 350, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("prawns", false, 400, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));
    // Skipping elements
    List<Dish> dishesSkip2 = menu.stream().filter(d -> d.getCalories() > 300).skip(2)
            .collect(Collectors.toList());

    dishesSkip2.forEach(System.out::println);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Dish> menu = Arrays.asList(new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("rice", true, 350, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("prawns", false, 400, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));
    // Truncating a stream
    List<Dish> dishesLimit3 = menu.stream().filter(d -> d.getCalories() > 300).limit(3)
            .collect(Collectors.toList());

    dishesLimit3.forEach(System.out::println);

}

From source file:Main.java

public static void main(String... args) {
    Student raoul = new Student("Raoul", "Cambridge");
    Student mario = new Student("Mario", "Milan");
    Student alan = new Student("Alan", "Cambridge");
    Student brian = new Student("Brian", "Cambridge");

    List<Graduate> transactions = Arrays.asList(new Graduate(brian, 2011, 300), new Graduate(raoul, 2012, 1000),
            new Graduate(raoul, 2011, 400), new Graduate(mario, 2012, 710), new Graduate(mario, 2012, 700),
            new Graduate(alan, 2012, 950));

    List<String> cities = transactions.stream().map(transaction -> transaction.getTrader().getCity()).distinct()
            .collect(Collectors.toList());
    System.out.println(cities);/*from   w w w  .j  a v a2 s. co  m*/

}

From source file:Main.java

public static void main(String... args) {
    Student raoul = new Student("Raoul", "Cambridge");
    Student mario = new Student("Mario", "Milan");
    Student alan = new Student("Alan", "Cambridge");
    Student brian = new Student("Brian", "Cambridge");

    List<Graduate> transactions = Arrays.asList(new Graduate(brian, 2011, 300), new Graduate(raoul, 2012, 1000),
            new Graduate(raoul, 2011, 400), new Graduate(mario, 2012, 710), new Graduate(mario, 2012, 700),
            new Graduate(alan, 2012, 950));

    List<Graduate> tr2011 = transactions.stream().filter(transaction -> transaction.getYear() == 2011)
            .sorted(Comparator.comparing(Graduate::getValue)).collect(Collectors.toList());
    System.out.println(tr2011);/*w w  w . ja  va 2  s .  c om*/

}

From source file:DataSet.java

public static void main(final String... args) {
    final List<String> docks = CHARACTERS.stream().filter(c -> c.data.contains(2))
            .sorted((a, b) -> a.data.size() - b.data.size()).map(c -> c.name).collect(Collectors.toList());

    System.out.println(docks);/* w ww . j av  a2s . c om*/
}

From source file:Main.java

public static void main(String... args) {
    Student raoul = new Student("Raoul", "Cambridge");
    Student mario = new Student("Mario", "Milan");
    Student alan = new Student("Alan", "Cambridge");
    Student brian = new Student("Brian", "Cambridge");

    List<Graduate> transactions = Arrays.asList(new Graduate(brian, 2011, 300), new Graduate(raoul, 2012, 1000),
            new Graduate(raoul, 2011, 400), new Graduate(mario, 2012, 710), new Graduate(mario, 2012, 700),
            new Graduate(alan, 2012, 950));

    // Get all students from Cambridge and sort them by name.

    List<Student> traders = transactions.stream().map(Graduate::getTrader)
            .filter(trader -> trader.getCity().equals("Cambridge")).distinct()
            .sorted(Comparator.comparing(Student::getName)).collect(Collectors.toList());
    System.out.println(traders);// w  ww  .j a  v a2 s  . c  o  m

}

From source file:software.uncharted.Reindex.java

public static void main(String[] args) throws IOException {
    // Get all images
    JsonNode response = HTTPUtil.getJSON("http://localhost:3030/images/all");
    final ObjectMapper mapper = new ObjectMapper();

    // Create a list of post requests
    List<JsonNode> indexRequestBodies = JSONUtil.getStringList(response, "files").stream()
            .map(file -> "http://localhost:3030/image/" + file).map(url -> "{\"url\":\"" + url + "\"}")
            .map(json -> {//w  w  w .  j  a  v  a2 s.c  o m
                try {
                    return mapper.readTree(json);
                } catch (IOException e) {
                }
                return null;
            }).filter(Objects::nonNull).collect(Collectors.toList());

    // Reindex each
    for (JsonNode body : indexRequestBodies) {
        System.out.println("Indexing " + body.get("url").asText());
        HTTPUtil.post("http://localhost:8080/index", body);
    }
}

From source file:pt.souplesse.spark.Server.java

public static void main(String[] args) {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("guestbook");
    EntityManager manager = factory.createEntityManager();
    JinqJPAStreamProvider streams = new JinqJPAStreamProvider(factory);
    get("/messages", (req, rsp) -> {
        rsp.type("application/json");
        return gson.toJson(streams.streamAll(manager, Message.class).collect(Collectors.toList()));
    });//from  w w  w . j  a  v  a  2s  . c  om
    post("/messages", (req, rsp) -> {
        try {
            Message msg = gson.fromJson(req.body(), Message.class);
            if (StringUtils.isBlank(msg.getMessage()) || StringUtils.isBlank(msg.getName())) {
                halt(400);
            }
            manager.getTransaction().begin();
            manager.persist(msg);
            manager.getTransaction().commit();
        } catch (JsonSyntaxException e) {
            halt(400);
        }
        rsp.type("application/json");
        return gson.toJson(streams.streamAll(manager, Message.class).collect(Collectors.toList()));
    });
    get("/comments", (req, rsp) -> {
        rsp.type("application/json");
        Map<String, List<Body>> body = new HashMap<>();
        try (CloseableHttpClient client = create().build()) {
            String url = String.format("https://api.github.com/repos/%s/events", req.queryMap("repo").value());
            log.info(url);
            body = client.execute(new HttpGet(url), r -> {
                List<Map<String, Object>> list = gson.fromJson(EntityUtils.toString(r.getEntity()), List.class);
                Map<String, List<Body>> result = new HashMap<>();
                list.stream().filter(m -> m.getOrDefault("type", "").equals("IssueCommentEvent"))
                        .map(m -> new Body(((Map<String, String>) m.get("actor")).get("login"),
                                ((Map<String, Map<String, String>>) m.get("payload")).get("comment")
                                        .get("body")))
                        .forEach(b -> result.compute(b.getLogin(), (k, v) -> v == null ? new ArrayList<>()
                                : Lists.asList(b, v.toArray(new Body[v.size()]))));
                return result;
            });
        } catch (IOException e) {
            log.error(null, e);
            halt(400, e.getMessage());
        }
        return gson.toJson(body);
    });
}