What Is The Difference Between HashSet TreeSet And LinkedHashSet
Mar 12, 2014
What is the difference between HashSet, TreeSet and LinkedHashSet?.One difference between HashSet and TreeSet is that TreeSet is sorted, whereas HashSet is not sorted. I dont know the other differences
In linkedHashSet are the elements stored according to the insertion order or according to the hash value. If the elements are stored according to the insertion order then why is it not named as LinkedSet instead of LinkedHashSet? Why is the word hash used in LinkedHashSet?
public void createPersons() { Random rand = new Random(); for(int index = 0; index < persons.length; index++) { Person person = new Person(rand.nextInt(maxAge), persons[index]); setOfPersons.add(person);
[Code] ....
setOfPersons is a TreeSet, persons[] is an array of strings that contains 10 names. Why printPersons() prints only the half names of setOfPersons?
public class Person implements Comparable<Person> { // the age of the person private int age; //the name of the person private String name; //the Integer object to wrap the age value; private Integer ageWrap;
[Code] .....
The collection library has a class named TreeSet, which is an example of a sorted set. Elements in this set are kept in order. Carefully read the description of this class, and then write a class Person that can be inserted into a TreeSet, which will then sort the Person objects by age.
This is the exercise I am trying to solve. And this is as far as I have gotten to. Is it possible to sort my setOfPersons TreeSet directly? I tried to use
Collections.sort(setOfPersons)
method but it wont compile, and I realized that it is not applicable to TreeSet. So I made the
sortByAge()
method to do it indirectly...
I am puzzled though because in the exercise it states
write a class Person that can be inserted into a TreeSet, which will then sort the Person objects by age.
meaning that the TreeSet will sort the Person Objects and not my class..
I am using a TreeSet to tokenize a string. The output is sorted with numeric first followed by words
E.g. 13 26 45 and before etc.....................
Is there a tidy way to remove the numeric?
Last bit of my code is :-
// print the words separating them with a space for(String word : words) { System.out.print(word + " "); } } catch (FileNotFoundException fnfe) { System.err.println("Cannot read the input file - pass a valid file name"); }
A file has the data of the subjects and name of professors. The file contains line for each subject. The line starts with professor Name followed by the Subject name.
Harry Williams Advanced DBMS
James H Computer Networks
Sasha Ben Artificial Intelligence
Harry Williams Software Engineering
Palbo Kastro Formal Languages
Alex W Advanced SE
James H Operating System
Harry Williams Theoretical Foundation
Write a program with setter getter method to read the file, and then write a class to store several professors in a hashset (Single Key and Multiple Values).The program should be able to display all the professors in the set and allow the user to search for a professor.
Input:
Professor Name: James H. Then the Output will be:
Subjects Taken by the professor: Computer Networks, Operating System.Display No Classes available if the professor name does not exists .
I am trying to write a hashset to a text file. Normally, I have no issues with writing to txt as it is simple for me. However, in this case I am absolutely clueless.
A bit of background about the class: it is a hashset of ComputerScientist objects with 2 fields; name, and field of research (and yes, I know what I put to fill up the HashSet does not count, I was only trying to test to see if I could get this to work).
I know the basic setup to use filewriter to save strings to a txt, but I am getting confused with the HashSet element of it.
import java.util.HashSet; import java.io.FileWriter; import java.io.IOException; /** * Write a description of class ComputerScientistSet here. */ public class ComputerScientistSet { private HashSet<ComputerScientist> computerScientistSet;
Having trouble adding Class (Dollar) objects to a HashSet (money), i have done this before with arraylists and i understand that HashSets are different in that they cannot contain duplicates. Currently when this code is compiled i am getting "null" printed when I run the "howFullDatWallet" method.
import java.util.*; public class Wallet { private HashSet<Dollar> money; private int walletSize = 0; private int walletFiller = 0; /** * Constructor for objects of class Pocket */ public Pocket(int walletCap)
I have a HashSet, which I created to prevent duplicates upon output, but of course it's printing duplicates(or else I wouldn't be posting this). The order of my output does not matter, nor the input. The data type is String in the format (x + "," + z), where x and z are integers, creating a collection of coordinate sets. So to prevent the output of duplicates, I'm trying to get rid of the duplicates before they are added to the collection.
I've tried doing a '.equals()' string comparison but what happens is, since my string is added via one variable, it compares itself to itself and if itself equals itself it won't be added to the collection. I really need to keep this as a comparison of a single variable, because creating a key for each value would be sooo ridiculous for this volume of inputs.
So, with that being said, I would like to add one copy of the string, discard the duplicates, and do this thousands of times..
Is there any way to find how many number of elements are landing in same bucket in HashSet. I know Reflection could be one way but i am not able to design a program for that.
I'm not new to java but i'm not able to solve the following issue: I have a class
public class Localizzazioni implements java.io.Serializable { private <complexType> id; public getId()....... public setId().....
The complexType is a class defined in the code somewhere. Now I want to access it in another class I have
Set localizzazioni = new HashSet(0); localizzazioni=opere.getOiLocalizzazioneOperas(); -- this object give an object of tyoe HashSet for(Object object : localizzazioni) { object.get......... // i cannot use any method defined in the class Localizzazioni }
Why I cannot write inside the for object.getId() and using it?? In other word how i can access the element contained in the object?? the object is an iterator of type Localizzazioni . The class Localizzazioni has some method but i cannot use them? why ....
String TO = "raj@gmail.com;ra@gmail.com;RS@yahoo.com"; String CC= "rajt@in.com;rht@basss.com";
My query is i want to extract domains from above two Strings and Store these domains in HashSet. How could i do this with minimum code and performance wise.
I am new to java i dont understand the difference between the abstract and inheritance i mean we use the abstract class with extends with other class name ,even we do that same in the inheritance pls tell me main difference between abstract and inheritance...
I have been tasked with creating an invoice (school assignment). Part of the calculations is creating an interest depending on the amount of days between the current date entered, and invoice date entered (The program prompts the user to enter both the current and invoice dates by asking for the day, month and year).
We are also supposed to set the contructor to a default date of 1/1/1900.. but I don't know how or where to code that.
How to calculate the difference between the CurrentDate and Invoice. I have displayed the dates to the user as follows.
I have just begin to understand collections in Java. I read about the differences between LinkedList and ArrayList but have several doubts on it. I have mentioned them below
1) Why retrieving in ArrayList is faster then LinkedList?. Since both have the get method how does performance differ?.
2) How does re-sizing happens internally in ArrayList when a item is added or removed?. How slow this is compared to the pointer re-shuffling in LinkedList when a item is added or removed?.
I read that InputStream is used for byte based reading it reads 1 byte at a time.And InputStreamReader is used for charcter based reading so it reads one charcter at a time so no need to convert this first to int then read it.Here is reading using InputStream.
input=new FileInputStream("D:/input.txt"); int c; while((c=input.read())!=-1) { System.out.print((char)c); }
and here is reading using InputStreamReader
input=new FileInputStream("D:/input.txt"); reader=new InputStreamReader(input,"UTF-8"); int c;
so what is difference between InputStream and InputStreamReader in both case i have to use a Int and then read it and at the end if I want to print that data I have to cast that with "(char)c".So what is advantage of using InputStreamReader?
I know that oracle has released a statement saying that JavaFX will eventually replace Swing. What is the advantage of JavaFX? The new format, using "stage" instead of JFrame, seemed weird. Why is this change necessary? What benefit do we reap from JavaFX that Swing does not have?
Im working on my homework and it mentioned element for one exercise and an index in another, what is the difference, If Any, Between An Element And An Index?