• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Directory Structures, HashSets, 2D Arrays, Comparators

 
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1: bigProject
|–source
||– Utils.java
|
|–classes

Is this the right way of reading this directory structure?
"|" means directory
"||" means a class within a directory.

So Utils.java is a class under the source directory, and classes is a separate directory.

2: If HashSets only have unique values, why did I have 2 values of 3 in the code below?



Result: [16, 3, 3]

What is the following array declaration trying to accomplish? When I tried using a nested for loop to print its contents I got an infinite loop.



Result:
562433 0
1562434 0
1562435 0
1562436 0

and so on.

3
I have this block of code:


Either of the following expressions fill the blank so the code prints gamma:



How is the reversed method being applied to a character? Isn't this method used to reverse an entire array?
According to the article below, returns "a Comparator object that will use the specified field as the sort key."
What is the blank argument of the sort method supposed to be? And how do the methods above supply that argument?
 
Dylan Kwon
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the article I talked about: https://www.webucator.com/article/how-to-use-the-comparatorcomparing-method-in-java/
 
Master Rancher
Posts: 5060
81
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These seem to be completely unrelated topics - probably better in separate posts.

----

1.  Your interpretation is correct.  || doesn't necessarily indicate a file  - it may be another directory contained within another directory.

----

2. Uniqueness for objects is a bit complicated, and depends on the equals() methods of the particular classes involved.  Here you have an Integer of 3, and a Long of 3.  Both Integer and Long define their equals() methods to require that the other object be the same class, in order to be equal.  Since Integer and Long are not the same class, these are considered not equal - even though both are 3, numerically.

Why did they do it this way?  I don't know - to me, it feels like a mistaken decision made long ago, that they keep now for backward compatibility.  Many classes have similar requirements in their equals methods, but not all.  It's possible for two different classes to have another parent class or interface in common, that defines equals() such that it can include different class types within it.  Look at List for an example, which allows an ArrayList and a LinkedList to be equal if their contents are equal.  They could have done something similar, defining equals() in the Number class to give the same result as == for primitive values.  But, they didn't, so we're stuck with it.

----

[no number]

It's an array of arrays, with only one row in it.  If you're having an infinite loop while printing it, that indicates you have a bug in your code, which we can't see.

----

3.

Dylan Kwon wrote:How is the reversed method being applied to a character? Isn't this method used to reverse an entire array?


The reversed method in this case is being applied to the Comparator, not any array, nor a String - though other methods exist with similar names which could apply to those other types.  But, this one is on Comparator, and is defined in that class.  The way it works is that it takes another Comparator (in this case, the one created by Comparator.comparing()) and creates a new Comparator with the opposite output.  

Dylan Kwon wrote:What is the blank argument of the sort method supposed to be? And how do the methods above supply that argument?


According to the API for Collections.sort(), the second argument (if there is one) should be a Comparator that knows how to compare elements of the type in question - Strings in this case.  As for "how do they work?", I think that's a bit too general a question at this point, that involves many different details.  The two different code samples use different techniques to create two different Comparator objects.  One uses a lambda expression, and one uses other methods on the Comparator class.  Have you studied either of these?  I suggest reading more about each, and asking more specific questions about the parts you don't understand.  But not as part of a long list of completely unrelated questions, because this does not have a short answer.
 
Dylan Kwon
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the clarifications! I have not studied Comparators well, so my questions are quite general.
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic