In: Computer Science
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.
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;
}
}