Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a java program that runs on the Eclipse. } } Java Programming 2-4: Colle

ID: 3849569 • Letter: T

Question

This is a java program that runs on the Eclipse.

}

}

Java Programming 2-4: Collections Part 1 Practice Activities Lesson objectives: Create a collection without using generics Create a collection using generics Implement an ArrayList Implement a set Vocabulary: Identify the vocabulary word for each definition below. A set similar to an ArrayList without any specific ordering. An ordered Collection that may contain duplicates. An interface used to define a group of objects. This includes lists and sets. A list that is very similar to an array. A Collection of elements that does not contain any duplicates. Try lt/Solve It 1. What is the difference between a set and a list? 2. You decide you want to roll 2 dice and see what the frequency is of each possible number combination. Would you use a Set collection to do this? State your reason (s) 3. Using a collection create a variable that will store a list of countries (Strings). Your collection should not store duplicates, and order is not important. Test your code by adding 6 countries, one of which is a duplicate. 4. Would the following Collection.sort0 statements both work? Explain your answer. HashSet countries Set new Hashset String> Collections. sort (countriesSet); Array List countr List new Array List Collections sort (countriesList);

Explanation / Answer

// find the below answers:

1. difference between set and list.
  
   Set is an unordered collection, it doesn’t maintain any order. It doesn't allow duplicate element. Set implementations: HashSet, LinkedHashSet, TreeSet etc.
   List is an ordered collection, it displays the element in a ordered the way it has inserted. It allows duplicate element. List implementations: ArrayList,LinkedList etc.

2. We can not use set collection as set cannot contain duplicate elements here we want to check the frequency of each possible number combination...And since    it wont be able to take duplicatewe wont be able to calculate frequency For this purpose we can use List collection.

3. import java.util.*;
       class TestCollection9{
       public static void main(String args[]){
       HashSet<String> country=new HashSet<String>();
       country.add("india");
       country.add("india");
       country.add("london");
       country.add("tokiyo");
       country.add("america");
       country.add("japan");
       //Traversing elements
       Iterator<String> itr=set.iterator();
       while(itr.hasNext()){
       System.out.println(itr.next());
   }
   }
   }
output: india
       london
       tokiyo
       america
       japan

4. //below function will work with the default function Collection.sort();
  
   HashSet<String>countriesSet=new HashSet<String>();
   collection.sort(countriesSet);

//below code will give compilation error. it should define the data type. ERROR warning: [unchecked] unchecked conversion

   ArrayList<String> listofcountries = new ArrayList();
   ArrayList<String>countriesList=new ArrayList();

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote