我有一个名单需要转换为具有相同姓氏的人都被标记的结果。例如:
原产地清单:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! TalesTableViewCell let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped:")
cell.tableImage.addGestureRecognizer(tapGesture)
cell.tableImage.userInteractionEnabled = true
return cell
}
func imageTapped(gesture: UIGestureRecognizer) {
//Code here I think
}
输出:
JayReese
ClaraSmith
JohnSmith
JayReese
ClaraSmith1
JohnSmith2
类的代码如下所示,如何比较所有Person
,何时重复lastName
,每个都添加唯一索引?应该添加什么方法?
我非常感谢任何输入和/或帮助。非常感谢你。
lastName
答案 0 :(得分:0)
你可以做这样的比较方法
public compairLastName(String LastName){
if(lastName.equals(LastName){
System.out.println("These last names are the same");
}else{
System.out.println("These last names are not the same");
}
答案 1 :(得分:0)
所以你问过标签(以及扩展排序),但是参考唯一标识 - 这里是两者的答案:)
排序
现实生活中的程序每天都在处理,只需确保您的可比代码默认按2个属性排序
如果他们按照这个顺序进行比较,你应该最终得到:
XavierAllen
JayReese
BillySmith
ClaraSmith
JohnSmith
为了比较多个属性,我将引用您的stackoverflow主题,该主题显示如何对具有相同字段名称的Person对象进行单次和多次比较; )
How to compare objects by multiple fields
独特参与
此外,如果您担心在简单比较排序之外唯一地识别Person,那么您将添加一个int字段(或guid,或任何你的风格),它将具有唯一值
基本上与数据库PK相同 - 您永远不会在数据库中使用人名作为PK,因此理想情况下,您的Person应该具有类似的属性。
如果你想将这个PK添加到你的toString(),那么就去吧
import java.util.*;
public class Person implements Comparable<Person> {
private int personID;
private String firstName;
private String lastName;
public Person(String firstName, String lastName, int personID) {
this.firstName = firstName;
this.lastName = lastName;
this.personID = personID;
}
public int getID(){
return this.personID;
}
public String toString() {
return this.personID + ": " + this.lastName + ", " + this.firstName;
}
}
@Mshnik确实发布了一个动态添加PK的方法,但你最好检查一下Person集合或一些高级变量来找到最后一个personID并从那里开始 - 否则动态值只能用于最多有限的上下文,你也可以只使用它从你正在从中检索它的Person集合中的索引
答案 2 :(得分:0)
首先,编辑Person
类或创建一个具有可设置索引字段的新类。您的用例完全没有可比性。
public class Person {
private String firstName;
private String lastName;
public int index;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String toString() {
return lastName + firstName + (index != 0 ? index : "");
}
}
您可以使用HashMap<String, Integer>
将姓名添加到姓氏中,如下所示:
public static void numberDuplicateLastNames(List<Person> people) {
HashMap<String, Integer> duplicatedLastNames = new HashMap<>();
for(Person p : people) {
if(! duplicatedLastNames.containsKey(p.lastName)) {
duplicatedLastNames.put(p.lastName, 1);
p.index = 1;
} else {
int i = duplicatedLastNames.get(p.lastName) + 1;
duplicatedLastNames.put(p.lastName, i);
p.index = i;
}
}
//Remove index from the people who don't have a duplicate last name
for(Person p : people) {
if (duplicatedLastNames.get(p.lastName) == 1) {
p.index = 0;
}
}
}
答案 3 :(得分:0)
以蛮力的方式标记Person
对象并不依赖于使用Map
来计算姓氏出现次数。
我还包括另一种利用Java 8流功能的方法。 IMO,蛮力方法更容易理解,但我还没有做过任何基准测试,知道哪个更有效。所以我会留下评论。
public static void main(String[] args) throws Exception {
List<Person> persons = new ArrayList() {
{
add(new Person("Jay", "Reese"));
add(new Person("Clara", "Smith"));
add(new Person("John", "Smith"));
add(new Person("James", "Smith"));
add(new Person("Christie", "Mayberry"));
add(new Person("Matthew", "Mayberry"));
}
};
// Toggle calls to see results
bruteForceLabel(persons);
// java8StreamLabel(persons);
}
public static void bruteForceLabel(List<Person> persons) {
for (int i = 0; i < persons.size(); i++) {
Person currentPerson = persons.get(i);
char lastCharacter = currentPerson.lastName.charAt(currentPerson.lastName.length() - 1);
// Only process last names that are not labeled
if (lastCharacter < '0' || '9' < lastCharacter) { // Not a digit
int counter = 2;
boolean foundDuplicateLastName = false;
for (int j = i + 1; j < persons.size(); j++) {
Person nextPerson = persons.get(j);
if (nextPerson.lastName.equals(currentPerson.lastName)) {
foundDuplicateLastName = true;
// Label the next person with the counter then
nextPerson.lastName = nextPerson.lastName + counter;
counter++;
}
}
// Label the current person with the starting sequence
if (foundDuplicateLastName) {
currentPerson.lastName = currentPerson.lastName + 1;
}
}
}
System.out.println(persons);
}
public static void java8StreamLabel(List<Person> persons) {
// Get a distinct count of all last names
Map<String, Long> lastNames = persons
.stream()
.map(p -> p.lastName)
.distinct()
.collect(Collectors
.toMap(p -> p, p -> persons
.stream()
.filter(p2 -> p2.lastName.equals(p)).count()));
// Apply number sequence to duplicate last names
lastNames.keySet().stream().filter((key) -> (lastNames.get(key) > 1)).forEach((key) -> {
int counter = 1;
for (Person person : persons.stream().filter(p -> p.lastName.equals(key)).toArray(size -> new Person[size])) {
person.lastName = person.lastName + counter;
counter++;
}
});
// Display the modified list
System.out.println(persons);
}
public static class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String toString() {
return firstName + " " + lastName;
}
}
结果:
[Jay Reese, Clara Smith1, John Smith2, James Smith3, Christie Mayberry1, Matthew Mayberry2]