Java创建客户函数生成随机值

huangapple go评论64阅读模式
英文:

Java create Customer function gives random values

问题

我正在进行一个向系统中插入客户的项目。它在Tomcat中运行,并且是一个Maven项目。自从我开始了一个新项目以来,有些事情能够正常工作,有些事情则不能。我创建了一个createCustomer函数:

public static Customer createCustomer(String name) {
    if (allCustomers.stream().noneMatch(e -> e.getName().equals(name))) {
        Customer newCustomer = new Customer(name);
        allCustomers.add(newCustomer);
        return newCustomer;
    } else return null;
}

当我运行我的BootupListener时,我输入了:

Customer.createCustomer("Maikol");
Customer.createCustomer("Henk");
nl.hu.bep.model.Customer.createCustomer("Sjon");

System.out.println(Customer.getAllCustomers());
System.out.println(Customer.getCustomer(1));
System.out.println(Customer.getCustomerById(1));

但奇怪的是,它输出如下:

[nl.hu.bep.model.Customer@94bfdfef, nl.hu.bep.model.Customer@4263369, nl.hu.bep.model.Customer@4c37fee]
nl.hu.bep.model.Customer@94bfdfef
nl.hu.bep.model.Customer@94bfdfef

nl.hu.bep是我的包标签,Customer是包含`createCustomer`的类。为什么它不起作用?
英文:

I'm working on a insert customers in system project. It is working in tomcat, and is a maven project. Since I started a new project; some things work; some things don't. I made a createCustomer function:

public static Customer createCustomer(String name){
    if (allCustomers.stream().noneMatch(e->e.getName().equals(name))) {
        Customer newCustomer = new Customer(name);
        allCustomers.add(newCustomer);
        return newCustomer;
    }
    else return null;
}

When I run my BootupListener; I entered:

    Customer.createCustomer("Maikol");
    Customer.createCustomer("Henk");
    nl.hu.bep.model.Customer.createCustomer("Sjon");

    System.out.println(Customer.getAllCustomers());
    System.out.println(Customer.getCustomer(1));
    System.out.println(Customer.getCustomerById(1));

Which strangely gives the prints:

[nl.hu.bep.model.Customer@94bfdfef, nl.hu.bep.model.Customer@4263369, nl.hu.bep.model.Customer@4c37fee]
nl.hu.bep.model.Customer@94bfdfef
nl.hu.bep.model.Customer@94bfdfef

nl.hu.bep is my package tag, and Customer is the class where createcustomer is located.
Why does it not work?

答案1

得分: 1

我不认为它会产生不同的输出。

所有具有对象标识符的客户分别是 -> 94bfdfef, 4263369, 4c37fee

据我们所知,最后两个输出明显显示两者都涉及到维护Customers

这个问题(如果它是一个问题的话),依我看,源自于保存用户的数据结构。例如,Set数据结构没有顺序。如果你使用HashMap将ID与客户号映射起来,仍然不能保证数据结构的顺序。

英文:

I don't think it gives different outputs.

All customers with the object specifiers are, respectively, -> 94bfdfef, 4263369, 4c37fee

As far as we know, the last 2 outputs show conspicuously that both are involved in the Customers maintained.

The issue(if it is a issue), imho, stems from the data structure hold the users. There is no order for Set data structure, for instance. If you map(using HashMap) the ids with the customer numbers, there is still no oder guarantee at all that of data structure.

答案2

得分: 1

当您打印一个集合时,它会打印 [,然后是一个逗号分隔的所有项的列表,然后是 ]。要打印 'an item',它会调用该项的 toString() 方法(除非它为空,然后它会打印 null)。

java.lang.Object 中的 toString() 完全不知道该怎么做,所以它只是打印类型的名称,后面跟着 @,然后是系统标识哈希码,这是无意义的,并且会在每次 JVM 调用时更改。

解决方法是实现 toString 方法。您的 Customer 方法应该是:

public class Customer {
    private String name;
    // 其他字段在这里

    @Override public String toString() {
        return "Customer: " + name;
    }
}
英文:

When you print a collection, it prints [, then a comma separated list of all the items inside, and then ]. And to print 'an item', it invokes that item's toString() method (unless it is null, then it prints null).

The toString() in java.lang.Object has absolutely no idea what to do, so it just prints the name of the type, followed by an @, followed by the system identity hashcode, which is meaningless, and will change on every JVM invocation.

The solution is to implement toString methods. Your Customer method SHOULD be:

public class Customer {
    private String name;
    // other fields here

    @Override public String toString() {
        return "Customer: " + name;
    }
}

huangapple
  • 本文由 发表于 2020年8月10日 01:29:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63329355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定