「java集合分解」java集合分割

博主:adminadmin 2023-01-05 08:00:10 674

本篇文章给大家谈谈java集合分解,以及java集合分割对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java中怎么把一个集合分成多个集合

List list1 = new ArrayList(); list1.add("a");list1.add("b"); List list2 = Arrays.asList("c","d"); list1.addAll(list2); System.out.println(list1);

求java代码。关于将一个集合按照要求分解成两个集合

//Platform.java

public class Platform {

/**

* @author lusong

*/

private String brandName;

private String model;

public Platform(String bString,String mString) {

brandName=bString;

model=mString;

}

public String getBrandName() {

return brandName;

}

public String getModel(){

return model;

}

}

//ShiXian.java,其中的getList()方法返回你要求的东东

//不知道合不合你的胃口

import java.util.*;

public class ShiXian {

ArrayListPlatform PlatformList=new ArrayListPlatform();

//用于包含型号的名称,用于验证重复,在getLIst方法中使用,因为内置类不能访问方法的局部变量所有在此定义方法中的变量

ListString arrayList=new ArrayListString();

public ShiXian() {

//向该集合中添加成员,型号之间用逗号隔开

PlatformList.add(new Platform("诺基亚","7210,5530,2120"));

PlatformList.add(new Platform("摩托罗拉","V3,L6"));

PlatformList.add(new Platform("诺基亚","N70,N73,N95"));

PlatformList.add(new Platform("摩托罗拉","V4,L7"));

PlatformList.add(new Platform("摩托罗","V4,L7"));

PlatformList.add(new Platform("摩","V4,L7"));

PlatformList.add(new Platform("摩","V4,L7"));

//你可以随意添加成员,注意型号之间用逗号隔开

}

//你的要求是返回一个包含所有品牌的List,每个List项为一个包含型号的List

public ListArrayListString getList(){

//用于最终返回结果的List

ListArrayListString list=new ArrayListArrayListString(){

//重写toString()方法,便于显示手机名称

@Override

public String toString() {

int i=0;

// TODO Auto-generated method stub

StringBuilder stringBuilder=new StringBuilder();

for (Iterator iterator = this.iterator(); iterator.hasNext();) {

ArrayListString arrayList1 = (ArrayListString) iterator.next();

stringBuilder.append(arrayList.get(i));

stringBuilder.append("的型号为:");

i=i+1;

stringBuilder.append(arrayList1.toString());

}

return stringBuilder.toString();

}

};

for (Iterator iterator = PlatformList.iterator(); iterator.hasNext();) {

Platform platform=(Platform)iterator.next();

if (arrayList.contains(platform.getBrandName())) {

//该手机已经存在,只用添加型号,并且验证是否重复

String[] strings=platform.getModel().split(",");

ArrayListString arrayList3=list.get(arrayList.indexOf(platform.getBrandName()));

for (int i = 0; i strings.length; i++) {

if (!arrayList3.contains(strings[i])) {

arrayList3.add(strings[i]);

}

}

}

else {

//该手机还不存在,要添加该手机,并添加型号

arrayList.add(platform.getBrandName());

ArrayListString arrayList3=new ArrayListString();

String[] strings=platform.getModel().split(",");

for (int i = 0; i strings.length; i++) {

arrayList3.add(strings[i]);

}

list.add(arrayList3);

}

}

return list;

}

public static void main(String[] args) {

System.out.println(new ShiXian().getList());

}

}

java怎么集合分类?

Set 接口继承 Collection,但不允许重复,使用自己内部的一个排列机制。

List 接口继承 Collection,允许重复,以元素安插的次序来放置元素,不会重新排列。

Map是以键值对存放!

现在主要用ArrayList和HashMap!

collection接口下由set,list两大集合实现,set又有HashSet,TreeSet实现,List又由ArrayList,LinkedList实现;另一Map接口由HashMap,TreeMap实现。

Java API中所用的集合类,都是实现了Collection接口,他的一个类继承结构如下:

Collection--List--Vector

Collection--List--ArrayList

Collection--List--LinkedList

Collection--Set--HashSet

Collection--Set--HashSet--LinkedHashSet

Collection--Set--SortedSet--TreeSet。

java集合分解的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java集合分割、java集合分解的信息别忘了在本站进行查找喔。