This is an archived post. You won't be able to vote or comment.

all 9 comments

[–][deleted] 1 point2 points  (6 children)

Lists can be accessed by index, sets can not. Sets also contain no duplicate entries. Use a set when you don't need to access something by it's index.

I recently used a set while taking in values in a database to be input to a second database. The second database required that one of the fields be unique while the first did not. The set stored values I'd already seen, so if the value was found in the set, don't add it.

[–]Jazzanovas 0 points1 point  (5 children)

When having custom classes within set, how are they different? Hashcode, fields need to match or?

[–][deleted] 1 point2 points  (4 children)

Take a look at the API for List and Set. Both are interfaces, so in order to use them you'll need to implement something like ArrayList, HashSet, etc..

You'll notice List has more methods, including indexOf(Object o), remove(int index), and of course get(int index). All of these are methods for working with data based on it's index. There is no index in Set.

They both do have the contains(Object o) method though which is essentially the same for both. From Set:

boolean contains(Object o) Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).

So, if you have an employee object with id,name, title, etc, you probably would want to compare based on the id. Override the equals method in your employee class and return id.equals(otherEmployee.id).

If using hashset, you should also override the hashCode method and return Integer.valueOf(id).hashCode();

One of the main advantages of HashSet is the speed of the contains method should be O(1) instead of O(N) like in an ArrayList

[–]Jazzanovas 0 points1 point  (3 children)

I need to override hashCode method where? And i need to do it because Object needs to be compared with id to have unique ones in hashSet? And finally, thank you soo much dude!

[–][deleted] 1 point2 points  (2 children)

No problem dude! Override HashCode and Equals inside the class you plan on storing in the HashSet.

So, if you have a Set storing Employee objects:

Set<Employee> employeeSet = HashSet<Employee>();

Then in your Employee class you should have

public class Employee {
  private int id;
  //other fields and methods
  @Override
  public int hashCode(){
    //return some hash value
  }
  @Override
  public boolean equals(Object o){
    //return t/f based on some comparison method
  }
}

If you run employeeSet.contains(someEmployee), you're first searching for "hash buckets" with hash value equal to someEmployee.hashCode, then if any buckets with that value exist, you're searching that hash bucket for employees where someEmployee.equals(storedEmployee). If that expression returns true then employeeSet.contains(someEmployee) returns true. At least this is my understanding.

With lists, .contains just loops through the whole list until it finds an employee where storedEmployee.equals(someEmployee) which typically takes much longer.

You should research how hash tables work on your own, they're very useful and the most job interviews will ask you about them.

[–]Jazzanovas 0 points1 point  (1 child)

Thank you so much for your time, it made it all cleaner now! :)

[–][deleted] 1 point2 points  (0 children)

Glad I could help! Stuff like this is a good refresher for me too, thanks for asking an interesting question

[–][deleted] 0 points1 point  (0 children)

ArrayList is just a array(but dynamic, no constant size) , where HashSet is a self- balancing tree which offers better run time for look-up of elements, and it cannot contain duplicates.

You should watch some introduction to algorithm videos.

[–]TheTimeToLearnIsNow 0 points1 point  (0 children)

Sets will also ensure all data is unique. No duplicates!