java集合形象代码 java 集合类型
java帮忙写写,几种集合的运用,我是菜鸟!
ArrayListInteger list = new ArrayListInteger();
创新互联建站自2013年起,先为喀喇沁等服务建站,喀喇沁等地企业,进行企业商务咨询服务。为喀喇沁企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
list.addAll(Arrays.asList(17,48,93,75,85));
Integer max = Collections.max(list);
Integer sum = 0;
for (Integer i : list) {
sum += i;
}
//最大值max,和sum
HashSetString set = new HashSetString();
set.addAll(Arrays.asList("a","b","c","d","e"));
for (String s : set) {
//输出Set
System.out.print(s);
System.out.print("\t");
}
HashMapString, String map = new HashMapString, String();
//输入Map
Scanner input = new Scanner(System.in);
for (int i = 0; i 5; i++) {
System.out.print("键:");
String k = input.nextLine();
System.out.print("值:");
String v = input.nextLine();
map.put(k, v);
}
map.put("a", "a");
map.put("b", "b");
map.put("c", "c");
map.put("d", "d");
map.put("e", "e");
Set keyset = map.keySet();//Map所有键
Collection values = map.values();//Map所有值
//输出Map
for (Object obj : keyset) {
System.out.print(obj);
System.out.println(":");
System.out.println(map.get(obj));
}
各位大神高分求一个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=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点抗 pile("\\{.*?\\}");
public static Pattern p_time = Pattern点抗 pile("TIME=.*?,");
public static Pattern p_price = Pattern点抗 pile("PRICE=.*?,");
public static Pattern p_account = Pattern点抗 pile("AccountMethod=.*?,");
public static Pattern p_value = Pattern点抗 pile("BW_VALUE=.*?\\}");
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;
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 代码 用Collections 里的方法 用集合的方式 表现 数字的快速排列 JDK1.6
package com.test.jdbc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 根据 order字段对 User 排序
*/
public class User implements ComparableUser {
private String name;
private Integer order;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
public int compareTo(User arg0) {
return this.getOrder()点抗 pareTo(arg0.getOrder());
}
public static void main(String[]args){
User user1 = new User();
user1.setName("a");
user1.setOrder(1);
User user2 = new User();
user2.setName("b");
user2.setOrder(2);
ListUser list = new ArrayListUser();
//此处先add user2再add user1
list.add(user2);
list.add(user1);
Collections.sort(list);
for(User u:list){
System.out.println(u.getName());
}
}
}
当前题目:java集合形象代码 java 集合类型
转载来于:http://scjbc.cn/article/ddegphg.html