Java数据映射是一种将Java对象与关系型数据库表之间的映射关系,它可以帮助开发人员在Java应用中使用关系型数据库。它可以将Java对象的属性映射到数据库表中的字段,从而使开发人员能够在Java应用中使用关系型数据库。
Java数据映射有助于减少代码量,并且可以节省时间。它可以帮助开发人员快速地将Java对象与关系型数据库表之间的映射关系进行配置,而不需要手动写大量的SQL语句来实现相同的功能。
此外,Java数据映射还有助于保证代码的一致性。当开发人员使用Java数据映射时,他们不需要手动写SQL语句来实现相同的功能,因此代码会保持一致性。
最后,Java数据映射也有助于减少代码量。当开发人员使用Java数据映射时,他们不需要手动写大量SQL语句来实现相同的功能;因此,代码量会大大减少。
public class User { private Long id; private String name; private Integer age; // getters and setters }
我们可以从流中收集数据到映射。
在三个版本中重载的toMap()方法Collectors类返回一个收集器以在Map中收集数据。
toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper) toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction) toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
第一个版本有两个参数。这两个参数都是一个函数。
第一个参数将流元素映射到映射中的键。
第二个参数将流元素映射到映射中的值。
如果重复键,则抛出IllegalStateException。
以下代码在Map<long,String>中收集员工的数据,其中的关键字是员工的ID,值是员工的姓名。
import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Map<Long,String> idToNameMap = Employee.persons() .stream() .collect(Collectors.toMap(Employee::getId, Employee::getName)); System.out.println(idToNameMap); } } class Employee { public static enum Gender { MALE, FEMALE } private long id; private String name; private Gender gender; private LocalDate dob; private double income; public Employee(long id, String name, Gender gender, LocalDate dob, double income) { this.id = id; this.name = name; this.gender = gender; this.dob = dob; this.income = income; } public long getId() { return id; } public String getName() { return name; } public static List<Employee> persons() { Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971, Month.JANUARY, 1), 2343.0); Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972, Month.JULY, 21), 7100.0); Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973, Month.MAY, 29), 5455.0); Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974, Month.OCTOBER, 16), 1800.0); Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975, Month.DECEMBER, 13), 1234.0); Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976, Month.JUNE, 9), 3211.0); List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6); return persons; } }
上面的代码生成以下结果。
下面列出的第二种形式的toMap具有额外的合并函数。
toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
合并函数传递重复键的旧值和新值。 该函数应合并两个值,并返回将用于键的新值。
以下代码显示如何连接所有男性和女性的名称。
import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Map<Employee.Gender,String> genderToNamesMap = Employee.persons() .stream() .collect(Collectors.toMap(Employee::getGender, Employee::getName, (oldValue, newValue) -> String.join(", ", oldValue, newValue))); System.out.println(genderToNamesMap); } } class Employee { public static enum Gender { MALE, FEMALE } private long id; private String name; private Gender gender; private LocalDate dob; private double income; public Employee(long id, String name, Gender gender, LocalDate dob, double income) { this.id = id; this.name = name; this.gender = gender; this.dob = dob; this.income = income; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Gender getGender() { return gender; } public static List<Employee> persons() { Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971, Month.JANUARY, 1), 2343.0); Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972, Month.JULY, 21), 7100.0); Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973, Month.MAY, 29), 5455.0); Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974, Month.OCTOBER, 16), 1800.0); Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975, Month.DECEMBER, 13), 1234.0); Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976, Month.JUNE, 9), 3211.0); List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6); return persons; } }
上面的代码生成以下结果。
下面列出的第三个版本允许我们使用供应商提供一个Map对象。
toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
以下代码按性别总结人数。
import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Map<Employee.Gender, Long> countByGender = Employee.persons() .stream() .collect(Collectors.toMap(Employee::getGender, p -> 1L, (oldCount, newCount) -> newCount+oldCount)); System.out.println(countByGender); } } class Employee { public static enum Gender { MALE, FEMALE } private long id; private String name; private Gender gender; private LocalDate dob; private double income; public Employee(long id, String name, Gender gender, LocalDate dob, double income) { this.id = id; this.name = name; this.gender = gender; this.dob = dob; this.income = income; } public Gender getGender() { return gender; } public static List<Employee> persons() { Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971, Month.JANUARY, 1), 2343.0); Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972, Month.JULY, 21), 7100.0); Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973, Month.MAY, 29), 5455.0); Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974, Month.OCTOBER, 16), 1800.0); Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975, Month.DECEMBER, 13), 1234.0); Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976, Month.JUNE, 9), 3211.0); List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6); return persons; } }
上面的代码生成以下结果。
以下代码在映射中按性别取得最高收入。
import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Map<Employee.Gender, Employee> highestEarnerByGender = Employee.persons() .stream() .collect(Collectors.toMap(Employee::getGender, Function.identity(), (oldPerson, newPerson) -> newPerson.getIncome() > oldPerson.getIncome() ? newPerson : oldPerson)); System.out.println(highestEarnerByGender); } } class Employee { public static enum Gender { MALE, FEMALE } private long id; private String name; private Gender gender; private LocalDate dob; private double income; public Employee(long id, String name, Gender gender, LocalDate dob, double income) { this.id = id; this.name = name; this.gender = gender; this.dob = dob; this.income = income; } public Gender getGender() { return gender; } public double getIncome() { return income; } public static List<Employee> persons() { Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971, Month.JANUARY, 1), 2343.0); Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972, Month.JULY, 21), 7100.0); Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973, Month.MAY, 29), 5455.0); Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974, Month.OCTOBER, 16), 1800.0); Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975, Month.DECEMBER, 13), 1234.0); Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976, Month.JUNE, 9), 3211.0); List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6); return persons; } @Override public String toString() { String str = String.format("(%s, %s, %s, %s, %.2f)n", id, name, gender, dob, income); return str; } }
上面的代码生成以下结果。
Java IO教程 -Java通道通道是数据源和Java程序之间的开放连接,用于执行I/O操作。Channel接口在java.nio.channels包中。通道接口...
Java IO教程 - Java控制台控制台类的目的是使Java程序和控制台之间的交互更容易。Console类是java.io包中的一个实用程序类,用于...
Java IO教程 - Java文件输出流创建输出流要写入文件,我们需要创建一个FileOutputStream类的对象,它将表示输出流。// Create af...
Java IO教程 - Java符号链接符号链接包含对另一个文件或目录的引用。符号链接引用的文件称为符号链接的目标文件。符号链接上的操...