Flattening a collection within collection to get a single List<> - java

I'm quite new to using Java and was trying to flatten collections within collections using map to try and get a single List. However I don't seem to be able to get this working. In order to reproduce this I've created quite a simple example of what I'm trying to do. Here is what I have so far:
ClassA
import java.util.List;
public class ClassA {
private List<ClassB> listOfClassB;
public ClassA(List<ClassB> listOfClassB) {
this.listOfClassB = listOfClassB;
}
public List<ClassB> getListOfClassB() {
return this.listOfClassB;
}
}
ClassB
public class ClassB {
private String item;
public ClassB(String item) {
this.item = item;
}
public String getItem() {
return item;
}
}
Main
public class Main {
public static void main(String[] args) {
ClassB firstClass = new ClassB("A");
ClassB secondClass = new ClassB("B");
ClassB thirdClass = new ClassB("C");
ClassB fourthClass = new ClassB("D");
ArrayList<ClassB> firstClassList = new ArrayList<>();
ArrayList<ClassB> secondClassList = new ArrayList<>();
firstClassList.add(firstClass);
firstClassList.add(secondClass);
secondClassList.add(thirdClass);
secondClassList.add(fourthClass);
ArrayList<ClassA> classes = new ArrayList<>();
classes.add(new ClassA(firstClassList));
classes.add(new ClassA(secondClassList));
List<List<String>> collect = classes.stream().map(c -> c.getListOfClassB().stream().map(ClassB::getItem).collect(Collectors.toList())).collect(Collectors.toList());
}
}
As you can see on the bottom I am able to get List<List<String>> but what I'm looking to get is a List<String> of the items within ClassB. I've tried using a flatmap for this but I couldn't get it working and was looking for some guidance.
Thanks in advance.

Here is the flatmap example which works fine:
classes.stream().flatMap(aclass -> aclass.getListOfClassB().stream())
.forEach(b -> System.out.println("Class B Item Name : "+b.getItem()));
It gives the following output:
Class B Item Name : A
Class B Item Name : B
Class B Item Name : C
Class B Item Name : D
and to get the exact answer:
List<String> collect2 = classes.stream().flatMap(aclass -> aclass.getListOfClassB().stream())
.map(b -> b.getItem())
.collect(Collectors.toList());
it gives me a list as follows:
collect2 : [A, B, C, D]

This will return a list of Strings of ClassB :
List<String> listB = classes.stream().flatMap(a-> a.getListOfClassB().stream())
.map(ClassB::getItem)
.collect(Collectors.toList());
Here, we use a flatMap and then map the getItem of ClassB and finally use .collect(...) to return the list.

Related

Sort List Object with another objects

Assume I have two Classes
public class TestA{
TestB testB;
String text;
SecondTest secondTest;
.
.
.
}
public class TestB{
int a;
int b;
}
Now I have a List with TestB List<TestB> list1
If I want to sort the list I can do something like this:
list1.sort(Comparator.comparing(TestB::getA)
But what if I have a List with TestA List<TestA> list2
How I sort to a or b (TestB)?
That's an interesting question.
I don't know of any Java "native" solution for this kind of deep comparison.
But one idea is to use your own specific Comparator:
List<TestA> list2 = Arrays.asList( new TestA(new TestB(2, 20)), new TestA(new TestB(1, 10)), new TestA(new TestB(3, 30)) );
list2.sort( getComparator() );
public Comparator<TestA> getComparator() {
return new Comparator<TestA>() {
#Override
public int compare(TestA obj1, TestA obj2) {
int obj1A = obj1.getTestB().getA();
int obj2A = obj2.getTestB().getA();
return Integer.compare(obj1A, obj2A);
}
};
}
Of couse null values should be handled accordingly.

How to call a method from a generic class as parameter

I've tried to make my code use generics, but I can't seem to get it to work using generics. Any help would be appreciated.
I have 3 classes: Classroom, Course, Teacher
I have the following working code 3 times: (With the small change of the class)
private ObservableList<Classroom> parseClassrooms() {
// create new Observable List
ObservableList<Classroom> classrooms = FXCollections.observableArrayList();
// get lines from file;
ArrayList<String> arrayList = fhClassroom.read();
for (String line : arrayList) {
classrooms.add(Classroom.fromString(line));
}
return classrooms;
}
Methods in my Classes:
#Override
public String toString() {
return name;
}
public static Classroom fromString(String line) {
return new Classroom(line);
}
Is it possible to make this method generic? and pass the class as parameter?
I would like something like the following:
private ObservableList<T> parseClassrooms(T, FileHelper fh) {
// create new Observable List
ObservableList<T> items = FXCollections.observableArrayList();
// get lines from file;
ArrayList<String> arrayList = fh.read();
for (String line : arrayList) {
items.add(T.fromString(line));
}
return items;
}
My best attempt:
import java.util.ArrayList;
import java.util.function.Function;
public class Helper {
public static <T> ObservableList<T> parseItems(Function<String, T> lineToItemFunction, FileHelper fh) {
// create new Observable List
ObservableList<T> items = FXCollections.observableArrayList();
// get lines from file;
ArrayList<String> arrayList = fh.read();
for (String line : arrayList) {
items.add(lineToItemFunction.apply(line));
}
return items;
}
}
And you call it this way:
ObservableList<ClassRoom> classRooms = Helper.parseItems(ClassRoom::fromLine, fileHelper);

transform Collection<myClass> to Collection<String>

I trying to implement functionally similar to CollectionUtils transform (Apache Commons Collections)
class CollectionUtils {
public static void transformerModifier(Collection<MyClass> myCollection) {
// How should I implement this method in order that
// output from the line 1 and line 2 will be the same ?
}
public static List<String> transform(Collection<MyClass> myCollection) {
List<String> strCollection = new LinkedList<>();
for (MyClass item : myCollection) {
strCollection.add(item.getName());
}
return strCollection;
}
}
class myClass {
private String name;
private int value;
myClass( String name, int value) {
this.name = name ;
this.value = value;
}
public String toString(){
return new String(name+ ":" + value ) ;
}
}
class MyClassCollection{
private List<myClass> list ;
myClassCollection(List<myClass> list){
this.list = list;
}
List<myClass> collection(){
return list.clone();
}
}
public class TestClass{
public static void main (String[] args) {
List<MyClass> list = new ArrayList<>();
list.add(new myClass("John", 12);
list.add(new myClass("Mike", 16);
list.add(new myClass("Eric", 13);
list.add(new myClass("Mark", 142);
list.add(new myClass("Alex", 112);
MyClassCollection myOjb = new MyClassCollection(list );
CollectionUtils.transformerModifier(myObj.collection() );
List<MyClass> myList = CollectionUtils.transform(myObj.collection());
System.out.println(Arrays.toString(myObj.collection().toArray)); // line 1
System.out.println(Arrays.toString(myList.toArray)); // line 2
}
}
output: [John,Mike,Eric,Mark,Alex] // output after line 1
output: [John,Mike,Eric,Mark,Alex] // should be output after line 2
My question is it possible to implement method transformerModifier in the way that it will change collection of the object myObj so that myObj.collection() return not the List<myClass> but the List of List<String> ( where string is the data from private String name data member of myClass ) ?
My guess is that the solution should be through anonymous class. However, I didn't understand yet how should I implement it.
If you are using Java 8, you could make use of streams and map() to do something like this:
List<MyClass> myClassList = new ArrayList<>();
//add your items to myClassList here
List<String> names = myClassList.stream().map(MyClass::getName).collect(Collectors.toList());
//names will now consist of a List of all the names associated with
//each of the MyClass objects within myClassList in the same order
This solution makes use of Method Reference as well MyClass::getName. This calls the getName method on each object in the stream mapping it to its respective spot in the transformed stream using .map().
Next it uses .collect() to bring it back from a stream to a list using Collectors.toList().
If you are working with a lot of objects within myClassList, this process can be sped up using .parallelStream() instead of .stream(), but if you are not working with a large amount of data, you may see a reduction in performance with .parallelStream(). It all depends on how many objects you expect to be present within the List.
public interface Converter<I, O> {
void tranformer(List list);
O retriever(I obj);
}
_
public static <I, O> void transform(Converter<I, O> converter, List inputList) {
Iterator<I> it = inputList.iterator();
List list = new LinkedList<>();
while (it.hasNext()) {
list.add(converter.retriever(it.next()));
}
converter.tranformer(list);
}
_
public static void main(String[] args) {
List<MyClass> list = new ArrayList<>();
list.add(new myClass("John", 12);
list.add(new myClass("Mike", 16);
list.add(new myClass("Eric", 13);
list.add(new myClass("Mark", 142);
list.add(new myClass("Alex", 112);
MyClassCollection myclasscollection = new MyClassCollection(list);
final List collectionList = myclasscollection.collection();
CollectionUtils.transform(new Converter<myClass, String>() {
#Override
public void tranformer(List list) {
employeeList.clear();
employeeList.addAll(list);
}
#Override
public String retriever(myClass obj) {
return obj.name; // make the data member public or add getter
}
}, collectionList);
collectionList.get(0).toString.toLowerCase();
}
This isn't fully what you need but I bet this isn't bad alternative. Please, notice that could output collection collectionList will be collection of objects ( not String ), however, you can access to methods of the String data type just to right like this collectionList.get(0).toString.toLowerCase(); Hope this help.

java downcast results in method not applicable error

I am trying to downcast a function for my database objects. I get:
The method GetAsStringArray(HashMap<Long,dbObject>) in the type
dbObject is not applicable for the arguments
(HashMap<Long,dbEmployee>)
when I try to call GetAsStringArray(). Here is my test code:
dbObject base class:
public class dbObject implements Comparable<dbObject> {
protected long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public static dbObject[] GetAsArray(HashMap<Long,dbObject> map) {
Collection<dbObject> list = map.values();
dbObject[] ar = list.toArray(new dbObject[] {});
Arrays.sort(ar);
return ar;
}
public static String[] GetAsStringArray(HashMap<Long,dbObject> map) {
Vector<String>vStrings = new Vector<String>();
Collection<dbObject> list = map.values();
dbObject[] ar = list.toArray(new dbObject[] {});
Arrays.sort(ar);
for (dbObject o : ar)
vStrings.add(o.toString());
return (String[]) vStrings.toArray(new String[] {});
}
#Override
public int compareTo(dbObject another) {
return (int)(this.getId() - another.getId());
}
}
Child class dbEmployee:
public class dbEmployee extends dbObject {
private String First;
private String Last;
public dbEmployee(){}
public String toString() {
return Last + ", " + First;
}
}
And the error:
public static void main(String[] args)
{
HashMap<Long,dbEmployee>mapEmployees = new HashMap<Long,dbEmployee>();
dbEmployee.GetAsStringArray(mapEmployees);
}
Note: some of my dbObjects will have their own compareTo() function (a few of the objects have a field in the db to override the default sort order), but most of the items just sort by id.
You will need to redefine the method thusly:
public static dbObject[] GetAsArray(HashMap<Long, ? extends dbObject> map) {
Collection<? extends dbObject> list = map.values();
dbObject[] ar = list.toArray(new dbObject[] {});
Arrays.sort(ar);
return ar;
}
A Map<Long, dbEmployee> cannot be used in place of a Map<Long, dbObject>, because although it has some of the same behaviour, some of its behaviour is very different. In particular, you can't add an Object of type dbObject to a Map<Long, dbEmployee>.
If the method GetAsArray(HashMap<Long, dbObject> map) allowed you to pass it a Map<Long, dbEmployee>, you would be able to add dbObjects to the map from inside the method. The method has no way to know that the map is not actually a map of dbObjects. Then, if you held on to a reference to the map outside of the method, you would have big problems. The map would no longer by type-safe. It would say it was a Map<Long, dbEmployee>, but it would actually contain dbObjects that were not dbEmployees.
The ? extends dbObject syntax allows you to specify what you can get out of the map without detailing what can go in it. You'll notice that if you now try to add something to the map inside the GetAsArray(HashMap<Long, extends dbObject> map) method, you'll get a compile error.
(There is an equivalent syntax, ? super dbEmployee, that would let you put things in the map without knowing what could come out).

Converting lists of one element type to a list of another type

I'm writing an adapter framework where I need to convert a list of objects from one class to another. I can iterate through the source list to do this as in
Java: Best way of converting List<Integer> to List<String>
However, I'm wondering if there is a way to do this on the fly when the target list is being iterated, so I don't have to iterate through the list twice.
Java 8 way:
List<String> original = ...;
List<Wrapper> converted = original.stream().map(Wrapper::new).collect(Collectors.toList());
assuming Wrapper class has a constructor accepting a String.
My answer to that question applies to your case:
import com.google.common.collect.Lists;
import com.google.common.base.Functions
List<Integer> integers = Arrays.asList(1, 2, 3, 4);
List<String> strings = Lists.transform(integers, Functions.toStringFunction());
The transformed list is a view on the original collection, so the transformation happens when the destination List is accessed.
As an alternative to the iterator pattern, you can use a abstract generic mapper class, and only override the transform method:
create a generic collection mapper for any data type
[optional] create a library of methods that transform between different data types (and override the method)
use that library
the implementation:
// Generic class to transform collections
public abstract class CollectionTransformer<E, F> {
abstract F transform(E e);
public List<F> transform(List<E> list) {
List<F> newList = new ArrayList<F>();
for (E e : list) {
newList.add(transform(e));
}
return newList;
}
}
// Method that transform Integer to String
// this override the transform method to specify the transformation
public static List<String> mapIntegerToStringCollection(List<Integer> list) {
CollectionTransformer transformer = new CollectionTransformer<Integer, String>() {
#Override
String transform(Integer e) {
return e.toString();
}
};
return transformer.transform(list);
}
// Example Usage
List<Integer> integers = Arrays.asList(1,2);
List<String> strings = mapIntegerToStringCollection(integers);
This would be useful is you have to use transformations every time, encapsulating the process.
So you can make a library of collection mappers very easy.
If you are trying to get a list of elements within a list then use the below code.Here the list of objects contains attribute name and below gets you list of names from that list
inputList.stream().map(p -> p.getName()).collect(Collectors.toList());
You can write a mapping iterator that decorates an existing iterator and applies a function on it. In this case, the function transforms the objects from one type to another "on-the-fly".
Something like this:
import java.util.*;
abstract class Transformer<T, U> implements Iterable<U>, Iterator<U> {
public abstract U apply(T object);
final Iterator<T> source;
Transformer(Iterable<T> source) { this.source = source.iterator(); }
#Override public boolean hasNext() { return source.hasNext(); }
#Override public U next() { return apply(source.next()); }
#Override public void remove() { source.remove(); }
public Iterator<U> iterator() { return this; }
}
public class TransformingIterator {
public static void main(String args[]) {
List<String> list = Arrays.asList("1", "2", "3");
Iterable<Integer> it = new Transformer<String, Integer>(list) {
#Override public Integer apply(String s) {
return Integer.parseInt(s);
}
};
for (int i : it) {
System.out.println(i);
}
}
}
Lambdaj allows to do that in a very simple and readable way. For example, supposing you have a list of Integer and you want to convert them in the corresponding String representation you could write something like that;
List<Integer> ints = asList(1, 2, 3, 4);
Iterator<String> stringIterator = convertIterator(ints, new Converter<Integer, String> {
public String convert(Integer i) { return Integer.toString(i); }
});
Lambdaj applies the conversion function only while you're iterating on the result.
There is also a more concise way to use the same feature. The next example works supposing that you have a list of persons with a name property and you want to convert that list in an iterator of person's names.
Iterator<String> namesIterator = convertIterator(persons, on(Person.class).getName());
Pretty easy. Isn't it?
This Could be a solutions --> by using map
List<Employee> employee = Arrays.asList(new Emp(1, 100), new Emp(2, 200), new Emp(3, 300));
List<Employee> employeS = employee.stream()
.map(emp -> new Emp(emp.getId(), emp.getSalary * 100))
.collect(Collectors.toList());
employeS .stream() .forEach(s -> System.out.println("Id :" + s.getId() + " Salary :" + s.getSalary()));
That question does not iterate through the list twice. It just iterates once and by far is the only known method.
Also you could use some transformer classes in commons-collections of google-collections but they all do the same thing under the hood :) the following being one way
CollectionUtils.collect(collectionOfIntegers, new org.apache.commons.collections.functors.StringValueTransformer());
Well, you could create your own iterator wrapper class to do this. But I doubt that you would save much by doing this.
Here's a simple example that wraps any iterator to a String iterator, using Object.toString() to do the mapping.
public MyIterator implements Iterator<String> {
private Iterator<? extends Object> it;
public MyIterator(Iterator<? extends Object> it) {
this.it = it;
}
public boolean hasNext() {
return it.hasNext();
}
public String next() {
return it.next().toString();
}
public void remove() {
it.remove();
}
}
I think you would either have to create a custom List (implementing the List interface) or a custom Iterator. For example:
ArrayList<String> targetList = new ArrayList<String>();
ConvertingIterator<String> iterator = new ConvertingIterator<String>(targetList);
// and here you would have to use a custom List implementation as a source List
// using the Iterator created above
But I doubt that this approach would save you much.
Here's an on-the-fly approach. (There must be something already like this in the jdk; I just can't find it.)
package com.gnahraf.util;
import java.util.AbstractList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
/**
*
*/
public class Lists {
private Lists() { }
public static <U,V> List<V> transform(List<U> source, Function<U, V> mapper) {
return new ListView<U, V>(source, mapper);
}
protected static class ListView<U, V> extends AbstractList<V> {
private final List<U> source;
private final Function<U, V> mapper;
protected ListView(List<U> source, Function<U, V> mapper) {
this.source = Objects.requireNonNull(source, "source");
this.mapper = Objects.requireNonNull(mapper, "mapper");
}
#Override
public V get(int index) {
return mapper.apply(source.get(index));
}
#Override
public int size() {
return source.size();
}
}
}

Categories