• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

chapter 7: K&B

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody help me to solve chapter 7, example 9, page:628. Why this range is from -6 to 4. I think range is from -5 to 4, because last index is 4 so last negative insertion point is (-4-1=-5). Am I right?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those of us who don't have the book could help you if you post some details instead of just a page number. This would also help other users browsing these threads.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dolly,

The answer is correct. Suppose you are looking for something and the element is not there and it is supposed to be inserted at last position i.e 5, so binarySearch will return -6 because if the result is negative then you invert it (So it becomes 6) and then subtract it with 1 so (6-1=5), So you have to insert the new element at position 5 to keep it sorted.

And suppose you are looking for something which is at last index then it will return 4.

So the range is (-6 through 4)

Regards
Padma
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot to padma. I got it.
But I have doubt for page 558 example. In which at line-4 the answer is -1. So when I inverted it, that is 1 & then (1-1=0). So it gives me original possition of "one". Can you explained?
Thanks
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
book K & B, chap:7, example:12

import java.util.*;
public class Group extends HashSet<Person> {
public static void main(String[]atgs){
Group g=new Group();
g.add(new Person("Hans"));
g.add(new Person("Lotte"));
g.add(new Person("Jane"));
g.add(new Person("Hans"));
g.add(new Person("Jane"));
System.out.println("Total :"+ g.size());
}
public boolean add(Person o){
System.out.println("Adding" + o);
return super.add(o);
}
}
class Person{
private final String name;
public Person(String name){this.name=name;}
public String toString(){return name;}

output
=======
AddingHans
AddingLotte
AddingJane
AddingHans
AddingJane
Total :5

Why HashSet allow duplicates?
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dolly shah:
Why HashSet allow duplicates?



It doesn't. HashSet uses equals() method to find duplicates. As you haven't overriden Object's implementation of equals() (and hashcode()) in your Person class, it is using Object's implementation which does the check using == operator.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi dolly,
You need to override hashCode() and equals() in Person class. Look at the following code

import java.util.*;

public class Group extends HashSet<Person>
{
public static void main(String[]atgs)
{
Group g=new Group();
g.add(new Person("Hans"));
g.add(new Person("Lotte"));
g.add(new Person("Jane"));
g.add(new Person("Hans"));
g.add(new Person("Jane"));
System.out.println("Total :"+ g.size());
}

public boolean add(Person o)
{
System.out.println("Adding" + o);
return super.add(o);
}

}

class Person
{
private final String name;
public Person(String name)
{
this.name=name;
}
public String toString()
{
return name;
}
public boolean equals(Object o)
{
if( (o instanceof Person) && (((Person)o).name==this.name))
return true;
else
return false;
}
public int hashCode()
{
return name.length();
}
}

If you doesn't override them, then the Objects class equals() and hashCode() is used to compare the objects. This will allow multiple objects which are considered meaningfully equal to be added to the HashSet
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. By your program I got below output. Still it takes duplicates but now size is three. can you explained?

output
=======
AddingHans
AddingLotte
AddingJane
AddingHans
AddingJane
Total :3
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dolly shah:
...Still it takes duplicates but now size is three. can you explained?


The add method of HashSet returns a boolean depending on whether the element was added or not. If you modify your Group class to display this value, you will see that the duplicates are not added...
 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dolly

I am not sure what do you mean by


But I have doubt for page 558 example.
In which at line-4 the answer is -1. So when I inverted it, that is 1 & then (1-1=0). So it gives me original possition of "one". Can you explained?



Could you please paste the original code. I don't have the book with me right now.

Regards
Padma
 
sravani jetty
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With the modified code duplicates are not added in the HashSet. Call to
g.add(new Person("Hans")) calls the add method in Group's class which prints Adding Hans. That is just a print statement. Super.add(o) does actual addition.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc i am not getting your code. Can you explain little more? I don't understand that boolean method is in Group class, so that method let the class to add duplicates? May be I am making you confuse but I don't understand how to put my doubt.
In simple word I am getting this example anyways. Can anybody please help me?
This chapter is little hard only for me or others are also complained?
 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dolly

This is not happening for you alone. I was also confused should I say still confused about the Collections. The information given in that chapter is huge. I read it twice so far because when I read it first time then I knew that I didn't get anything, so I reread it again but during the second time I wrote some code and now I got some confidence but I still believe that I have to refer it once again before appearing in the real exam.
Nyways I hope these tips would be useful.

I can comment on Marc's example.


add() returns true or false depending on whether the element is added. It returns true if it can add the elements to the set and false otherwise and technically you can't add duplicate elements to set data structure.

Hope I am clear.

Regards
Padma
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Padma thanks for that.
But my doubt was without print your addition in Group class why boolean allows duplicates?
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic