求集合java代码 java集合的代码

各位大神高分求一个JAVA集合问题,请给出详细的代码import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static String str = "[{TIME=2006-06, PRICE=246.91388889, AccountMethod=7, BW_VALUE=https://www.04ip.com/post/88888}, {TIME=2006-07, PRICE=305611.11111111, AccountMethod=1, BW_VALUE=7070120}, {TIME=2006-08, PRICE=246.91388889, AccountMethod=1, BW_VALUE=88888}, {TIME=2006-08, PRICE=246.91388889, AccountMethod=7, BW_VALUE=7777}]";
public static ListDemo demos = new ArrayListDemo();
public static MapString, Demo maps = new HashMapString, Demo();
public static Pattern p = Pattern.compile("\\{.*?\\}");
public static Pattern p_time = Pattern.compile("TIME=.*?,");
public static Pattern p_price = Pattern.compile("PRICE=.*?,");
public static Pattern p_account = Pattern.compile("AccountMethod=.*?,");
public static Pattern p_value = https://www.04ip.com/post/Pattern.compile("BW_VALUE=https://www.04ip.com/post/.*?\\}");
public static void main(String[] args) {
Matcher m = p.matcher(str);
while (m.find()) {
Demo demo = null;
String s = m.group();
String t = null;
Matcher m2 = null;
m2 = p_time.matcher(s);
if (m2.find()) {
t = m2.group();
t = t.substring(5, t.length() - 1);
if (maps.containsKey(t)) {
demo = maps.get(t);
m2 = p_price.matcher(s);
if (m2.find()) {
String p = m2.group();
demo.setPRICE(demo.getPRICE()" "
p.substring(6, p.length() - 1));
}
m2 = p_account.matcher(s);
if (m2.find()) {
String a = m2.group();
a = a.substring(14, a.length() - 1);
if (a.equals("7")) {
m2 = p_value.matcher(s);
if (m2.find()) {
String v = m2.group();
v = v.substring(9, v.length() - 1);
demo.setFree(Double.parseDouble(v)
demo.getFree());
}
} else {
m2 = p_value.matcher(s);
if (m2.find()) {
String v = m2.group();
v = v.substring(9, v.length() - 1);
demo.setCharge(Double.parseDouble(v)
demo.getCharge());
}
}
}
} else {
demo = new Demo();
demo.setTIME(t);
m2 = p_price.matcher(s);
if (m2.find()) {
String p = m2.group();
demo.setPRICE(p.substring(6, p.length() - 1));
}
m2 = p_account.matcher(s);
if (m2.find()) {
String a = m2.group();
a = a.substring(14, a.length() - 1);
if (a.equals("7")) {
m2 = p_value.matcher(s);
if (m2.find()) {
String v = m2.group();
v = v.substring(9, v.length() - 1);
demo.setFree(Double.parseDouble(v));
}
} else {
m2 = p_value.matcher(s);
if (m2.find()) {
String v = m2.group();
v = v.substring(9, v.length() - 1);
demo.setCharge(Double.parseDouble(v));
}
}
}
}
}
maps.put(t, demo);
}
System.out.println(getResult());
}
public static String getResult() {
StringBuffer sb = new StringBuffer();
sb.append("[");
IteratorString iter = maps.keySet().iterator();
while (iter.hasNext()) {
String key = iter.next();
Demo demo = maps.get(key);
String temp = null;
【求集合java代码 java集合的代码】if (demo.getFree() != 0) {
if (demo.getCharge() != 0) {
temp = "{TIME="demo.getTIME()",PRICE="
demo.getPRICE()",charge="demo.getCharge()
",free="demo.getFree()"}";
} else {
temp = "{TIME="demo.getTIME()",PRICE="
demo.getPRICE()",free="demo.getFree()"}";
}
} else {
if (demo.getCharge() != 0) {
temp = "{TIME="demo.getTIME()",PRICE="
demo.getPRICE()",charge="demo.getCharge()
"}";
} else {
temp = "{TIME="demo.getTIME()",PRICE="
demo.getPRICE()"}";
}
}
sb.append(temp",");
}
sb.deleteCharAt(sb.length() - 1);
sb.append("]");
return sb.toString();
}
}
其中的Demo就是一个javabean 。
Java求集合编写package com.dy.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class Test {
public static void main(String[] args) throws IOException {
Student[] stu = new Student[10];
stu[0] = new Student("20170101", "小明", new Random().nextInt(41)60);
stu[1] = new Student("20170102", "小红", new Random().nextInt(41)60);
stu[2] = new Student("20170103", "小华", new Random().nextInt(41)60);
stu[3] = new Student("20170104", "小风", new Random().nextInt(41)60);
stu[4] = new Student("20170105", "小黄", new Random().nextInt(41)60);
stu[5] = new Student("20170106", "小绿", new Random().nextInt(41)60);
stu[6] = new Student("20170107", "小帅", new Random().nextInt(41)60);
stu[7] = new Student("20170108", "小黑", new Random().nextInt(41)60);
stu[8] = new Student("20170109", "小军", new Random().nextInt(41)60);
stu[9] = new Student("20170110", "小胖", new Random().nextInt(41)60);
System.out.println("排序前:");
for (int i = 0; istu.length; i) {
System.out.println(stu[i]);
}
// List集合
ListStudent stuList = new ArrayListStudent();
for (int i = 0; istu.length; i) {
stuList.add(stu[i]);
}
System.out.println("List排序:");
Collections.sort(stuList); // 排序方法
for (int i = 0; istuList.size(); i) {
System.out.println(stuList.get(i));
}
// Set集合
SetStudent stuSet = new TreeSetStudent(); // TreeSet自动排序
for (int i = 0; istu.length; i) {
stuSet.add(stu[i]);
}
System.out.println("Set排序:");
for (int i = 0; istuSet.size(); i) {
System.out.println(stuSet.toArray()[i]);
}
// Map
MapStudent, Object stuMap = new TreeMapStudent, Object(); // TreeMap自动排序
for (int i = 0; istu.length; i) {
stuMap.put(stu[i], i);
}
System.out.println("Map排序:");
Object[] keys = stuMap.keySet().toArray();
for (int i = 0; ikeys.length; i) {
System.out.println(keys[i]);
}
}
}
class Student implements ComparableStudent {
private String sid;
private String sname;
private int score;
public Student(String sid, String sname, int score) {
super();
this.sid = sid;
this.sname = sname;
this.score = score;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public int compareTo(Student o) {
//return this.score - o.getScore(); // 从低到高
int r = o.getScore() - this.score; // 从高到低
return r == 0 ? o.getSid().hashCode() - sid.hashCode() : r;
}
@Override
public String toString() {
return sname"("sid") "score"分";
}
}
运行效果:
将两个数组 排列组合到一个数组集合 求java 代码import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
private String[] arr1;
private String[] arr2;
public Test(){
//数组长度可以随意改,但必须保证两个数组长度一样
arr1 = new String[3];
arr2 = new String[3];
initArray();
}
private void initArray(){
Scanner scanner = new Scanner(System.in);
for(int i=0;iarr1.length;i){
arr1[i] = scanner.next();
}
for(int i=0;iarr2.length;i){
arr2[i] = scanner.next();
}
}
/**
* 获取组合后的数组表单
* @return
*/
private ListString[] getcombineList(){
ListString[] list = new ArrayListString[]();
combineArray(list, arr1, arr2);
combineArray(list, arr2, arr1);
return list;
}
/**
* 遍历数组str1,并将其复制到新的数组,再将str2中索引值与str1中索引值
* 相同的元素放入新的数组,然后将新的数组放入list中
* @param list
* @param str1
* @param str2
*/
private void combineArray(ListString[] list,String[] str1, String[] str2){
for(int i=0; istr1.length; i){
String[] str = new String[str1.length];
for(int j=0; jstr2.length; j){
str[j] = str2[j];
if(i == j)
str[j] = str1[j];
}
System.out.println(getArrayMessage(str));
list.add(str);
}
}
/**
* 将数组每一个元素组合成字符串
* @param str 字符串数组
* @return 组合字符串
*/
private String getArrayMessage(String[] str){
StringBuffer buffer = new StringBuffer();
for(int i=0; istr.length; i){
if(i == str.length-1)
buffer.append(str[i]);
else
buffer.append(str[i]).append(",");
}
return buffer.toString();
}
public static void main(String[] argv){
Test test = new Test();
test.getcombineList();
}
}
用java编写程序,求集合的并集、交集和差集public static voidmain(String[] args) {
Integer[] A = {1,2,3,4};
Integer[] B = {1,3,7,9,11};
ListInteger listA = Arrays.asList(A);
ListInteger listB = Arrays.asList(B);
ListInteger jiaoji = new ArrayListInteger();
for(Integer a:listA){
if(listB.contains(a)){
jiaoji.add(a);
}
}
System.out.println(jiaoji);
ListInteger bingji = new ArrayListInteger();
for(Integer a:listA){
if(!bingji.contains(a)){
bingji.add(a);
}
}
for(Integer b:listB){
if(!bingji.contains(b)){
bingji.add(b);
}
}
System.out.println(bingji);
ListInteger chaji = new ArrayListInteger();
for(Integer a:listA){
if(!listB.contains(a)){
chaji.add(a);
}
}
System.out.println(chaji);
}
求集合java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java集合的代码、求集合java代码的信息别忘了在本站进行查找喔 。

    推荐阅读