Scala HashSet java.lang.NegativeArraySizeException





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















Following code throws exception after inserting 483180000 values. Is this expected. Is there any way to add more than a billion values into a mutable HashSet in Scala. Tried this code with Scala versions 2.11 and 2.12. Both versions throw same exception.



import scala.collection.mutable.HashSet

object TestApp extends App {
val hs = HashSet[Int]()
for (i <- 1 to Int.MaxValue) {
hs += i
}
}

java.lang.NegativeArraySizeException
at scala.collection.mutable.FlatHashTable.growTable(FlatHashTable.scala:218)
at scala.collection.mutable.FlatHashTable.addEntry(FlatHashTable.scala:160)
at scala.collection.mutable.FlatHashTable.addEntry$(FlatHashTable.scala:148)
at scala.collection.mutable.HashSet.addEntry(HashSet.scala:39)
at scala.collection.mutable.FlatHashTable.addElem(FlatHashTable.scala:140)
at scala.collection.mutable.FlatHashTable.addElem$(FlatHashTable.scala:139)
at scala.collection.mutable.HashSet.addElem(HashSet.scala:39)
at scala.collection.mutable.HashSet.$plus$eq(HashSet.scala:58)
at alice.grid.collection.TestApp$.$anonfun$new$2(TestApp.scala:12)
at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:155)
at alice.grid.collection.TestApp$.delayedEndpoint$alice$grid$collection$TestApp$1(TestApp.scala:11)
at alice.grid.collection.TestApp$delayedInit$body.apply(TestApp.scala:5)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:388)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at alice.grid.collection.TestApp$.main(TestApp.scala:5)
at alice.grid.collection.TestApp.main(TestApp.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)









share|improve this question





























    1















    Following code throws exception after inserting 483180000 values. Is this expected. Is there any way to add more than a billion values into a mutable HashSet in Scala. Tried this code with Scala versions 2.11 and 2.12. Both versions throw same exception.



    import scala.collection.mutable.HashSet

    object TestApp extends App {
    val hs = HashSet[Int]()
    for (i <- 1 to Int.MaxValue) {
    hs += i
    }
    }

    java.lang.NegativeArraySizeException
    at scala.collection.mutable.FlatHashTable.growTable(FlatHashTable.scala:218)
    at scala.collection.mutable.FlatHashTable.addEntry(FlatHashTable.scala:160)
    at scala.collection.mutable.FlatHashTable.addEntry$(FlatHashTable.scala:148)
    at scala.collection.mutable.HashSet.addEntry(HashSet.scala:39)
    at scala.collection.mutable.FlatHashTable.addElem(FlatHashTable.scala:140)
    at scala.collection.mutable.FlatHashTable.addElem$(FlatHashTable.scala:139)
    at scala.collection.mutable.HashSet.addElem(HashSet.scala:39)
    at scala.collection.mutable.HashSet.$plus$eq(HashSet.scala:58)
    at alice.grid.collection.TestApp$.$anonfun$new$2(TestApp.scala:12)
    at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:155)
    at alice.grid.collection.TestApp$.delayedEndpoint$alice$grid$collection$TestApp$1(TestApp.scala:11)
    at alice.grid.collection.TestApp$delayedInit$body.apply(TestApp.scala:5)
    at scala.Function0.apply$mcV$sp(Function0.scala:34)
    at scala.Function0.apply$mcV$sp$(Function0.scala:34)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App.$anonfun$main$1$adapted(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:388)
    at scala.App.main(App.scala:76)
    at scala.App.main$(App.scala:74)
    at alice.grid.collection.TestApp$.main(TestApp.scala:5)
    at alice.grid.collection.TestApp.main(TestApp.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)









    share|improve this question

























      1












      1








      1








      Following code throws exception after inserting 483180000 values. Is this expected. Is there any way to add more than a billion values into a mutable HashSet in Scala. Tried this code with Scala versions 2.11 and 2.12. Both versions throw same exception.



      import scala.collection.mutable.HashSet

      object TestApp extends App {
      val hs = HashSet[Int]()
      for (i <- 1 to Int.MaxValue) {
      hs += i
      }
      }

      java.lang.NegativeArraySizeException
      at scala.collection.mutable.FlatHashTable.growTable(FlatHashTable.scala:218)
      at scala.collection.mutable.FlatHashTable.addEntry(FlatHashTable.scala:160)
      at scala.collection.mutable.FlatHashTable.addEntry$(FlatHashTable.scala:148)
      at scala.collection.mutable.HashSet.addEntry(HashSet.scala:39)
      at scala.collection.mutable.FlatHashTable.addElem(FlatHashTable.scala:140)
      at scala.collection.mutable.FlatHashTable.addElem$(FlatHashTable.scala:139)
      at scala.collection.mutable.HashSet.addElem(HashSet.scala:39)
      at scala.collection.mutable.HashSet.$plus$eq(HashSet.scala:58)
      at alice.grid.collection.TestApp$.$anonfun$new$2(TestApp.scala:12)
      at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:155)
      at alice.grid.collection.TestApp$.delayedEndpoint$alice$grid$collection$TestApp$1(TestApp.scala:11)
      at alice.grid.collection.TestApp$delayedInit$body.apply(TestApp.scala:5)
      at scala.Function0.apply$mcV$sp(Function0.scala:34)
      at scala.Function0.apply$mcV$sp$(Function0.scala:34)
      at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
      at scala.App.$anonfun$main$1$adapted(App.scala:76)
      at scala.collection.immutable.List.foreach(List.scala:388)
      at scala.App.main(App.scala:76)
      at scala.App.main$(App.scala:74)
      at alice.grid.collection.TestApp$.main(TestApp.scala:5)
      at alice.grid.collection.TestApp.main(TestApp.scala)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)









      share|improve this question














      Following code throws exception after inserting 483180000 values. Is this expected. Is there any way to add more than a billion values into a mutable HashSet in Scala. Tried this code with Scala versions 2.11 and 2.12. Both versions throw same exception.



      import scala.collection.mutable.HashSet

      object TestApp extends App {
      val hs = HashSet[Int]()
      for (i <- 1 to Int.MaxValue) {
      hs += i
      }
      }

      java.lang.NegativeArraySizeException
      at scala.collection.mutable.FlatHashTable.growTable(FlatHashTable.scala:218)
      at scala.collection.mutable.FlatHashTable.addEntry(FlatHashTable.scala:160)
      at scala.collection.mutable.FlatHashTable.addEntry$(FlatHashTable.scala:148)
      at scala.collection.mutable.HashSet.addEntry(HashSet.scala:39)
      at scala.collection.mutable.FlatHashTable.addElem(FlatHashTable.scala:140)
      at scala.collection.mutable.FlatHashTable.addElem$(FlatHashTable.scala:139)
      at scala.collection.mutable.HashSet.addElem(HashSet.scala:39)
      at scala.collection.mutable.HashSet.$plus$eq(HashSet.scala:58)
      at alice.grid.collection.TestApp$.$anonfun$new$2(TestApp.scala:12)
      at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:155)
      at alice.grid.collection.TestApp$.delayedEndpoint$alice$grid$collection$TestApp$1(TestApp.scala:11)
      at alice.grid.collection.TestApp$delayedInit$body.apply(TestApp.scala:5)
      at scala.Function0.apply$mcV$sp(Function0.scala:34)
      at scala.Function0.apply$mcV$sp$(Function0.scala:34)
      at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
      at scala.App.$anonfun$main$1$adapted(App.scala:76)
      at scala.collection.immutable.List.foreach(List.scala:388)
      at scala.App.main(App.scala:76)
      at scala.App.main$(App.scala:74)
      at alice.grid.collection.TestApp$.main(TestApp.scala:5)
      at alice.grid.collection.TestApp.main(TestApp.scala)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)






      scala hashset






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 20:45









      MichaelSebMichaelSeb

      216




      216
























          1 Answer
          1






          active

          oldest

          votes


















          3














          No, there is no way, how to add that many items. If you check the source code at (FlatHashTable.scala:218), you will find:



          table = new Array[AnyRef](table.length * 2)


          lenth is an integer. At certain point, multiplying it by two will cause an overflow and the result is a negative number. Java does not allow to create an array with size specified by negative number.



          This is simply a limitation imposed by implementation of the HashSet.






          share|improve this answer


























          • Additionally, the max size of Java arrays is also limited roughly to max value of Integer, more precisely: stackoverflow.com/questions/3038392/…

            – ygor
            Nov 16 '18 at 21:14












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345144%2fscala-hashset-java-lang-negativearraysizeexception%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          No, there is no way, how to add that many items. If you check the source code at (FlatHashTable.scala:218), you will find:



          table = new Array[AnyRef](table.length * 2)


          lenth is an integer. At certain point, multiplying it by two will cause an overflow and the result is a negative number. Java does not allow to create an array with size specified by negative number.



          This is simply a limitation imposed by implementation of the HashSet.






          share|improve this answer


























          • Additionally, the max size of Java arrays is also limited roughly to max value of Integer, more precisely: stackoverflow.com/questions/3038392/…

            – ygor
            Nov 16 '18 at 21:14
















          3














          No, there is no way, how to add that many items. If you check the source code at (FlatHashTable.scala:218), you will find:



          table = new Array[AnyRef](table.length * 2)


          lenth is an integer. At certain point, multiplying it by two will cause an overflow and the result is a negative number. Java does not allow to create an array with size specified by negative number.



          This is simply a limitation imposed by implementation of the HashSet.






          share|improve this answer


























          • Additionally, the max size of Java arrays is also limited roughly to max value of Integer, more precisely: stackoverflow.com/questions/3038392/…

            – ygor
            Nov 16 '18 at 21:14














          3












          3








          3







          No, there is no way, how to add that many items. If you check the source code at (FlatHashTable.scala:218), you will find:



          table = new Array[AnyRef](table.length * 2)


          lenth is an integer. At certain point, multiplying it by two will cause an overflow and the result is a negative number. Java does not allow to create an array with size specified by negative number.



          This is simply a limitation imposed by implementation of the HashSet.






          share|improve this answer















          No, there is no way, how to add that many items. If you check the source code at (FlatHashTable.scala:218), you will find:



          table = new Array[AnyRef](table.length * 2)


          lenth is an integer. At certain point, multiplying it by two will cause an overflow and the result is a negative number. Java does not allow to create an array with size specified by negative number.



          This is simply a limitation imposed by implementation of the HashSet.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 21:15

























          answered Nov 16 '18 at 21:10









          ygorygor

          1,1521616




          1,1521616













          • Additionally, the max size of Java arrays is also limited roughly to max value of Integer, more precisely: stackoverflow.com/questions/3038392/…

            – ygor
            Nov 16 '18 at 21:14



















          • Additionally, the max size of Java arrays is also limited roughly to max value of Integer, more precisely: stackoverflow.com/questions/3038392/…

            – ygor
            Nov 16 '18 at 21:14

















          Additionally, the max size of Java arrays is also limited roughly to max value of Integer, more precisely: stackoverflow.com/questions/3038392/…

          – ygor
          Nov 16 '18 at 21:14





          Additionally, the max size of Java arrays is also limited roughly to max value of Integer, more precisely: stackoverflow.com/questions/3038392/…

          – ygor
          Nov 16 '18 at 21:14




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345144%2fscala-hashset-java-lang-negativearraysizeexception%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine