• Post Reply Bookmark Topic Watch Topic
  • New Topic

containsAll is not working  RSS feed

 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.

Could someone please explain me why when I have

and

operation

returns false ??

 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post code that compiles and executes and shows the problem?
The posted code does not compile.


Note: Two objects with the same contents are still different objects.  For example "aaa" and "aaa" are two different Strings with the same contents.  The JVM does some optimization and may use one object for two Strings with the same contents.
 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Can you post code that compiles and executes and shows the problem?


Sure. This method checks if list (arr) contains all rotations of the string (str). I create additional array StringBuilder[] which contains all possible rotations.

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and the List<String> does not contain any of the elements of a StringBuilder[] nor of the List made from it.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have all sorts of confusing features in that method. Calling a List arr is confusing, for a start.
Don't use String#equals(""); use String#isEmpty(). That is probably incorrect, that your List doesn't contain any empty Strings. The null check shou‍ld precede that line, otherwise there is a risk of a null pointer exception.
You seem to be doing something strange with those StringBuilders; are you trying to create Strings of differing lengths?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The posted code does not compile so it can be executed for testing.  There needs to be a class defined and a main method.
Also the args for the method need to be given values to call the method.
 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for not posting all the code from the beginning.
Campbell Ritchie, I need StringBuilder[] to create an array of all rotations of a word. If we have "1234" String, its rotations will be "1234","4123","3412","2341".
I decided to use StringBuilder[] instead of String[] because I need to concatenate string a few times.


So, how would you compare StringBuilder[] and List<Strings>? Or there is no additional function for this kind of comparing?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you miss this?

Two objects with the same contents are still different objects.  For example "aaa" and "aaa" are two different Strings with the same contents.  The JVM does some optimization and may use one object for two Strings with the same contents.


Try this:

 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Did you miss this?
Two objects with the same contents are still different objects.  For example "aaa" and "aaa" are two different Strings with the same contents.  The JVM does some optimization and may use one object for two Strings with the same contents.


Yes, thanks. So actually there is no shorter equivalent of this code?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
547
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aleksey Movchan wrote: So actually there is no shorter equivalent of this code?


If you use Java 8 streams, there is:
 
Jesse Matty
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aleksey Movchan wrote:






that code will never compile.  List is an interface  you must declare  a class that implements  an interface to use for your list of strings such as ArrayList  or Vector or any other class that implements the list interface.  basic  arrays  do not implement  the list interface so cannot be declared like that. I would do  List<String> string = new ArrayList<String>();  and then call  the string. add("string"); method to add strings to it.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!