我有一个ArrayList
来自.dat文件的员工信息(姓名,员工分数和每个员工雇用的日期)。我正在尝试遍历ArrayList以将员工团队放在一起,以便他们的集体得分超过20.我试图通过屏蔽子集中的每个位来实现这一点。
这是.dat文件:
Francis Pelaez, 9, 3/7/1992
Marlo Frankel, 4, 4/4/2001
Suzanne Engelmann, 2, 5/20/1992
Gertrude Anderson, 5, 8/16/2009
Delmer Mickels, 3, 1/19/1994
以下是我从.dat文件中将员工加载到ArrayList中的地方:
public static ArrayList<Employee> loadEmployees() {
ArrayList<Employee> employees = new ArrayList<Employee>();
//load employees from employees.dat here...
try (Scanner scanner = new Scanner(new FileInputStream("employees.dat"))) {
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] tokens = line.split(", ");
String fullName = tokens[0];
String score = tokens[1];
String dateHired = tokens[2];
System.out.println("Full Name: " + fullName);
System.out.println("Employee Score: " + score);
System.out.println("Date Hired: " + dateHired);
System.out.println("-----------------------------------");
employees.add(new Employee(fullName, score, dateHired));
}
} catch (FileNotFoundException ex) {
System.out.println("File not found: " + ex.getMessage());
}
return employees;
}
这是我的saveTeams()方法:
public static void saveTeams() {
//load employee objects and then query them
ArrayList<Employee> employees = loadEmployees();
//enumerate all subsets of employees and print
//any teams found with a collective employee
//rating above 20
int allMasks = (1 << employees.size());
for (int i = 1; i < allMasks; i++) {
for (int j = 0; j < employees.size(); j++) {
if ((i &(1 << j)) > 0) //the j-th element is used
System.out.print((j + 1) + " ");
System.out.println();
}
}
}
一旦我运行saveTeams(),它似乎显示的数字组合小于我的ArayList的大小。我的问题是如何比较每个员工的得分而不是他们在ArrayList中的索引?
谢谢大家的时间。
答案 0 :(得分:0)
如果您不介意使用Guava Collections和Java 8:
Set<Employee> employees = loadEmployees();
Sets.powerSet(employees).stream()
.filter(es -> 20 > es.stream().map(Employee::getScore).sum())
.collect(Collectors.toList())