Question

In: Computer Science

In this lab, we will write some utility methods within a class called ListUtils. These three...

In this lab, we will write some utility methods within a class called ListUtils. These three methods will all be static methods, and their purpose will be to perform conversions between objects implementing one type of interface and objects implementing another type. For example, we will write a method for converting from an Iterable object to a Collection, and so on. We will discuss more about how this will work below.

Tip: you will definitely want to import java.util for this lab, since List and all of its relatives use it.

Iterable to Collection task:
public static <E> Collection<E> iterToCollection(Iterable<? extends E> iterable)

(3pts) Every Collection is an Iterable but not every Iterable is a Collection. However, if we have an Iterable, it implies a sequence of elements, so we can use that to built a Collection. That is what we will do in this method. To implement the method, we will need to create some kind of Collection object, and add elements from the Iterable's sequence to it one by one.

Hint: we don't want to store the result in a Collection itself, because it is just an interface, but there may be a more familiar choice of structure which implements Collection.

Collection to List task:
public static <E> List<E> collToList(Collection<? extends E> coll)

(3pts) As above, every List is a Collection but not every Collection is a List. However, if we have a Collection, it implies a fixed number of elements which can be retrieved in sequence, so we can use that to built a List by numbering the element indices in the order they are retrieved. That is what we will do in this method. To implement the method, we will need to create some kind of List object, and add elements from the Collection.

List to Map task:
public static <E> Map<Integer, E> listToMap(List<? extends E> list)

(3pts) A Map, sometimes known as an associative array or dictionary, is in some ways similar to a List, except that instead of accessing elements by the indices 0, 1, 2, etc., the elements are accessed by a key, which may or may not be in order, and which may or may not be a number. Every element in a Map is stored as a key-value pair (the value of the element plus the key which is used to access it). Thus, when we call the get() method, instead of passing in an int index, we pass in the key corresponding to the element we're looking for. An example of a class which implements the Map interface in Java is the HashMap class.

A List is not a Map and a Map is not a List. However a Map stores collections of data indexed by some kind of mapping. A Map is essentially a dictionary which allows us to look up elements in a collection of data using some reference key to find each element. The reference key for a Map can be any data type, but if we make that data type an Integer, then we can use the key to represent a List index. Thus, in this method, we will take a List and use it to build a Map by using the list index of each element as the element's key within the Map. Thus, if our List contains the elements 2, 4, and 6, then this method will produce a Map with the mappings 0 ⇒ 2, 1 ⇒ 4, and 2 ⇒ 6. As above, this would involve creating an appropriate type of Map and adding the elements from the List.

Solutions

Expert Solution


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


import java.util.*;

public class ListUtils {
public static <E> Collection<E> iterToCollection(Iterable<? extends E> iterable){
Collection<E> col = new ArrayList<E>();
for(E obj : iterable)
col.add(obj);
return col;
}
  
public static <E> List<E> collToList(Collection<? extends E> coll){
ArrayList<E> list = new ArrayList<E>();
Iterator<? extends E> iter = coll.iterator();
  
while(iter.hasNext())
list.add(iter.next());
return list;
  
}
  
public static <E> Map<Integer, E> listToMap(List<? extends E> list){
  
HashMap<Integer, E> map = new HashMap<Integer, E>();
for(int i = 0; i < list.size(); i++){
map.put(i, list.get(i));
}
  
return map;
  
}
}


Related Solutions

IDS 201 Lab Exercise 5 Within a class named LX5, write methods as follows: 1. a...
IDS 201 Lab Exercise 5 Within a class named LX5, write methods as follows: 1. a method named valueCheck that accepts a parameter int named value and, using a switch statement, displays text output as follows: if value is 4 or greater: "LOTS" if value equals 3: "three" if value equals 2: "two" if value equals 1: "one" if value equals 0: "zero" if value is negative: "NEGATIVE" 2. a method named textMatch that accepts two parameter String objects named...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
For today's lab we will be using the Car Object Class which we built in Lab...
For today's lab we will be using the Car Object Class which we built in Lab 1. I have attached my solution if you would prefer to use my solution instead of your own solution. We will working on remembering how to build sub-classes and user interfaces. So we will be creating new Java files for: An Interface called GreenHouseGasser Requires a method called CO2() which returns how much carbon dioxide the Object produces. Make sure to update Car so...
For this question you need to write some methods and class headers. (a) Assume that you...
For this question you need to write some methods and class headers. (a) Assume that you have written a Rectangle class with instance variables length and width. You have already written all set and get methods and area and perimeter methods. Write an equals() method that takes Object o as a parameter. The method should return true when the Object o is a rectangle with the same length and width. Answer: (b) A class named Fruit implements an interface called...
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
Write a class that has three overloaded static methods for calculating the areas of the following...
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: - circles - rectangles - cylinders Here are the formulas for calculating the area of the shapes. Area of a circle: Area = π r2, where p is Math.PI and r is the circle's radius Area of a rectangle: Area = Width x Length Area of a cylinder: Area = π r2 h, where p is Math.PI, r is the radius of...
Write a class that has three overloaded static methods for calculating the areas of the following...
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: - circles - rectangles - cylinders Here are the formulas for calculating the area of the shapes. Area of a circle: Area = π r2, where p is Math.PI and r is the circle's radius Area of a rectangle: Area = Width x Length Area of a cylinder: Area = π r2 h, where p is Math.PI, r is the radius of...
In Lab 4, you made a class with static methods. The static methods converted an integer...
In Lab 4, you made a class with static methods. The static methods converted an integer in binary, Hex, and Octal. To do this, you made a class of static methods. The class did not have any fields. The static methods received the integer value as a parameter. For this lab, make all of the static methods, non-static methods. Lab 5 Requirements Create a class named “Lab5_IntConv_Class Have one private field name intValue. Add two constructors: One is a default...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT