Java – unable to get unique list element

I tried to extract a unique element from the class list with the following format:

[EntityClientPlayerMP['Player989'/228,l='MpServer',x=138.16,y=68.62,z=522.96],EntityCow['Cow'/231,x=143.63,y=68.00,z=527.50]....]

These lists are usually 60-100 in size

The method I'm trying to use now is

List<Class> uniqueList = new ArrayList<Class>(new HashSet<Class>(fullList));

This will return exactly the same list, but sorted differently Any ideas?

Solution

They are sorted differently because HashSet has a "weird" sort, based on the hashcode of objects To preserve sorting, use linkedhashset

As for uniqueness, set will use equals () and hashcode (), so please ensure the correct implementation Sometimes a feasible lazy technique is to use the toString () method of the object (and call hashcode and equals on the string)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>