Asked  6 Months ago    Answers:  5   Viewed   50 times

For example, lets say you have two classes:

public class TestA {}
public class TestB extends TestA{}

I have a method that returns a List<TestA> and I would like to cast all the objects in that list to TestB so that I end up with a List<TestB>.

 Answers

54

Simply casting to List<TestB> almost works; but it doesn't work because you can't cast a generic type of one parameter to another. However, you can cast through an intermediate wildcard type and it will be allowed (since you can cast to and from wildcard types, just with an unchecked warning):

List<TestB> variable = (List<TestB>)(List<?>) collectionOfListA;
Tuesday, June 1, 2021
 
huhushow
answered 6 Months ago
23

The sorted() function takes a key= parameter

newlist = sorted(list_to_be_sorted, key=lambda k: k['name']) 

Alternatively, you can use operator.itemgetter instead of defining the function yourself

from operator import itemgetter
newlist = sorted(list_to_be_sorted, key=itemgetter('name')) 

For completeness, add reverse=True to sort in descending order

newlist = sorted(l, key=itemgetter('name'), reverse=True)
Tuesday, June 1, 2021
 
phirschybar
answered 6 Months ago
27

You can use the fs.readdir or fs.readdirSync methods. fs is included in Node.js core, so there's no need to install anything.

fs.readdir

const testFolder = './tests/';
const fs = require('fs');

fs.readdir(testFolder, (err, files) => {
  files.forEach(file => {
    console.log(file);
  });
});

fs.readdirSync

const testFolder = './tests/';
const fs = require('fs');

fs.readdirSync(testFolder).forEach(file => {
  console.log(file);
});

The difference between the two methods, is that the first one is asynchronous, so you have to provide a callback function that will be executed when the read process ends.

The second is synchronous, it will return the file name array, but it will stop any further execution of your code until the read process ends.

Tuesday, June 1, 2021
 
conmen
answered 6 Months ago
11

I believe you're looking for a method that takes a list, adds something to it, and then returns the list. The list is generic and you want the return type to match the type of the parameter.

Here's how you do it, in the general case:

public <T extends Parent> List<T> myfunction(List<T> myList) {
   ... // Add something to myList
   return myList;
} 

Specifically, you're trying to add new Child() to myList. This cannot work. myfunction() may be called with many kinds of lists, whereas adding new Child() can only work if the list is either a List<Parent> or a List<Child>. Here's an example for a List of different kind:

public static class Parent {}  
public static class Child extends Parent {}  
public static class OtherChild extends Parent {}

public <T extends Parent> List<T> myfunction(List<T> myList) {
  myList.add(new Child());
  return myList;
} 

myfunction(new ArrayList<OtherChild>());

In the last line, myfunction() is called with a List of OtherChild objects. Obviously, adding a Child object into such a list is illegal. The compiler prevents that by rejecting the definition of myfunction()

Appendix

If you want myfunction() to be able to add an element to myList you need to use a factory (since Java does not allow new T() where T is a type parameter - due to type erasure). Here's how myfunction() should look like:

public interface Factory<T> {
  public T create();
}

public <T extends Parent> List<T> myfunction(List<T> myList, 
                                             Factory<? extends T> factory) {
  myList.add(factory.create());
  return myList;
} 

And now, its usage:

public static class ChildOfOtherChild extends OtherChild {}

myfunction(new ArrayList<OtherChild>(), new Factory<ChildOfOtherChild>() {
    @Override public ChildOfOtherChild create() { return new ChildOfOtherChild(); }
  });
}
Friday, August 27, 2021
 
John Oleynik
answered 3 Months ago
58

Most collections accept Collection as a constructor argument:

List<String> keyList = new ArrayList<String>(map.keySet());
List<String> valueList = new ArrayList<String>(map.values());
Thursday, October 7, 2021
 
ngourley
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share