public interface List扩展Collection
Collection是一个接口,而不是List扩展它而不是实现?
我需要搜索案例类示例列表:在下面的示例中,我想知道teamList
包含name=php
与否.
scala> case class Team(name: String, image: String, nMember: BigInt, nYear: BigInt)
defined class Team
scala> val teamList=List(Team("scala","s.jpg",58,5),Team("java","cup.jpg",5400,18),Team("php","elephant.jpg",5800,8))
teamList: List[Team] = List(Team(scala,s.jpg,58,5), Team(java,cup.jpg,5400,18), Team(php,elephant.jpg,5800,8))
Run Code Online (Sandbox Code Playgroud) 我对收藏的概念很陌生.我有一个类object(Officer
),它有几个属性,其中三个构成了官员的名字.
我还有一个单独的类对象Company
,它被定位在Officer
对象上方.A company
包含一系列官员,因此要显示官员的姓名,我需要两者之间的某种形式的参考:
private List<Officer> officerList;
public void addOfficer(Officer off)
{
officerList.Add(off);
}
Run Code Online (Sandbox Code Playgroud)
现在,要实际将该数据返回到另一个类,我需要创建一个列表(无意义),创建大量Officer
对象(效率低下)或返回一个集合.
public void getOfficerNames(out *something* names) //I want to use a collection
//here... I think
{
foreach (Officer o in officerList)
{
names = o.Title
+ o.ForeName
+ o.SurName;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我所拥有的,因为我很难理解
a)如何实现集合和
b)如果这是正确的事情.
我List<Point2D.Double> serie
在绘图仪中用于存储点坐标。当我想排序时,我决定使用,Collections.sort(serie)
但它表明存在类似的错误
不能有双重类型
那么如何List<Point2D.Double>
按x
坐标排序?
我想编写一个方法,它将Map作为参数,可以包含任何对象作为其值.我试图使用泛型实现它,但我无法实现它.
下面是我到目前为止尝试过的代码.这个代码作为两种方法: genericTest
,我试图实现一个通用的解决方案 nonGenericTest
,这是我实际上想要使用通用解决方案,但是没有使用泛型实现.基本上,第二种方法是我想要转换为泛型的方法.
我究竟做错了什么?
import java.util.Iterator;
import java.util.Map;
public class Test {
//V cannot be resolve to a type
public void genericTest(Map<String, V> params) {
}
public void nonGenericTest(Map<String, Object> params) {
Iterator<String> it = params.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
Object value = params.get(key);
if(value instanceof String){
String val1=(String) value;
// do something
}else if(value instanceof Integer){
Integer val1 = (Integer) value;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到编译器错误,如代码中所示.
我有这样的地图,
Map<String, Map<String, Set<String>>> sampleMap = new Map<String, Map<String, Set<String>>>();
Run Code Online (Sandbox Code Playgroud)
这个地图中的数据就是这样的,
sampleMap={2014={A=[1, 2], B=[3], 2015={A=[1,2], B=[1,2], 2016={A=[1,2], B=[3,4]}};
Run Code Online (Sandbox Code Playgroud)
我想根据这个输入从地图中删除密钥:List<String> filter;
这样的值,
filterArray : [2014, 2015]
Run Code Online (Sandbox Code Playgroud)
即,首先逐个遍历arraylist值,验证arraylist值是否与Hashmap中的任何键匹配.
即,我总是希望只保留地图中匹配的键,与传递的输入值进行比较.
在这种情况下,因为我有这样的arraylist值,[2014,2015],
2014,2015键只能在我的地图中.所以,
删除前的数据:
sampleMap={2014={A=[1, 2], B=[3], 2015={A=[1,2], B=[1,2], 2016={A=[1,2], B=[3,4]}};
Run Code Online (Sandbox Code Playgroud)
移动后的数据:
sampleMap={2014={A=[1, 2], B=[3], 2015={A=[1,2], B=[1,2]}};
Run Code Online (Sandbox Code Playgroud)
我试过这种方式,但我只是想知道这是正确的approch,还是容易出现任何异常?
Iterator<Map.Entry<String , Map<String, Set<String>>>> iter = sampleMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String , Map<String, Set<String>>> entry = iter.next();
logger.info("Keys : " + entry.getKey());
if (filterArray.equalsIgnoreCase(entry.getKey())) {
iter.remove();
}
}
Run Code Online (Sandbox Code Playgroud) 在下面的例子中,所做的更改List B
直接反映在int中List A
.
List<string> A = new List<string>();
A.Add("1");
List<string> B = new List<string>();
B = A;
foreach (string value in B)
{
Console.WriteLine("B= " + value);
}
B.Add("2");
foreach (string value in A)
{
Console.WriteLine("A= " + value);
}
Run Code Online (Sandbox Code Playgroud)
输出是
B = 1
A = 1
A = 2
即使我使用了输出也是相同的Arraylist
.
如果是字符串,则行为不然!
String s1 = "abc";
String s2 = s1;
s2 += "TEST";
Console.WriteLine("S1=" + s1 + " S2=" + s2);
Run Code Online (Sandbox Code Playgroud)
输出是
S1 = …
我有以下任务:
"声明一个方法,期待一个Collection并在你的方法中反转它.返回给出的相同集合,不要返回一个新的集合!
static <T> void reverse (Collection<T> collection)
Run Code Online (Sandbox Code Playgroud)
不要尝试使用Collections.reverse.它仅适用于List,而不适用于集合"
我最初的想法是以下内容:
public static <T> void reverse(Collection<T> collection){
int size = collection.size();
Iterator<T> iter = collection.iterator();
Iterator<T> iter2 = collection.iterator();
for (int i = 0; i < size / 2; i++) {
collection.add(iter.next());
iter2.remove();
}
}
Run Code Online (Sandbox Code Playgroud)
但我不断得到奇怪的例外:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at ReverseCollection.reverse(ReverseCollection.java:16)
at ReverseCollection.main(ReverseCollection.java:25)
Run Code Online (Sandbox Code Playgroud)
知道应该怎么做吗?
我有关于unmodifiableList
API的查询Collections
.
代码段:
import java.util.*;
public class ReadOnlyList{
public static void main(String args[]){
List<String> list = new ArrayList<String>();
list.add("Stack");
list.add("Over");
List list1 = Collections.unmodifiableList(list);
//list1.add("Flow");
list.add("Flow");
System.out.println("sizes:1:2:"+list.size()+":"+list1.size());
for (Iterator it = list1.iterator();it.hasNext();){
System.out.println("elements:"+it.next());
}
System.out.println("__________");
list.remove("Flow");
for (Iterator it = list1.iterator();it.hasNext();){
System.out.println("elements:"+it.next());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我无法修改不可修改的集合,因此我会收到错误
//list1.add("Flow");
Run Code Online (Sandbox Code Playgroud)
我希望list
在创建之前我将获得只读变量列表unmodifiableList
.但即使在list1
创建之后,父列表中的更改也会反映在子级中list1
我无法修改子无法修改列表,但仍然收到父列表的修改.这对我来说很意外.
我知道修复是从父级创建新的集合,但stil不理解上述行为.
在迭代期间,不允许从集合中删除.我有一段代码正在工作,我认为不应该工作.它可能在将来失败,为什么它现在正在运作?
public class RemoveFromSet {
static Set<Integer> set = new HashSet<>();
public static void main(String[] args) {
set.add(1);
set.add(2);
set.add(3);
set.add(4);
while(set.size()>0) {
int val = set.iterator().next();
set.remove(val);
System.out.println("removed val = "+val);
}
}
}
Run Code Online (Sandbox Code Playgroud)