How can I add an object into a set in Java?

tigfra

I am writing some JUnit tests for a bus stop but I am having trouble trying to create objects to be used through the tests:

public class StopTests {
  BusRoute rte = new BusRoute("250");
  Set<BusRoute> set = new Set<BusRoute>();
  BusStop stop = new BusStop(00000, "Staples Center", 90.0, 90.0, set);
...
}

My problem is that the test won't compile because Eclipse says "new Set()" cannot be instantiated. My intention is to add 'rte' into 'set' so that 'stop' can be created without compilation errors, but I am stumped on how to do this. I tried approaching it like:

public class StopTests {
  BusRoute rte = new BusRoute("250");
  Set<BusRoute> set = new Set<BusRoute>();
  set.add(BusRoute rte);
  BusStop stop = new BusStop(00000, "Staples Center", 90.0, 90.0, set);
...
}

but Eclipse gave me another error about not having an identifier following 'add'.

What would be the best way to approach this?

EDIT: This is what I have now:

public class StopTests {
  BusRoute rte = new BusRoute("250");
  Set<BusRoute> set = new HashSet<BusRoute>();
  set.add(rte);
  BusStop stop = new BusStop(00000, "Staples Center", 90.0, 90.0, set);
...
}
Juned Ahsan

Set is an interface so cannot create an instance of it. You need to create and instance of Set implementations such as HashSet.

Try changing this:

Set<BusRoute> set = new Set<BusRoute>()

to

Set<BusRoute> set = new HashSet<BusRoute>()

Also to add the element in set you need to call add on set instance and not on BusRoute instance. So change this :

  rts.add(BusRoute rte);

to

  set.add(rte);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java - How can I add an object to a list of another object

From Dev

JAVA how can I readl a SQL and add in a object array

From Dev

JAVA how can I readl a SQL and add in a object array

From Dev

How can I add method to an existing object in Java?

From Dev

How can I add an object to an object in Javascript?

From Dev

How can i add to value not set value

From Dev

In Java how can I add an object to an array via the object's constructor?

From Dev

How can I set an NSDate object to midnight?

From Dev

How can I set properties of a clone object?

From Dev

How can I add an object into vector?

From Dev

How can I dynamically add to this object

From Dev

Can I add a property to the current "this" object? How?

From Dev

How can I add additional Dictionary object

From Dev

How can i add object into static array

From Dev

how can i add data to this object in node?

From Dev

How can I add an object to <? extends Interface>?

From Dev

how can I add this object in an array?

From Dev

How can i add set of information (in user defined data type) to an Array in java

From Dev

So i have this code that i am using arrays of [60] and how if i add an object can i edit it afterwards in java?

From Dev

How can I create linked invocations on my object in Java to execute a set of instructions by chaining methods together?

From Dev

How can I create an object and then add that object to an ArrayList?

From Dev

How can i set this path in java?

From Dev

How can I add a border to an image in Java?

From Dev

Java: how can i add a day on Date?

From Dev

How can I store Object in Enum in Java

From Dev

how can I create a graphics object in java?

From Dev

how can I read Object (java.lang.Object) in java

From Dev

Why can't I set a Double object equal to an int? - Java

From Dev

Why can't I set a Double object equal to an int? - Java

Related Related

  1. 1

    Java - How can I add an object to a list of another object

  2. 2

    JAVA how can I readl a SQL and add in a object array

  3. 3

    JAVA how can I readl a SQL and add in a object array

  4. 4

    How can I add method to an existing object in Java?

  5. 5

    How can I add an object to an object in Javascript?

  6. 6

    How can i add to value not set value

  7. 7

    In Java how can I add an object to an array via the object's constructor?

  8. 8

    How can I set an NSDate object to midnight?

  9. 9

    How can I set properties of a clone object?

  10. 10

    How can I add an object into vector?

  11. 11

    How can I dynamically add to this object

  12. 12

    Can I add a property to the current "this" object? How?

  13. 13

    How can I add additional Dictionary object

  14. 14

    How can i add object into static array

  15. 15

    how can i add data to this object in node?

  16. 16

    How can I add an object to <? extends Interface>?

  17. 17

    how can I add this object in an array?

  18. 18

    How can i add set of information (in user defined data type) to an Array in java

  19. 19

    So i have this code that i am using arrays of [60] and how if i add an object can i edit it afterwards in java?

  20. 20

    How can I create linked invocations on my object in Java to execute a set of instructions by chaining methods together?

  21. 21

    How can I create an object and then add that object to an ArrayList?

  22. 22

    How can i set this path in java?

  23. 23

    How can I add a border to an image in Java?

  24. 24

    Java: how can i add a day on Date?

  25. 25

    How can I store Object in Enum in Java

  26. 26

    how can I create a graphics object in java?

  27. 27

    how can I read Object (java.lang.Object) in java

  28. 28

    Why can't I set a Double object equal to an int? - Java

  29. 29

    Why can't I set a Double object equal to an int? - Java

HotTag

Archive