Android Question Is there an equivalent to java.util.Set in B4A?

Shaun

Member
Licensed User
Longtime User
Sets are very handy in Java for making game boards and stuff. Example: In Tetris to assure your game pieces don't overlap each other or go off the board, etc. If not this would be very handy in b4a.

If not, is there a tutorial for making a basic wrapper for Java libraries?

Thanks

Shaun
 

Shaun

Member
Licensed User
Longtime User
Is there a tutorial for making a b4a wrapper for java libraries? Things like adapting java data types for use in b4a? Common issues people have wrapping java libraries?

A complete java code example of a relatively simple wrapped/adapted java library with step by step instructions on how it was made would be very helpful!

I know it this ^ is asking a lot, but it sure would be helpful for beginners!

Thanks
 
Last edited:
Upvote 0

Shaun

Member
Licensed User
Longtime User
You do not need to create a library for a Set. A Set is just a Map without values. You can use a regular Map and ignore the values (add an empty string as the value).

You can also wrap it in your own Set class.

What I am really interested in is the Hashset. I read that a Hashset is actually a Hashmap instance. This must be what you refer to. With a hashset you can compare multiple sets to see whether or not they contain elements of each other. A hashset data structure would be a nice feature in b4a.

The reason I ask is because I made a Tetris game in a java course I took years ago. While thinking about the board and game pieces, I realized that they could be represented as sets. Using Hashsets of points for the board and game pieces made collision detection trivial. It also allowed me to easily detect when the pieces came to the edge or bottom of the game board. I finished the assignment quickly, where the other students struggled manually comparing everything point by point. I found the hashset data structure to be very useful.

Thanks Erel.
 
Last edited:
Upvote 0

Shaun

Member
Licensed User
Longtime User
I apologize in advance for the pseudo-code. I haven't touched java (or any other language) in four years. I'm an old school VB6 programmer

The board was a HashSet of points and the pieces were HashSets of points

whenever a piece was rotated or moved, I did something like this:
board.containsAll(gamePiece) - If it returned false, the piece was going off the board - and the move wasn't allowed

To check for collisions between pieces when they moved or rotated, I did something like this:
tempPiece = new HashSet<Point>
tempPiece = gamePiece1.retainAll(gamePiece2) -tempPiece contains intersection between pieces
If tempPiece.size > 0 then the pieces overlapped - thus the move was not allowed


It would be nice to have an implementation of hashset that performs the set-algebraic operations. I know some of the methods are from collection.

From http://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
Bulk operations are particularly well suited to Sets; when applied, they perform standard set-algebraic operations. Suppose s1 and s2 are sets. Here's what bulk operations do:

  • s1.containsAll(s2) — returns true if s2 is a subset of s1. (s2 is a subset of s1 if set s1 contains all of the elements in s2.)
  • s1.addAll(s2) — transforms s1 into the union of s1 and s2. (The union of two sets is the set containing all of the elements contained in either set.)
  • s1.retainAll(s2) — transforms s1 into the intersection of s1 and s2. (The intersection of two sets is the set containing only the elements common to both sets.)
  • s1.removeAll(s2) — transforms s1 into the (asymmetric) set difference of s1 and s2. (For example, the set difference of s1 minus s2 is the set containing all of the elements found in s1 but not in s2.)
It would be nice to have a hashset with these operations for the community to use. I will be writing a tetris game as a learning exercise and would like to use sets to do so.

What is the best way to implement a hashset or equivalent? Is it possible to wrap the java hashset or would I have to implement the methods in b4a using a map?

Thanks for your patience.




 
Last edited:
Upvote 0

Shaun

Member
Licensed User
Longtime User
So keys can be objects and just use the Keyset. I think I can get it from here.

Thanks,

-Shaun
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…