1 itialize objects: J a HashSet object, nset, of Double objects ) a HashSet obje
ID: 3733030 • Letter: 1
Question
1 itialize objects: J a HashSet object, nset, of Double objects ) a HashSet object, stset of Student objects. ). a TreeSet object, cset, of Card objects. a TreeSet object, cSet1, of City objects, ordered by the default order l a TreeSet object, cset2, of City objects, ordered by the alternative order ByPopulation B. True or false? )A HashSet object can be accessed using index ) An ArayList object can have duplicated elements. ) A LinkedList object can be accessed using index. ) A TreeSet object can have duplicated elementsExplanation / Answer
1)
a) HashSet<Double> nset=new HashSet<Double>();
b) HashSet<Student> stSet=new HashSet<Student>(); // Student class must be declared above this initization
c) TreeSet<Card> cset=new TreeSet<Card>(); //The elements in TreeSet must be of Comparable type
d) TreeSet<City> cset1 =new TreeSet<City>();
e) TreeSet<City> cset2=new TreeSet<City>(Collections.reverseOrder());
2)
a) FALSE. HashSet should be used when you don't want to allow duplicate value in your collection. In HashSet you won't be able to find value by index. If you still want to find the element you have no other option but to use the iterator, it will iterate through the Hashset and give you one by one element from the Hashset
b) TRUE
c) TRUE. in java there is function java.util.LinkedList.get(int index) method returns the element at the specified position in this list. although this is not a effective and fast method take O(n) time;
d) FALSE .since it is a part of set. so it cannot contain duplicate
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.