In: Computer Science
import java.util.*;
class Main
{
static ArrayList<String> list;
public static List<String> createList(ArrayList<String>
arrayList)
{
list = arrayList;
return list;
}
public static void printList(ArrayList<String>
arrayList)
{
System.out.println("Printing in 4 ways\n");
// 1
System.out.println(arrayList);
//2
for(String s:arrayList)
System.out.print(s+" ");
System.out.println();
//3
System.out.println(Arrays.deepToString(list.toArray()));
//4
for(int i=0;i<arrayList.size();i++)
System.out.print(arrayList.get(i)+" ");
System.out.println();
}
public static void filterList(ArrayList<String>
arrayList)
{
System.out.println("Filtered in 2 ways\n");
ArrayList<String> copyArrayList = arrayList;
//1
for(int i=0;i<arrayList.size();i++)
{
if(arrayList.get(i).contains("chuck")) {
arrayList.remove(i);
i--;
}
}
System.out.println(arrayList);
//2
copyArrayList.removeIf(str -> str.contains("chunk"));
System.out.println(copyArrayList);
}
public static void main(String[] args) {
//Task -1
String[] array={"The", "quick", "brown", "fox", "jumped", "over",
"the", "lazy", "dog", "how", "much", "wood", "would", "a",
"woodchuck", "chuck", "if", "a", "woodchuck", "could", "chuck",
"wood"};
ArrayList<String > list = new
ArrayList<String>(Arrays.asList(array));
createList(list);
printList(list);
filterList(list);
}
}
This is the first Part of the Code: The rest underneath has yet to be answered.
List Performance – 25 Points
Generate a loop of each type (ArrayList, LinkedList)
For each list, iterate 100,000 times using random access to grab a random value from the list
You don’t need to do anything with the value
(hint – use math.random())
Time your loops and print the results
(hint – consider using java.time.Instant for your timing)
Map Creation – 25 Points
Create a method with the following signature
public static Map<String, String> createMap(MapType type)
(hint MapType should be an enum with the values HashMap and LinkedHashMap)
createMap should return a map with the following values
[ [“Hello”, “World”], [“Goodbye”, “World”], [“Pink”, “Brown”], [“Ford”, “Toyota”], [“Nittany Lions”, “Stanford”]]
Map Iteration – 25 Points
Create two maps, one with the HashMap flag one with the LinkedHashMap tag
Iterate over each list printing out the Key/Value Pairs
Note the different loop ordering
Expert Answer
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
import java.util.*;
class Main
{
static ArrayList<String> list;
static HashMap<String,String> map;
public static List<String> createList(ArrayList<String>
arrayList)
{
list = arrayList;
return list;
}
public static void printList(ArrayList<String>
arrayList)
{
System.out.println("Printing in 4 ways\n");
// 1
System.out.println(arrayList);
//2
for(String s:arrayList)
System.out.print(s+" ");
System.out.println();
//3
System.out.println(Arrays.deepToString(list.toArray()));
//4
for(int i=0;i<arrayList.size();i++)
System.out.print(arrayList.get(i)+" ");
System.out.println();
}
public static void filterList(ArrayList<String>
arrayList)
{
System.out.println("Filtered in 2 ways\n");
ArrayList<String> copyArrayList = arrayList;
//1
for(int i=0;i<arrayList.size();i++)
{
if(arrayList.get(i).contains("chuck")) {
arrayList.remove(i);
i--;
}
}
System.out.println(arrayList);
//2
copyArrayList.removeIf(str -> str.contains("chunk"));
System.out.println(copyArrayList);
}
public static void iterateArrayList(ArrayList<String>
arrayList)
{
long startTime = System.nanoTime();
Random random = new Random();
for(int i=0;i<100000;i++)
{
//Create a random index
int index = random.nextInt(arrayList.size());
//Access the element in the list
arrayList.get(index);
}
long endTime = System.nanoTime();
long totalTime = (endTime - startTime)/1000000;
System.out.println("Array List Takes "+totalTime+" Milli
Seconds");
}
public static void iterateLinkedList(LinkedList<String>
linkedList)
{
long startTime = System.nanoTime();
Random random = new Random();
for(int i=0;i<100000;i++)
{
//Create a random index
int index = random.nextInt(linkedList.size());
//Access the element in the list
linkedList.get(index);
}
long endTime = System.nanoTime();
long totalTime = (endTime - startTime)/1000000;
System.out.println("Linked List Takes "+totalTime+" Milli
Seconds");
}
//TASK - 4
public static Map<String, String>
createMap(HashMap<String, String> hashMap)
{
map = hashMap;
return map;
}
static void iterateHashMap(HashMap<String, String>
hashMap)
{
System.out.println("Hash MAP Iterate");
for (Map.Entry<String,String> entry :
hashMap.entrySet())
System.out.println("Key = " + entry.getKey() +
", Value = " + entry.getValue());
}
static void iterateLinkedHashMap(LinkedHashMap<String,
String> linkedHashMap)
{
System.out.println("Linked Hash MAP Iterate");
for (Map.Entry<String,String> entry :
linkedHashMap.entrySet())
System.out.println("Key = " + entry.getKey() +
", Value = " + entry.getValue());
}
public static void main(String[] args) {
//Task -1
String[] array={"The", "quick", "brown", "fox", "jumped", "over",
"the", "lazy", "dog", "how", "much", "wood", "would", "a",
"woodchuck", "chuck", "if", "a", "woodchuck", "could", "chuck",
"wood"};
ArrayList<String > list = new
ArrayList<String>(Arrays.asList(array));
createList(list);
//TASK 2
printList(list);
filterList(list);
//Task 3
//TYPE - 1 ARRAY LIST
ArrayList<String > list1 = new
ArrayList<String>(Arrays.asList(array));
iterateArrayList(list1);
//TYPE - 2 LINKED LIST
LinkedList<String > list2 = new
LinkedList<String>(Arrays.asList(array));
iterateLinkedList(list2);
//TASK 4
HashMap<String, String> hash_map = new HashMap<String,
String>();
hash_map.put("Hello","World");
hash_map.put("GoodBye", "World");
hash_map.put("Pink","Brown");
hash_map.put("Ford","Toyota");
hash_map.put("Nittany Lions","Stanford");
createMap(hash_map);
//TASK 5
iterateHashMap(hash_map);
LinkedHashMap<String, String> linked_hash_map = new
LinkedHashMap<String, String>();
linked_hash_map.put("Hello","World");
linked_hash_map.put("GoodBye", "World");
linked_hash_map.put("Pink","Brown");
linked_hash_map.put("Ford","Toyota");
linked_hash_map.put("Nittany Lions","Stanford");
iterateLinkedHashMap(linked_hash_map);
}
}
SCREENSHOT FOR CODE:
OUTPUT: