Map<String, Integer> map = list.stream()
.collect(Collectors.toMap(Animal::getType,
Animal::getCount,
(a, b) -> a + b));
Map<String, Integer> map = list.stream()
.collect(Collectors.groupingBy(Animal::getType,
Collectors.summingInt(Animal::getCount)));
List<Animal> aggregated
= list.stream()
.collect(Collector.of(HashMap<String, Integer>::new,
(acc, val) -> acc.compute(val.getType(), (k, v) -> v == null ? val.getCount() : v + val.getCount()),
(l, r) -> {
l.replaceAll((k, v) -> v + r.getOrDefault(k, 0));
return l;
},
acc -> acc.entrySet()
.stream()
.map(e -> new Animal(e.getKey(), e.getValue()))
.collect(Collectors.toList())));
public static void main(String[] args) throws Exception {
char ch;
do {
System.out.println("Press a key followed by ENTER: ");
ch = (char) System.in.read();
System.out.println("Entered: '" + ch + "' (=" + (int)ch + ")");
} while (ch != 'q');
}
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class NewServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Параметр
String parameter = request.getParameter("parameter");
// Старт HTTP сессии
HttpSession session = request.getSession(true);
session.setAttribute("parameter", parameter);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Заголовок</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Пример сервлета"+parameter+"</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
@Override
public String getServletInfo() {
return "Пример сервлета";
}
}