Tutorial by Examples

django-admin is a command line tool that ships with Django. It comes with several useful commands for getting started with and managing a Django project. The command is the same as ./manage.py , with the difference that you don't need to be in the project directory. The DJANGO_SETTINGS_MODULE envir...
5 Object.freeze makes an object immutable by preventing the addition of new properties, the removal of existing properties, and the modification of the enumerability, configurability, and writability of existing properties. It also prevents the value of existing properties from being changed. Howev...
You can create an HTML`...` template string tag function to automatically encodes interpolated values. (This requires that interpolated values are only used as text, and may not be safe if interpolated values are used in code such as scripts or styles.) class HTMLString extends String { static e...
5 Object.seal prevents the addition or removal of properties from an object. Once an object has been sealed its property descriptors can't be converted to another type. Unlike Object.freeze it does allow properties to be edited. Attempts to do this operations on a sealed object will fail silently ...
// Java: List<String> list = people.stream().map(Person::getName).collect(Collectors.toList()); // Kotlin: val list = people.map { it.name } // toList() not needed
// Java: String joined = things.stream() .map(Object::toString) .collect(Collectors.joining(", ")); // Kotlin: val joined = things.joinToString() // ", " is used as separator, by default
// Java: int total = employees.stream() .collect(Collectors.summingInt(Employee::getSalary))); // Kotlin: val total = employees.sumBy { it.salary }
// Java: Map<Department, List<Employee>> byDept = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment)); // Kotlin: val byDept = employees.groupBy { it.department }
// Java: Map<Department, Integer> totalByDept = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.summingInt(Employee::getSalary))); // Kotlin: val totalByDept = employees.groupBy { it.dept }.mapValues { it.v...
// Java: Map<Boolean, List<Student>> passingFailing = students.stream() .collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD)); // Kotlin: val passingFailing = students.partition { it.grade >= PASS_THRESHOLD }
// Java: List<String> namesOfMaleMembersCollect = roster .stream() .filter(p -> p.getGender() == Person.Sex.MALE) .map(p -> p.getName()) .collect(Collectors.toList()); // Kotlin: val namesOfMaleMembers = roster.filter { it.gender == Person.Sex.MALE }.map { it.nam...
// Java: Map<Person.Sex, List<String>> namesByGender = roster.stream().collect( Collectors.groupingBy( Person::getGender, Collectors.mapping( Person::getName, Collectors.toList()))); // ...
// Java: List<String> filtered = items.stream() .filter( item -> item.startsWith("o") ) .collect(Collectors.toList()); // Kotlin: val filtered = items.filter { item.startsWith('o') }
// Java: String shortest = items.stream() .min(Comparator.comparing(item -> item.length())) .get(); // Kotlin: val shortest = items.minBy { it.length }
// Java: Stream.of("a1", "a2", "a3") .findFirst() .ifPresent(System.out::println); // Kotlin: sequenceOf("a1", "a2", "a3").firstOrNull()?.apply(::println)
// Java: IntStream.range(1, 4).forEach(System.out::println); // Kotlin: (inclusive range) (1..3).forEach(::println)
// Java: Arrays.stream(new int[] {1, 2, 3}) .map(n -> 2 * n + 1) .average() .ifPresent(System.out::println); // 5.0 // Kotlin: arrayOf(1,2,3).map { 2 * it + 1}.average().apply(::println)
// Java: Stream.of("a1", "a2", "a3") .map(s -> s.substring(1)) .mapToInt(Integer::parseInt) .max() .ifPresent(System.out::println); // 3 // Kotlin: sequenceOf("a1", "a2", "a3") .map { it.substring(1) } ...
// Java: IntStream.range(1, 4) .mapToObj(i -> "a" + i) .forEach(System.out::println); // a1 // a2 // a3 // Kotlin: (inclusive range) (1..3).map { "a$it" }.forEach(::println)
// Java: Stream.of(1.0, 2.0, 3.0) .mapToInt(Double::intValue) .mapToObj(i -> "a" + i) .forEach(System.out::println); // a1 // a2 // a3 // Kotlin: sequenceOf(1.0, 2.0, 3.0).map(Double::toInt).map { "a$it" }.forEach(::println)

Page 88 of 1336