I have an object list that retrieves multiple values from a database, like so:
List<Object> listRequest = daoManager.getLaptopsForRequest(BigInteger.valueOf(itemTransItem.getAssetType().getId()), BigInteger.valueOf(approverId));
The result of which looks like this (printed out on the console, via for each):
{asset_type_id=1, inventory_id=1, from_dt=2015-09-18 18:04:55.77, id=1, asset_id=1, status=1, thru_dt=null}
{asset_type_id=1, inventory_id=1, from_dt=2015-09-18 18:04:55.77, id=2, asset_id=2, status=1, thru_dt=null}
{asset_type_id=1, inventory_id=1, from_dt=2015-09-18 18:04:55.77, id=3, asset_id=3, status=1, thru_dt=null}
What's the quickest and/or most efficient way to get only the object where asset_id = 2, or an array of asset_id (1 and 2), and putting the results in another array?
I contemplated casting each object as a string, and then turning each string into an array (split by the comma), and then turning each item of the array into a further array (or a hashmap) by using the =, but that seems like a long, long, complex way of nested for loops that might fail (see comparing array of assets).
Perhaps there's another quicker / less complex way to do this that I'm missing? Any suggestions? Thanks.
EDIT: For reference, here's the getLaptopsForRequest function:
public List getLaptopsForRequest(BigInteger asset_type_id, BigInteger party_id){
SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(laptopsForRequestSql);
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List forRequest = query.setBigInteger(0, asset_type_id).setBigInteger(1, party_id).list();
return forRequest;
}
It returns a list of the results of the query. As this code has been in place, I'm not allowed to edit it.
A quick and dirty solution would be to match each item against regex ^.*asset_id=([0-9]+).*$.
If what you're getting from that method is indeed a list of Strings containing those JSONs, you could create a model class and use a JSON serializer like GSON or Jackson to read the strings into Java objects, and then you could work with them.
What you are trying to do basically is to filter a list of objects. You could implement the Filter Pattern writing your own Iterator for the list.
Just extends this class to implement your own filter.
public abstract class Filter<T> {
public abstract boolean passes(T object);
public Iterator<T> filter(Iterator<T> iterator) {
return new FilterIterator(iterator);
}
public Iterable<T> filter(Iterable<T> iterable) {
return new Iterable<T>() {
public Iterator<T> iterator() {
return filter(iterable.iterator());
}
};
}
private class FilterIterator implements Iterator<T> {
private Iterator<T> iterator;
private T next;
private FilterIterator(Iterator<T> iterator) {
this.iterator = iterator;
toNext();
}
public boolean hasNext() {
return next != null;
}
public T next() {
if (next == null)
throw new NoSuchElementException();
T returnValue = next;
toNext();
return returnValue;
}
public void remove() {
throw new UnsupportedOperationException();
}
private void toNext() {
next = null;
while (iterator.hasNext()) {
T item = iterator.next();
if (item != null && passes(item)) {
next = item;
break;
}
}
}
}
}
and then use it in this way:
List<MyObject> newList = new ArrayList<MyObject>();
for(MyObject obj : filter.filter(listObjs) ){
newList.add(obj);
}
Assuming your objects have getter et setter methods.
Only the object where asset_id = "2", asset_id here being a string
listRequest.stream().filter(e -> e.getAssetId() == "2" ).toArray();
Related
So I have a LinkedHashSet , with values say a1, a2, , b, c1, c2
I want to replace, b with x , such that the order of x should be same as order of b.
One obvious way would be
private LinkedHashSet<String> orderedSubstitution(final Set<String> originalOrderedSet, final String oldItem,
final String newItem) {
final LinkedHashSet<String> newOrderedSet = new LinkedHashSet<String>();
// Things we do to maintain order in a linkedHashSet
for (final String stringItem : originalOrderedSet) {
if (stringItem.equals(oldItem)) {
newOrderedSet.add(newItem);
} else {
newOrderedSet.add(stringItem);
}
}
return newOrderedSet;
}
not only this is O(n) i also feel this is not the fastest way. Any better solution ?
NOTE : I HAVE TO use linkedHashMap.
One way to do it would be to use a subclass of LinkedHashSet that has the replacement built in, e.g.:
public class ReplacingLinkedHashSet extends LinkedHashSet<String> {
private final String what;
private final String with;
public ReplacingLinkedHashSet(String what, String with) {
this.what = what;
this.with = with;
}
#Override
public Iterator<String> iterator() {
final Iterator<String> iterator = super.iterator();
return new Iterator<String>() {
#Override
public boolean hasNext() {
return iterator.hasNext();
}
#Override
public String next() {
String next = iterator.next();
return what.equals(next) ? with : next;
}
#Override
public void remove() {
iterator.remove();
}
};
}
}
But that means the replacement would have to be known before you fill the Set.
(Of course you could easily turn this <String> version into a generic one.
Responding to comments:
OK, then there is no way to solve it without a full iteration. You could however just leave the LinkedHashSet untouched and decorate the iterator when retrieving the values.
Create a structure Map
Insert all the string with < String, OrderOfTheString>
Do the insertion of the new String by adding a small Delta after the current string's OrderOfTheString.
Convert Map to LikedHashSet
I know it is complicated but it is definately better when we have linked hash Map of ~1000000 elements and there are about 1000 elements to be inserted.
ArrayList searchList = new ArrayList();
ArrayList words=(ArrayList) request.getSession().getAttribute("words");
words.add("one");
words.add("twenty one");
words.add("thirty one");
words.add("two");
words.add("twenty two");
words.add("thirty two");
words.add("three");
words.add("twenty three");
words.add("thirty three");'
If I have this arraylist and I want to search all the strings containing one(i.e. one,twenty one and thirty one), what logic should I use? Means how should I do that?
for (String item : searchList) {
if (item.contains("one") {
// Do something. Like adding the result to a different list.
// If you need the index from the original list, you a for instead of a for each
}
}
//iterate through words
for(String str : list){
//check if word contains the key
if(str.contains(key)){
//add its reference to another resultant list
result.add(str);
}
}
for (String word : words) {
if (word.contains("one")) {
//we have a match
}
}
Of course you have to loop thru the elements. Look for ways to loop thru an ArrayList: that can be indexed or with the
for (x : collect)
notation.
In the loop you have to do some pattern matching. Read String Java API doc for a method.
(Give'em some think food ...)
You could solve this using iterators if the condition will be more complex
public interface IPredicate<T> {
boolean check(T t);
}
public class PredicatIterable<T> implements Iterable<T> {
private final Iterator<T> iterator;
private final IPredicate<T> predicate;
public PredicatIterable(Iterable<T> iterable, IPredicate<T> predicate) {
this.iterator = iterable.iterator();
this.predicate = predicate;
}
#Override
public Iterator<T> iterator() {
return new Iterator<T>() {
T current;
#Override
public boolean hasNext() {
if(iterator.hasNext()) {
T next = iterator.next();
if(predicate.check(next)) {
current = next;
return true;
}
current = null;
}
return false;
}
#Override
public T next() {
return current;
}
#Override
public void remove() {
throw new RuntimeException("Invalid useage of method");
}
};
}
}
To validate more the single predicate you can create also method that is responsible for conuntion or alternative of two IPredicate argument.
In general, when searching an item in a List, the best solution is to sort your List first using Collections.sort() method. Then using the Collections.binarySearch() method, find your element.
In this case your elements are String type that are Comparable and can be sorted alphabetically otherwise you needed to implement Comparable interface for your element class type.
Okay, so I'm supposed to implement a set that contains elements of type Object with no duplicates which means that I need to compare each new element with the previous elements in the set. The set has its own class and has a method for inserting a new element.
My question is: How do I use the iterator I wrote below to compare all the entries in the set with the proposed element to add?
class SetIterator implements MyIterator {
private ArraySet arr; //ArraySet is the name of the Set class
private int n;
SetIterator(ArraySet myArraySet)
{
arr = myArraySet;
n = 0;
}
#Override
public boolean hasNext()
{
return (n <= arr.size());
}
#Override
public Object next()
{
if (hasNext())
return arr[n++];
}
}
Thanks!
You'd need something like this in ArraySet.java.
public Iterator iterator()
{
return new SetIterator(this);
}
public boolean add(Object o)
{
for (Object item : this)
if (o.equals(next)) return false;
}
// add code to put o in the array
return true;
}
The for loop is translated by the compiler to something like this:
Iterator it = this.iterator();
while (it.hasNext())
{
Object item = it.next();
if (o.equals(next)) return false;
}
The implementation of Set should guarantee no-duplication rather than the Iterator.
That is, your Set.add() and its constructor with Collection as argument should guarantee no-duplication.
The Iterator just implements hasNext(), next(), and remove().
If Iterator takes care of duplication checking, your Set will violate java.util.Set contract. Moreover, the Iterator will take two responsibilities—checking duplication and traversing, that violates “Single Responsibily Principle”.
Yes, I just want an example on how to use that iterator instead of a for loop or a while loop.
You pretty much have to use some kind of loop to use a Iterator. Here is the basic pattern:
Iterator it = ... // instantiate the iterator
while (it.hasNext()) {
Object obj = it.next();
... // do something with obj
}
If the collection object (e.g. your set of objects) implements Iterable then you can use the new for loop syntax; e.g.
for (Object obj : yourSet) {
... // do something with obj
}
Of course, there are other ways to express this, but they all involve (somewhere) a loop of some kind to pull the objects from the iterator.
I still have a question about Enumerations. Here's a quick sketch of the situation.
I have a class Backpack that has a Hashmap content with as keys a variable of type long, and as value an ArrayList with Items.
I have to write an Enumeration that iterates over the content of a Backpack. But here's the catch: in a Backpack, there can also be another Backpack. And the Enumeration should also be able to iterate over the content of a backpack that is in the backpack. (I hope you can follow, I'm not really good at explaining..)
Here is the code I have:
public Enumeration<Object> getEnumeration() {
return new Enumeration<Object>() {
private int itemsDone = 0;
//I make a new array with all the values of the HashMap, so I can use
//them in nextElement()
Collection<Long> keysCollection = getContent().keySet();
Long [] keys = keysCollection.toArray(new Long[keysCollection.size()]);
public boolean hasMoreElements() {
if(itemsDone < getContent().size()) {
return true;
}else {
return false;
}
}
public Object nextElement() {
ArrayList<Item> temporaryList= getContent().get(keys[itemsDone]);
for(int i = 0; i < temporaryList.size(); i++) {
if(temporaryList.get(i) instanceof Backpack) {
return temporaryList.get(i).getEnumeration();
}else {
return getContent().get(keys[itemsDone++]);
}
}
}
};
Will this code work decently? It's just the "return temporaryList.get(i).getEnumeration();" I'm worried about. Will the users still be able to use just the hasMoreElemens() and nextElement() like he would normally do?
Any help is appreciated,
Harm De Weirdt
You need to create a Stack<Enumeration<Object>>. When you see another Backpack, you push a new Enumeration on that element into the Stack. You always nextElement() from the top of the stack. If the top element is empty, you pop it off. Repeat until the Stack.isEmpty().
Another perhaps simpler technique (depending on how comfortable you are with recursion) is to use "inner" enumerations, which itself can have inner enumerations. Here's a code sample using Iterator on Object[]. It recursively iterates into any nested Object[].
public class RecursiveIterator implements Iterator<Object> {
final Object[] arr;
int index = 0;
Iterator<Object> inner;
RecursiveIterator(Object[] arr) {
this.arr = arr;
}
#Override public boolean hasNext() {
while (true) {
if (inner != null) {
if (inner.hasNext()) {
return true;
} else {
inner = null;
index++;
}
}
if (index == arr.length) return false;
if (arr[index] instanceof Object[]) {
inner = new RecursiveIterator((Object[]) arr[index]);
} else {
return true;
}
}
}
#Override public Object next() {
if (!hasNext()) throw new NoSuchElementException();
return (inner != null) ? inner.next() : arr[index++];
}
#Override public void remove() {
throw new UnsupportedOperationException();
}
}
Here's a test harness:
static void dump(Object[] arr) {
Iterator<Object> iter = new RecursiveIterator(arr);
while (iter.hasNext()) {
System.out.print(iter.next() + " ");
}
System.out.println("(done)");
}
public static void main(String[] args) throws FileNotFoundException {
dump(new Object[] {
1,
2,
new Object[] {
3,
new Object[] { 4 },
5,
},
6,
new Object[] {},
7,
});
dump(new Object[] {
new Object[] {},
new Object[] {
new Object[] {
new Object[] {},
},
},
new Object[] {},
new Object[] { null },
});
}
This prints:
1 2 3 4 5 6 7 (done)
null (done)
If it was for practical purposes and not homework, I would use the Iterator interface instead of the old Enumeration. For iterators you have some nice utilities in the apache collections project.
Secondly, your solution seems to have a bug. The method nextElement() should return the elements themselves, but the line return temporaryList.get(i).getEnumeration() returns an Enumeration object instead.
--EDIT--
polygenelubricants suggested a nice, elegant solution. I thought of something else. You can implement a generic ChainEnumeration class (implements Enumeration), that receives a list of Enumerations and allows enumerating the underlying items. In your tree structure, return a simple enumeration with one item for leafs, and a chain enumeration of inner nodes.
The implementation of ChainEnumeration is simple: it manages a list of iterators, plus a reference to the current active iterator, where items are taken from.
I am a newbie, I have a question.
I have a map. I have to loop through the map and build the iterator.
Example:
public Iterable<Test> getTests(Map<String, Test> testMap,
Set<String> strings)
{
//loop tru the set of strings and build iterator.
for(final String test1 : strings)
{
Test test = testMap.get(test1);
//build a iterator. not a list.
}
return iterator
}
How can I do this?
First of all, your method is returning an Iterable, not an Iterator. Map, Set, and List all implement Iterable, so it might be easier than you think.
Second, an Iterable is merely a class that has an iterator() method which returns an Iterator.
So I would simply build a List of the results and then return it. If you really want to return an Iterator, I would instead call iterator() on the list and return that.
Skipping over your Iterator/Iterable confusion (and Iterable is basically an Iterator factory... so you need to write an Iterator either way), I think you mean something like this:
Iterator<Test> getTests(final Map<String,Test> testMap, final Set<String> strings) {
return new Iterator<Test>() {
private final Iterator<String> keyIter = strings.iterator();
private String lastKey;
public boolean hasNext() { return keyIter.hasNext(); }
public Test next() { lastKey = keyIter.next(); return testMap.get(lastKey); }
public void remove() { testMap.remove(lastKey); }
};
}
And if you want to return an Iterable, well, that just has to be a factory for those:
Iterable<Test> getTests(final Map<String,Test> testMap, final Set<String> strings) {
return new Iterable<Test>() {
public Iterator<Test> iterator() {
return new Iterator<Test>() {
private final Iterator<String> keyIter = strings.iterator();
private String lastKey;
public boolean hasNext() { return keyIter.hasNext(); }
public Test next() { lastKey = keyIter.next(); return testMap.get(lastKey); }
public void remove() { testMap.remove(lastKey); }
};
}
};
}
For extra credit, you can parameterise this method itself and have a generic way of iterating over a selection from a map:
Map<String, Action> map;
Set<String> keys;
for (Action x : filterMap(map, keys)) {
}
Since Collection extends Iterable, the correct solution would be to add the tests to some sort of collection, like an ArrayList.
Collection<Test> tests = new ArrayList<Test>();
for (String key : strings) {
Test t = testMap.get(key);
if (t != null)
tests.add(t);
}
return tests;
If you want to really return Iterable simplest way is this
public Iterable<Test> getTests(Map<String, Test> testMap, Set<String> strings)
{
testMap = new HashMap<String, Test>(testMap);
testMap.keySet().retainAll(strings);
return testMap.values();
}
If you want to return an iterator, replace the last line with
return testMap.values().iterator();
If you don't care about modifications to the passed in map, skip the initial temp map construction.
EDIT:
removed use of clone on the Map interface which doesn't have it.
As a stab in the dark, what you may be looking for is Map.entrySet.iterator(). It allows you to iterate over the entries of the map.
Note: I know this is not an answer to the OP's question, but it's my guess at answering what they are actually looking for. If this guess turns out to be wrong, I'll delete this answer again to spare people the confusion.
An Iterable is something different from an Iterator. An Iterable is something you can iterate through, like a List. You use an Iterator for that. Your questions is not clear about what you want to return from getTests().
Your best shot would be to create an Iterable (like a List or Vector) and return that, or return its Iterator.