首页 \ 问答 \ HashSet使用int数组(HashSet usage with int arrays)

HashSet使用int数组(HashSet usage with int arrays)

因为我有一个包含重复项的int数组的ArrayList,所以我想使用HashSet。 不幸的是,我无法像我所希望的那样设法使用HashSet:

System.out.print("\nTESTs\n");
    ArrayList<int[]> list = new ArrayList<int[]>();
    list.add(new int[]{1,2,3});
    list.add(new int[]{5,1,1});
    list.add(new int[]{1,2,3});//duplicate
    list.add(new int[]{5,1,3});

    Set<int[]> set = new HashSet<int[]>(list);
    System.out.println("Size of the set = "+set.size());

    ArrayList<int[]> arrayList = new ArrayList<int[]>(set);
    System.out.println("Size of the arrayList = "+arrayList.size());

    for (int[] array:arrayList){
        System.out.println(Arrays.toString(array));
    }

它导致:

Size of the set = 4
Size of the arrayList = 4
[1, 2, 3]
[1, 2, 3] // duplicate still here
[5, 1, 1]
[5, 1, 3]

有人可以告诉我我错了吗?

在此先感谢多米尼克(java新手)


As I have an ArrayList of int arrays which contains duplicates, I'd like to use HashSet. Unfortunately, I can't manage to use HashSet as I wish:

System.out.print("\nTESTs\n");
    ArrayList<int[]> list = new ArrayList<int[]>();
    list.add(new int[]{1,2,3});
    list.add(new int[]{5,1,1});
    list.add(new int[]{1,2,3});//duplicate
    list.add(new int[]{5,1,3});

    Set<int[]> set = new HashSet<int[]>(list);
    System.out.println("Size of the set = "+set.size());

    ArrayList<int[]> arrayList = new ArrayList<int[]>(set);
    System.out.println("Size of the arrayList = "+arrayList.size());

    for (int[] array:arrayList){
        System.out.println(Arrays.toString(array));
    }

It results in:

Size of the set = 4
Size of the arrayList = 4
[1, 2, 3]
[1, 2, 3] // duplicate still here
[5, 1, 1]
[5, 1, 3]

Could anybody tell me where I'm wrong ?

Thanks in advance Dominique (java newbie)


原文:https://stackoverflow.com/questions/28344312
更新时间:2021-07-08 17:07

最满意答案

问题在细节上有点模糊,但我会尽力提出一些建议。

首先,我认为在外部API上创建包装器是明智的。 我将使用外部电子邮件提供商的示例来尝试回答您的问题。

想象一下,我们有第三方电子邮件API,我们希望围绕它创建一个包装器。

我首先制作一个包装器,其行为方式与我希望电子邮件API的工作方式相同。 应该有这样的方法:


public EmailResponse SendEmail(EmailRequest request) { }
public User GetUser(string email) { }
public void OptOut(string email) { }

我首先设计的是我作为客户认为对于域都是通用的 - 并且易于使用。

然后,您将实现直接映射到第三方API的包装器接口。 在上面的示例中,可能需要两个外部API调用来发送电子邮件,但使用您的包装器的人不会知道这一点。

在我看来,如果你将它们与特定的实现紧密绑定,那么包装器就毫无价值。

C#有很多语言功能可以使这种方法成为可能,但我认为只需使用简单的接口即可满足您的需求。 不需要让它变得更难。


The question is a little vague on details, but I'll try and offer up some advice.

First I think you're wise to create a wrapper over an external API. I'll use the example of an external email provider to try and answer your question.

Imagine we have a third party email API and we want to make a wrapper around it.

I would start by making a wrapper that behaves the way I would expect an email API to work. There should be methods like:


public EmailResponse SendEmail(EmailRequest request) { }
public User GetUser(string email) { }
public void OptOut(string email) { }

I start by designing what I as a client think is both generic for the domain - and easy to use.

Then you make an implementation of the wrapper interface that maps directly to the third party API. In the example above, maybe it takes two external API calls to send an email, but the person using your wrapper won't know that.

In my opinion wrappers are worthless if you bind them too closely with a specific implementation.

C# has tons of language features that make this kind of approach possible, but I think just using a simple Interface will fit your needs. No need to make it harder than it is.

相关问答

更多
  • 我认为你在这里混淆了新旧混合,即ASMX Web服务与WCF,看到这个问题有类似的问题。 看看约翰的答案。 I think you are mixing old and new here, i.e ASMX web services with WCF, see this question for a similar issue. Look at the answers by John.
  • Assaf说。 您可以选择使用Web引用(WSDL.exe封装)和使用Service引用(svcutil.exe封装)。 服务引用是.NET 3.0+,也是WCF做事情的一部分。 就我个人而言,我大部分时间仍然使用Web引用,但是YMMV。 编辑: 两个菜单选项的屏幕截图 :) What Assaf said. You have the choice between using a Web reference (wrapper around WSDL.exe) and using a Service ref ...
  • 问题在细节上有点模糊,但我会尽力提出一些建议。 首先,我认为在外部API上创建包装器是明智的。 我将使用外部电子邮件提供商的示例来尝试回答您的问题。 想象一下,我们有第三方电子邮件API,我们希望围绕它创建一个包装器。 我首先制作一个包装器,其行为方式与我希望电子邮件API的工作方式相同。 应该有这样的方法: public EmailResponse SendEmail(EmailRequest request) { } public User GetUser(string email) { } publ ...
  • My_Arx_Search.Field_String campo = ((My_Arx_Search.Field_String)aggSearch); 这将创建一个类型为“My_Arx_Search.Field_String”的变量“campo”,并将其作为值将变量“aggSearch”转换为此类型。 campo.Operatore = My_Arx_Search.Dm_Base_Search_Operatore_String.Uguale; 这将右侧的值分配给“campo”变量的“Operatore” ...
  • 找到解决方案并发布它,以防其他人遇到此问题。 这是一个服务器端错误。 如果你看看我上面发布的方法的签名: public XmlDocument PostOrders(XmlDocument request) 问题是参数是一个XmlDocument 。 由于SOAP请求的有效负载已经被封装在XML中,因此无法将其反序列化为XmlDocument ,这是由于有效负载的根元素不是它来自的根元素(仅作为XmlElement或Linq XElement 。 但即使改变参数类型也不行 - 那就是我查看WSDL时的情况 ...
  • 您链接到的文档底部的c#示例看起来很有帮助但有缺陷。 APISoapClient没有CookieContainer属性,所以我会在没有尝试设置它的代码行的情况下尝试它。 稍后在代码中有一个对sAPI.createUsers的调用,但该示例尚未定义sAPI ,我认为它们意味着重用proxy 。 要以简单的方式开始,请右键单击项目“引用”,然后选择“添加服务引用”。 输入其端点并单击GO: https : //secure.logmeinrescue.com/api/API.asmx 如果从Service1更改 ...
  • 只是想回来并回答这个问题以防其他人有这个问题。 问题是Weblogic生成的XSD的元素顺序与SOAP响应的顺序不同。 Java似乎没有问题,但C#确实存在问题。 在这种情况下,XSD首先有文件名,文件日期为DataExtractDateModel。 当我切换订单时,它开始工作。 Just wanted to come back and answer this in case anyone else has this issue. The problem was the XSD that Weblogic ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)