Convert Set to List without creating new List - java

I am using this code to convert a Set to a List:
Map<String, List<String>> mainMap = new HashMap<>();
for (int i=0; i < something.size(); i++) {
Set<String> set = getSet(...); //returns different result each time
List<String> listOfNames = new ArrayList<>(set);
mainMap.put(differentKeyName, listOfNames);
}
I want to avoid creating a new list in each iteration of the loop. Is that possible?

You can use the List.addAll() method. It accepts a Collection as an argument, and your set is a Collection.
List<String> mainList = new ArrayList<String>();
mainList.addAll(set);
EDIT: as respond to the edit of the question.
It is easy to see that if you want to have a Map with Lists as values, in order to have k different values, you need to create k different lists.
Thus: You cannot avoid creating these lists at all, the lists will have to be created.
Possible work around:
Declare your Map as a Map<String,Set> or Map<String,Collection> instead, and just insert your set.

Use constructor to convert it:
List<?> list = new ArrayList<>(set);

Also from Guava Collect library, you can use newArrayList(Collection):
Lists.newArrayList([your_set])
This would be very similar to the previous answer from amit, except that you do not need to declare (or instanciate) any list object.

We can use following one liner in Java 8:
List<String> list = set.stream().collect(Collectors.toList());
Here is one small example:
public static void main(String[] args) {
Set<String> set = new TreeSet<>();
set.add("A");
set.add("B");
set.add("C");
List<String> list = set.stream().collect(Collectors.toList());
}

the simplest solution
I wanted a very quick way to convert my set to List and return it, so in one line I did
return new ArrayList<Long>(mySetVariable);

Since it hasn't been mentioned so far, as of Java 10 you can use the new copyOf factory method:
List.copyOf(set);
From the Javadoc:
Returns an unmodifiable List containing the elements of the given Collection, in its iteration order.
Note that this creates a new list (ImmutableCollections$ListN to be precise) under the hood by
calling Collection#toArray() on the given set and then
putting these objects into a new array.

I would do :
Map<String, Collection> mainMap = new HashMap<String, Collection>();
for(int i=0; i<something.size(); i++){
Set set = getSet(...); //return different result each time
mainMap.put(differentKeyName,set);
}

You could use this one line change: Arrays.asList(set.toArray(new Object[set.size()]))
Map<String, List> mainMap = new HashMap<String, List>();
for(int i=0; i<something.size(); i++){
Set set = getSet(...);
mainMap.put(differentKeyName, Arrays.asList(set.toArray(new Object[set.size()])));
}

Java 8 provides the option of using streams and you can get a list from Set<String> setString as:
List<String> stringList = setString.stream().collect(Collectors.toList());
Though the internal implementation as of now provides an instance of ArrayList:
public static <T>
Collector<T, ?, List<T>> toList() {
return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add,
(left, right) -> { left.addAll(right); return left; },
CH_ID);
}
but JDK does not guarantee it. As mentioned here:
There are no guarantees on the type, mutability, serializability, or
thread-safety of the List returned; if more control over the returned
List is required, use toCollection(Supplier).
In case you want to be sure always then you can request for an instance specifically as:
List<String> stringArrayList = setString.stream()
.collect(Collectors.toCollection(ArrayList::new));

For the sake of completeness...
Say that you really do want to treat the Map values as Lists, but you want to avoid copying the Set into a List each time.
For instance, maybe you are calling one library function that creates a Set, but you are passing your Map<String, List<String>> result to a (poorly-designed but out of your hands) library function that only takes Map<String, List<String>>, even though somehow you know that the operations it does with the Lists are equally applicable to any Collection (and thus any Set). And for some reason you need to avoid the speed/memory overhead of copying each Set to a List.
In this super niche case, depending on the (maybe unknowable) behavior the library function needs out of your Lists, you may be able to create a List view over each Set. Note that this is inherently unsafe (because the library function's requirements from each List could presumably change without you knowing), so another solution should be preferred. But here's how you'd do it.
You'd create a class that implements the List interface, takes a Set in the constructor and assigns that Set to a field, and then uses that internal Set to implement the List API (to the extent possible, and desired).
Note that some List behavior you simply will not be able to imitate without storing the elements as a List, and some behavior you will only partially be able to imitate. Again, this class is not a safe drop-in replacement for Lists in general. In particular, if you know that the use case requires index-related operations or MUTATING the List, this approach would go south very fast.
public class ListViewOfSet<U> implements List<U> {
private final Set<U> wrappedSet;
public ListViewOfSet(Set<U> setToWrap) { this.wrappedSet = setToWrap; }
#Override public int size() { return this.wrappedSet.size(); }
#Override public boolean isEmpty() { return this.wrappedSet.isEmpty(); }
#Override public boolean contains(Object o) { return this.wrappedSet.contains(o); }
#Override public java.util.Iterator<U> iterator() { return this.wrappedSet.iterator(); }
#Override public Object[] toArray() { return this.wrappedSet.toArray(); }
#Override public <T> T[] toArray(T[] ts) { return this.wrappedSet.toArray(ts); }
#Override public boolean add(U e) { return this.wrappedSet.add(e); }
#Override public boolean remove(Object o) { return this.wrappedSet.remove(o); }
#Override public boolean containsAll(Collection<?> clctn) { return this.wrappedSet.containsAll(clctn); }
#Override public boolean addAll(Collection<? extends U> clctn) { return this.wrappedSet.addAll(clctn); }
#Override public boolean addAll(int i, Collection<? extends U> clctn) { throw new UnsupportedOperationException(); }
#Override public boolean removeAll(Collection<?> clctn) { return this.wrappedSet.removeAll(clctn); }
#Override public boolean retainAll(Collection<?> clctn) { return this.wrappedSet.retainAll(clctn); }
#Override public void clear() { this.wrappedSet.clear(); }
#Override public U get(int i) { throw new UnsupportedOperationException(); }
#Override public U set(int i, U e) { throw new UnsupportedOperationException(); }
#Override public void add(int i, U e) { throw new UnsupportedOperationException(); }
#Override public U remove(int i) { throw new UnsupportedOperationException(); }
#Override public int indexOf(Object o) { throw new UnsupportedOperationException(); }
#Override public int lastIndexOf(Object o) { throw new UnsupportedOperationException(); }
#Override public ListIterator<U> listIterator() { throw new UnsupportedOperationException(); }
#Override public ListIterator<U> listIterator(int i) { throw new UnsupportedOperationException(); }
#Override public List<U> subList(int i, int i1) { throw new UnsupportedOperationException(); }
}
...
Set<String> set = getSet(...);
ListViewOfSet<String> listOfNames = new ListViewOfSet<>(set);
...

I create simple static method:
public static <U> List<U> convertSetToList(Set<U> set)
{
return new ArrayList<U>(set);
}
... or if you want to set type of List you can use:
public static <U, L extends List<U>> List<U> convertSetToList(Set<U> set, Class<L> clazz) throws InstantiationException, IllegalAccessException
{
L list = clazz.newInstance();
list.addAll(set);
return list;
}

Recently I found this:
ArrayList<T> yourList = Collections.list(Collections.enumeration(yourSet<T>));

I found this working fine and useful to create a List from a Set.
ArrayList < String > L1 = new ArrayList < String > ();
L1.addAll(ActualMap.keySet());
for (String x: L1) {
System.out.println(x.toString());
}

You convert Set to List without adding ordering information (like sorting) just to store it in the map.
Because Set is unordered and no ordering information is added, List should not be used, as it will contain randomly ordered data and all it's methods that are related to ordered data will be ambiguous.
You should use Collection interface instead, that accepts both Set and List in the map. This way, no additional memory is required as you use polymorphism instead of copying data.
Map<String, Collection<String>> mainMap = new HashMap<>();
for (int i=0; i < something.size(); i++) {
Set<String> set = getSet(...); //returns different result each time
mainMap.put(differentKeyName, set);
}
Disclaimer: my edit to a similar answer was rejected so I added my own answer with additional information

Map<String, List> mainMap = new HashMap<String, List>();
for(int i=0; i<something.size(); i++){
Set set = getSet(...); //return different result each time
mainMap.put(differentKeyName, new ArrayList(set));
}

Related

A sorted Collection with multiple instances but unique object identity

I was looking for something implementing the Collection interface where I can add multiple instances of the same object (based on the given Comparator), but the collection cannot contain twice the same object identity (based on the == operator). The collection has to be sorted and I must be able to remove one particular element (based on the == operator).
In other words, it has to satisfy the following testcase :
public MyCollection<E> implements Collection<E>
{ ... }
public class MyCollectionTest extends TestCase
{
static class MyComparator implements Comparator<MyInterface>
{
#Override
public int compare(MyInterface pO1, MyInterface pO2)
{
// the return type of getCategory() is just an enum.
return pO1.getCategory().ordinal() - pO2.getCategory().ordinal();
}
}
public void testAdd()
{
MyCollection<MyInterface> sut = new MyCollection<MyInterface>(new MyComparator());
MyInterface svc1 = EasyMock.createNiceMock(MyInterface.class);
MyInterface svc2 = EasyMock.createNiceMock(MyInterface.class);
EasyMock.expect(svc1.getCategory()).andStubReturn(Category.CAR);
EasyMock.expect(svc2.getCategory()).andStubReturn(Category.VAN);
EasyMock.replay(svc1, svc2);
sut.add(svc1);
sut.add(svc2);
assertEquals(2, sut.size());
assertSame(svc1, sut.last());
assertSame(svc2, sut.first());
}
public void testAddDouble()
{
MyCollection<MyInterface> sut = new MyCollection<MyInterface>(new MyComparator());
MyInterface svc1 = EasyMock.createNiceMock(MyInterface.class);
EasyMock.expect(svc1.getCategory()).andStubReturn(Category.CAR);
sut.add(svc1);
sut.add(svc1);
assertEquals(1, sut.size());
}
public void testRemove()
{
MyCollection<MyInterface> sut = new MyCollection<MyInterface>(new MyComparator());
MyInterface svc1 = EasyMock.createNiceMock(MyInterface.class);
MyInterface svc2 = EasyMock.createNiceMock(MyInterface.class);
EasyMock.expect(svc1.getCategory()).andStubReturn(Category.VAN);
EasyMock.expect(svc2.getCategory()).andStubReturn(Category.VAN);
EasyMock.replay(svc1, svc2);
sut.add(svc1);
sut.add(svc2);
assertEquals(2, sut.size());
sut.remove(svc1);
assertEquals(1, sut.size());
}
}
Any help ?
Thank you!
EDIT: Actually I think this can be solved with just new TreeSet<>(Ordering.natural().thenComparing(Ordering.arbitrary())) (with Guava's Ordering)
If you don't have Guava, you could roll your own using TreeMap and IdentityHashMap, something like:
public class IdentityTreeSet<T extends Comparable> extends AbstractCollection<T> {
private SortedMap<T, Set<T>> values = new TreeMap<>();
#Override
public Iterator<T> iterator() {
return new Iterator<T>() {
Iterator<Set<T>> outerIterator = values.values().iterator();
Set<T> currentSet = Collections.newSetFromMap(new IdentityHashMap<>());
Iterator<T> innerIterator = currentSet.iterator();
#Override
public boolean hasNext() {
return innerIterator.hasNext() || outerIterator.hasNext();
}
#Override
public T next() {
if (innerIterator.hasNext()) {
return innerIterator.next();
} else {
currentSet = outerIterator.next();
innerIterator = currentSet.iterator();
return next();
}
}
#Override
public void remove() {
innerIterator.remove();
if (currentSet.isEmpty()) {
outerIterator.remove();
}
}
};
}
#Override
public int size() {
int i = 0;
for (Set<T> set : values.values()) {
i += set.size();
}
return i;
}
#Override
public boolean add(T e) {
Set<T> entrySet = values.get(e);
if (entrySet == null) {
Set<T> newSet = Collections.newSetFromMap(new IdentityHashMap<>());
newSet.add(e);
values.put(e, newSet);
return true;
} else {
return entrySet.add(e);
}
}
#Override
public boolean remove(Object o) {
Set<T> entrySet = values.get(o);
if (entrySet == null) {
return false;
} else {
boolean removed = entrySet.remove(o);
if (entrySet.isEmpty()) {
values.remove(o);
}
return removed;
}
}
}
Note that the documentation for Collection.remove is written in terms of equals, so this class cannot strictly adhere to the Collection contract, and may cause errors if passed as a Collection to code you don't control.
If no existing collection does exactly what you want, consider the following strategy:
Define a class whose public methods do exactly what you need, no more and no less.
Implement the class using existing collections to take care of the busy work, but with your code in control to impose your requirements.
For example, your class might have a TreeSet each of whose elements is a non-empty IdentityHashMap of the underlying class. The TreeSet comparator would pull an element from each IdentityHashMap and return the result of comparing them.
To remove an element, first check whether removing it would empty its IdentityHashMap (it is present, and the set size is 1). If so, remove the IdentityHashMap from the TreeSet. If not, remove the element from its IdentityHashMap.
This is only an outline, with a lot of details that need to be filled in. The main point is the idea of building exactly what you want, based on what already exists wrapped in a class you write.
Concerning this part of your question "but the collection cannot contain twice the same object identity (based on the == operator)", if you want two objects to be both equal by equals and == operator, you need to read about instance controlled objects (basically objects that hash their own instances, and return you the same cached object instead of creating a new one when the object being requested already exists).

Which data structure in Java should I use for storing key value pairs in ordered form? No duplicates [duplicate]

This question already has answers here:
java collection that has key/value pair, and is ordered according to insert order
(4 answers)
Closed 10 years ago.
I am working on a project and I need to store key value pairs (one-to-one mapping) in an ordered fashion. Then I should be able to retrieve the key using the value and value using the key. I have looked at Maps, Sets and Hash Tables, but they aren't ordered.
Also, though trivial, it would be great if we could DS retrieve the keys and values at once i.e., the interface supports such functions.
EDIT: The keys and values are all unique. Maintaining the inserted order is good enough.
Notice that you don't define what counts as "ordered". A LinkedHashMap enables iterating over the keys (and therefore values) in insertion-order. Conversely, a TreeMap lets you specify a sort order with a comparator, and ensures all items added to the map are stored in sorted order. 99 times out of 100, one of these classes should be all you need. Alternatively, Google's Guava project has several very nice BiMap implementations that you may find fits your needs.
I strongly caution you: if you think you need more than what these classes can provide, you are likely over-engineering your problem.
For reasons I can't fully justify, I implemented a proper UniqueOrderedBiMap for you, which is compatible with the Java Collections framework and all implemented functions run efficiently. You can use whatever underlying map you see fit (including an un-ordered map, if you really wanted) and keys and values are always unique. Notice that it is a very thin wrapper around a LinkedHashMap, because that's all you need, a LinkedHashMap with extra checks to ensure Values remain unique.
For the curious, check this answers revision history for a UniqueOrderedMap which lacks the getKey() and removeKey() methods, but more properly implements the Map interface, and only needs a HashSet, rather than a HashMap, to store the known values.
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class UniqueOrderedBiMap<K, V>implements Map<K, V> {
private Map<K, V> orderedMap;
private HashMap<V, K> valueMap;
public UniqueOrderedBiMap() {
this(new LinkedHashMap<K,V>());
}
public UniqueOrderedBiMap(Map<K, V> underlyingMap) {
orderedMap = underlyingMap;
valueMap = new HashMap<V, K>(orderedMap.size());
for(Map.Entry<K, V> e : orderedMap.entrySet()) {
if(!valueMap.containsKey(e.getValue())) { // Duplicate value
// could instead fail softly by removing the associated item from the map, but this seems cleaner/clearer.
// generally this constructor should be passed an empty map anyways
throw new IllegalArgumentException("Duplicate value "+e.getValue()+" found in underlying map.");
}
valueMap.put(e.getValue(), e.getKey());
}
}
#Override
public int size() {
return orderedMap.size();
}
#Override
public boolean isEmpty() {
return orderedMap.isEmpty();
}
#Override
public boolean containsKey(Object key) {
return orderedMap.containsKey(key);
}
#Override
public boolean containsValue(Object value) {
// more efficient than iterating over the map
return valueMap.containsKey(value);
}
#Override
public V get(Object key) {
return orderedMap.get(key);
}
public K getKey(V value) {
return valueMap.get(value);
}
// Likely want to implement a forcePut(K, V) method like Guava's BiMaps do
#Override
public V put(K key, V value) {
if(valueMap.containsKey(value)) {
throw new IllegalArgumentException("Cannot insert non-unique value "+value);
}
V ret = orderedMap.put(key, value);
valueMap.remove(ret);
valueMap.put(value, key);
return ret;
}
#Override
public V remove(Object key) {
V ret = orderedMap.remove(key);
valueMap.remove(ret);
return ret;
}
public K removeKey(V value) {
K ret = valueMap.remove(value);
orderedMap.remove(ret);
return ret;
}
#Override
public void putAll(Map<? extends K, ? extends V> m) {
// Existing Map implementation's putAll have some optimizations we
// could take advantage of, but this isn't unreasonable for a first pass
for(Entry<? extends K, ? extends V> e : m.entrySet()) {
put(e.getKey(), e.getValue());
}
}
#Override
public void clear() {
orderedMap.clear();
valueMap.clear();
}
#Override
public Set<K> keySet() {
return orderedMap.keySet();
}
#Override
public Collection<V> values() {
return orderedMap.values();
}
#Override
public Set<java.util.Map.Entry<K, V>> entrySet() {
return orderedMap.entrySet();
}
#Override
public boolean equals(Object o) {
if(o instanceof UniqueOrderedBiMap) {
UniqueOrderedBiMap<?,?> map = (UniqueOrderedBiMap<?,?>)o;
return orderedMap.equals(map.orderedMap);
}
return false;
}
#Override
public int hashCode() {
return orderedMap.hashCode();
}
#Override public String toString() {
return orderedMap.toString();
}
public static void main(String[] args) {
String[] names = { "Marcus", "Jim", "Tom", "Sam" };
String[] grades = { "A", "B", "D", "F" };
UniqueOrderedBiMap<String,String> insertionMap = new UniqueOrderedBiMap<>();
UniqueOrderedBiMap<String,String> sortedMap = new UniqueOrderedBiMap<>(new TreeMap<String,String>());
for(int i = 0; i < names.length; i++) {
insertionMap.put(names[i], grades[i]);
sortedMap.put(names[i], grades[i]);
}
// Poor man's assert
System.out.println(insertionMap.toString().equals("{Marcus=A, Jim=B, Tom=D, Sam=F}"));
System.out.println(sortedMap.toString().equals("{Jim=B, Marcus=A, Sam=F, Tom=D}"));
insertionMap.put("Tom", "C");
sortedMap.put("Tom", "C");
System.out.println(insertionMap.toString().equals("{Marcus=A, Jim=B, Tom=C, Sam=F}"));
System.out.println(sortedMap.toString().equals("{Jim=B, Marcus=A, Sam=F, Tom=C}"));
try {
insertionMap.put("Sam", "C");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
try {
sortedMap.put("Sam", "C");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
insertionMap.remove("Tom");
sortedMap.remove("Tom");
insertionMap.put("Sam", "C");
sortedMap.put("Sam", "C");
System.out.println(insertionMap.toString().equals("{Marcus=A, Jim=B, Sam=C}"));
System.out.println(sortedMap.toString().equals("{Jim=B, Marcus=A, Sam=C}"));
insertionMap.removeKey("A");
sortedMap.removeKey("A");
System.out.println(insertionMap.toString().equals("{Jim=B, Sam=C}"));
System.out.println(sortedMap.toString().equals("{Jim=B, Sam=C}"));
}
}
If you can use third party libraries then consider using an ImmutableBiMap. Its a Guava Collection class that provides
User specified iteration order
Normal mapping from keys to values and inverse mapping from values to keys
The one consideration is that once created the map is immutable and cannot be modified.
LinkedHashMap should be suitable for you. Read through the link
You will need two LinkedHashMap. You can create custom class that internally uses two LinkedHashMap. One for mapping keys to value and another one for mapping values to key.

Java retainAll based on Enum value

I have a list of objects which all have a enum value called AssetType, is it possible to use the retainAll() method to sort the list so that only objects with AssetType.BANK_ACCOUNT is kept?
Thx in advance for any help.
(Disclosure: I contribute to Guava.)
A somewhat more intuitive Guava-based implementation would be
Iterables.removeIf(allAssets, new Predicate<MyObject>() {
public boolean apply(MyObject asset) {
return asset.getAssetType() != AssetType.BANK_ACCOUNT;
}
});
...That said, honestly I would prefer the dumb, plain-Java implementation:
Iterator<MyObject> itr = allAssets.iterator();
while (itr.hasNext()) {
if (itr.next().getAssetType() != AssetType.BANK_ACCOUNT) {
itr.remove();
}
}
If you pull in Guava, you can make a live transform between the object and its AssetType, and then call retainAll on that:
Lists.transform(allAssets, assetTypeFn).retainAll(
Collections.singleton(AssetType.BANK_ACCOUNT));
//...elsewhere...
public static final Function<MyObject, AssetType> assetTypeFn =
new Function<MyObject, AssetType>() {
public AssetType apply(MyObject object) {
return object.getAssetType();
}
};
Similarly you can use the filter() method if you don't want to change the original list:
List<MyObject> bankAccounts = Lists.newArrayList(
Iterables.filter(allAssets, isBankAccount));
public static final Predicate<MyObject> isBankAccount = new Predicate<MyObject>() {
public boolean apply(MyObject asset) {
return asset.getAssetType() == AssetType.BANK_ACCOUNT;
}
}

How to implement a fixed size "list" in Java?

Since the Java core library doesn't have such a collection, would an array be the best option, especially if one doesn't want to rely on third-party libraries?
Arrays.asList(T ...) Returns a fixed-size list backed by the specified array
Object[] array = new Object[10];
List<Object> fixedList = Arrays.asList(array);
You could use an array, or an ArrayList<E> pre-initialized with the desired size.
If you want to actively prevent the expansion of the list, then using an array is probably the easiest.
I'd write a wrapper class around an ArrayList, and in the add and addAll methods, I'd check for the list's size before adding new elements. If you have reached the maximum size, you can then throw an exception (or do nothing, depending on what you really want to do in your code).
Here's a short example:
public class SizeLimitedArray<E> implements java.util.List<E>
{
private static final int DEFAULT_SIZE_LIMIT = 10;
private ArrayList<E> myList;
private int maxSize;
public SizeLimitedArray ()
{
this (DEFAULT_SIZE_LIMIT);
}
public SizeLimitedArray (int size)
{
myList = new ArrayList<E> (size);
maxSize = size;
}
#Override
public boolean add (E objectToAdd)
{
if (myList.size () > maxSize)
{
throw new IllegalStateException ("The array is full");
}
return myList.add (objectToAdd);
}
#Override
public boolean addAll (Collection collectionToAdd)
{
if (myList.size () + collectionToAdd.size () > maxSize)
{
throw new IllegalStateException ("The array is full");
}
return myList.addAll (collectionToAdd);
}
// Rest of class omitted for brevity
}
Just implement your own. You could use a proxy-based approach. Define your own list that is backed by an ArrayList. Make the internal list private. Also implement a simple limit field that has a default and also can be set via a constructor.
Your list will implement List, and for every method that modifies the internal list, increment and decrements the count appropriately. If the size exceeds the limit, throw some sort of exception. Something like
public class FixedList implements List {
private ArrayList<E> list = new ArrayList<E>();
private int limit = 10; // default
FixedList(){} // default constructor
FixedList(int limit) {
this.limit = limit;
}
public boolean add(E object) {
if (this.list.size() == limit - 1) {
// throw some sort of LimitExceeded Runtime Exception
}
this.list.add(object);
}
...
}
You will have to work on the generics, and remember to support the cases where multiple things are added at once addAll.
Well you could inherit from class ArrayList for example and reimplement the add method to not be able to add past a given amount of elements. Or, even better as pointer out by Laf, use composition:
public class MyArrayList<T> {
private ArrayList<T> innerList;
private int maxSize;
public boolean add(T item) {
if(innerList.size() < maxSize) {
return innerList.add(item);
} else {
return false;
}
}
}
Use Collections.unmodifiableList(List<T> list). This will return a generic List<T> object which throws UnsupportedOperationException if you attempt to add (or remove) elements.
I will probably get burned, but you can also use an ArrayBlockingQueue for this. Which provides the benefit of being able to use regular Collection methods.
public class libsystem extends javax.swing.JFrame {
//public static ArrayList<books> al = new ArrayList<books>();
public static List<books> al = Arrays.asList(new books[100]);
public libsystem() {
initComponents();
}
String msg =jTextArea1.getText();
try {
FileWriter fs=new FileWriter("library.txt");
try(
BufferedWriter out= new BufferedWriter(fs)){;
out.write(msg);
}
} catch (Exception e){
System.err.println("wrong" + e.getMessage());
}
String line;
String id,name,type;
try{
FileReader in=new FileReader("library.txt");
try (BufferedReader br = new BufferedReader(in)) {
while((line=br.readLine())!=null){
StringTokenizer st1 = new StringTokenizer(line,",");
while(st1.hasMoreTokens()){
id=st1.nextToken();
name=st1.nextToken();
type=st1.nextToken();
books book=new books(id,name,type);
al.add(book);
}
br.close();
for(int i=0;i<al.size();i++){
books obj = al.get(i);
System.out.println("Book NAme :"+obj.getName()+ "\n" +" Name:"+obj.getAuthor()+ "\n"+"Type :"+obj.getSubject()+"\n");
}

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