简体   繁体   English

关于java集合和泛型:

[英]About java Collections and generics:

I have been given the following school assignment: 我得到了以下学校作业:
Given two sorted lists of comparable items, L1 and L2 给定两个可比较项目的排序列表L1L2

  • (a) Implement a method in Java to determinate if L1 and L2 are disjoints lists. (a)在Java中实现一种方法来确定L1L2是否是不交连的列表。
  • (b) Implement a method in Java to compute the difference (\\) (b)在Java中实现一种方法来计算差异(\\)
    between L1 and L2 . L1L2之间。 L1 \\ L2 = { x | L1 \\ L2 = {x | x ∈ L1 and x ∉ L2 }. x∈L1和x∉L2}。
  • (c) What is the running time complexity of your methods? (c)您的方法的运行时间复杂度是多少? Justify. 自圆其说。

Note: For this problem you can use only the Java Collections API basic methods. 注意:对于此问题,您只能使用Java Collections API基本方法。

I have answered both questions but have found two possible solutions to problem (b), given that I am still learning java Generics and wildcards and have still yet to fully grasp this concept. 考虑到我仍在学习java泛型和通配符,并且还没有完全掌握这个概念,我已经回答了两个问题,但是找到了解决问题(b)的两种可能的方法。

Here is my answer to question (a): 这是我对问题(a)的回答:

public static final <T> boolean disjoint(List<? extends T> list1, List<? extends T> list2 ){
        return Collections.disjoint(list1, list2);
    }

This method efficiently answers if both lists are disjoint or not. 如果两个列表都不相交,则此方法可以有效回答。 And uses Collections.disjoint just as the problem requested. 并按照要求使用Collections.disjoint。

ex: 例如:

List<Float> lista = Arrays.asList(2.3f, 3.4f);
List <Object> listb = Arrays.asList("a","b","c","d", 1, 2, 3, 3.4f);

System.out.println(SetList.disjoint(list1, list9));//prints false;

Now for part (b) I created first the following method: 现在,对于(b)部分,我首先创建了以下方法:

public static final <T> List<? super T> 
                              setDifference(List<? extends T> list1, List<? extends T> list2){

            List<? super T> resultSet = new ArrayList<>();

            for(T x : list1){
                if(!list2.contains(x)){
                    resultSet.add(x);
                }
            }
            return resultSet;
    }

This method allows me to do the following: 这种方法使我可以执行以下操作:

List <Object> list1 = Arrays.asList(1.2f,12f, "a","b","c","d", 1,2,3,3.4f);
List<Float> list9 = Arrays.asList(2.3f, 3.4f);

System.out.println(SetList.setDifference(list1, list9));
//Prints [1.2, 12.0, a, b, c, d, 1, 2, 3]

or this: 或这个:

List<Integer> list3 = Arrays.asList(1,2,3,4,10);
List<Float> list9 = Arrays.asList(2.3f, 3.4f);
System.out.println(SetList.setDifference(list3, list9));
// this prints [1, 2, 3, 4, 10]

Now since the problem says that the lists are of comparable items I naively assumed that I must throw in comparable in the signature of this method so I implemented the following: 现在,由于问题表明列表是可比较的项目,因此我天真地假定我必须在此方法的签名中加入可比较的内容,因此我实现了以下内容:

public static final <T extends Comparable<? super T>> List<? super T> 
                              setDifference2(List<? extends T> list1, List<? extends T> list2){

        List<T> resultSet = new ArrayList<>();

        for(T x: list1){
            if(Collections.binarySearch(list2, x)<0){
                resultSet.add(x);
            }
        }
        return resultSet;
    }  

However with this signature it seems I can only take lists that have the same type, which was not the case before where I could take a list of type object and a list of Integers etc. 但是,有了这个签名,似乎我只能接受具有相同类型的列表,在我可以接受类型对象列表和整数列表等之前,情况并非如此。

It seems that now I can only work with lists of the same type. 看来现在我只能使用相同类型的列表。 And I really do not know if that is was the problem intended me to do since the beginning. 我真的不知道那是我从一开始就打算解决的问题。

I understand that Binary search improves the performance of this problem but does it do it at the expense of the restriction it imposes over the input of the method? 我知道二进制搜索可以改善此问题的性能,但是这样做是以牺牲对方法输入的限制为代价的吗?
Also am I doing proper use of the wildcards for these methods signatures and Lists declarations? 另外,我是否对这些方法签名和Lists声明正确使用了通配符?

I recently started reading the book: Java Generics and Collections by Maurice Naftalin and Philip Wadler and I am trying to follow the recommendations from the book, the gets and puts principle etc, would love some advice and consult about these answers I came up with and also some recommended further reading that would enable me to learn this topic better. 我最近开始阅读这本书: Maurice NaftalinPhilip Wadler 撰写的 Java Generics and Collections ,我正尝试遵循本书中的建议, getsputs原理等,希望得到一些建议并咨询我提出的这些答案,并也有人建议进一步阅读,使我可以更好地学习该主题。

I understand that Binary search improves the performance of this problem but does it do it at the expense of the restriction it imposes over the input of the method? 我知道二进制搜索可以改善此问题的性能,但是这样做是以牺牲对方法输入的限制为代价的吗?

Yes. 是。

Also am I doing proper use of the wildcards for these methods signatures and Lists declarations? 另外,我是否对这些方法签名和Lists声明正确使用了通配符?

Yes. 是。

In response to @shmosel, is this a better answer to part (a)? 回应@shmosel,这是对(a)部分的更好回答吗?

public static final <T extends Comparable<? super T>> 
                      boolean disjoint2(List<? extends T> list1, List<? extends T>list2){
    for(T x: list1){
        if(Collections.binarySearch(list2, x)>=0){
            return false;
        }
    }   
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM