关于java中的泛型和列表 [英] About generics and lists in java

查看:83
本文介绍了关于java中的泛型和列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的界面:

  public interface可传送< T> {
public T getTransport();
}

以及许多提供实现的类,即:

  class A implements Transportable< ATransport> {
public ATransport getTransport(){
ATransport at = new ATransport();
[...]
返回;
}
}
class B实现可传输< BTransport> {
public BTransport getTransport(){
BTransport bt = new BTransport();
[...]
return bt;
}
}

我也列出了可移动对象,像这样:

  LinkedList< A> aList = new LinkedList<>(); 

我想为aList中的所有元素调用getTransport方法,获得一个LinkedList,那:

  LinkedList< ATransport> atList = getTransport(aList); 
LinkedList< BTransport> btList = getTransport(bList);

我不想为每个类都写foreach,有没有办法实现这使用泛型?例如:

  public< T>列表与LT; ???> getTransport(List< T> list)

或类似的东西?我试过但没有运气,我想我错过了一些关于泛型的知识,以及使用等方面的区别。



谢谢!

解决方案

这似乎是工作:

 包装假人; 

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

class ATransport {}
class BTransport {}
class A implements Transportable< ATransport> {
public ATransport getTransport(){
ATransport at = new ATransport();
返回;
}
}
class B实现可传输< BTransport> {
public BTransport getTransport(){
BTransport bt = new BTransport();
return bt;
}
}
class X {
public static< U,T extends Transportable< U>>列表与LT; U> getTransport(列表< T>列表){
列表< U> ret = new LinkedList<>(); (T t:list){
ret.add(t.getTransport());

}
return ret;
}

public static void main(String [] args){
List< BTransport> transportsB = getTransport(Collections.singletonList(new B()));
列表< ATransport> transportsA = getTransport(Collections.singletonList(new A()));






这个背后的想法是我们将这个类型命名为需要和我们得到的。唯一的技巧是创建一个静态方法,所以类型推断在那里工作。 (就像在 Collections.singletonList 调用中一样。)



编辑:
这个是一个泛型解释 static 方法技巧和一些其他类型的推理用例。 似乎是一种非常有用的仿制药资源。基本的想法(命名其他类型并在绑定中使用)可能会有一个更通用的名称,我不知道,因为没有提及这个名称而感到遗憾。


I've this simple interface:

public interface Transportable<T> { 
  public T getTransport();
}

and a lot of classes that provide implementation, ie:

class A implements Transportable<ATransport> {
  public ATransport getTransport() {
    ATransport at=new ATransport();
    [...]
    return at;
  }
}
class B implements Transportable<BTransport> {
  public BTransport getTransport() {
    BTransport bt=new BTransport();
    [...]
    return bt;
  }
}

also I've lists of transportables objects, like this:

LinkedList<A> aList=new LinkedList<>();

and I want to call the getTransport method for all elements in aList, obtaining a LinkedList, doing something like that:

LinkedList<ATransport> atList = getTransport(aList);
LinkedList<BTransport> btList = getTransport(bList);

I don't want to write a foreach for every class I have, is there a way to implement this using generics? like:

public <T> List<???> getTransport(List<T> list)

or something similar? I've tried but without luck, I think I'm missing something about generics and about the difference between using or etc...

Thank you!

解决方案

This seems to be working:

package dummy;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

class ATransport{}
class BTransport{}
class A implements Transportable<ATransport> {
  public ATransport getTransport() {
    ATransport at=new ATransport();
    return at;
  }
}
class B implements Transportable<BTransport> {
  public BTransport getTransport() {
    BTransport bt=new BTransport();
    return bt;
  }
}
class X {
    public static <U, T extends Transportable<U>> List<U> getTransport(List<T> list) {
        List<U> ret = new LinkedList<>();
        for (T t : list) {
            ret.add(t.getTransport());
        }
        return ret;
    }

    public static void main(String[] args) {
        List<BTransport> transportsB = getTransport(Collections.singletonList(new B()));
        List<ATransport> transportsA = getTransport(Collections.singletonList(new A()));
    }
}

The idea behind this is that we name the type we need and what we get. The only trick is to create as a static method, so the type inference is working there. (Just like in the Collections.singletonList call.)

Edit: This is a tutorial for generics explaining the static method trick and some other type inference use cases. This seems to be a useful resource for generics. The basic idea (naming the other type and using in the bound) might have a more general name that I am unaware of, sorry for not giving a reference to that.

这篇关于关于java中的泛型和列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆