集合

集合共有的方法:add,isEmpty,Iterator,remove

  • Collection(不唯一,无序) 
  1. List(不唯一,有序):ArrayList,LinkedList
  2. Set(唯一,无序):HashSet,TreeSet
  • Map(键值对象)(没有Iterator): HashMap,TreeMap

List常用办法get,set,Listiterator

同步:同斥,上锁
vector(与ArrayList同级)

异步
ArrayList 动态
连续空间,遍历,随机访问元素
长度可变的数组

LinkedList 链表
插入,删除元素
增加或删除首尾对象

  1 范例1:
  2 /**
  3  * 把1到100中所有包含4的数剔除,并打印剩下的数
  4  */
  5 import java.util.*;
  6 public class DivisibleBy4 {
  7     public static void main(String[] args) {
  8         List nums = new ArrayList();
  9         List nums1 = new ArrayList();
 10         //存数据
 11         for (int i = 1; i <= 100; i++) {
 12             nums.add(i);
 13         }
 14         
 15         !!!!★★★★★对数据进行操作,把数据放在另一个集合中★★★★★
 16         !!!!★★★★★对某对象进行foreach操作,不能对该对象进行remove,add操作★★★★★
 17         for (int i = 0; i < nums.size(); i++) {
 18             if (nums.get(i).toString().contains("4")) {
 19                 //【★★★如果add为false,那么添加不成功!!!!★★★】
 20                 nums1.add(nums.get(i));
 21             }
 22         }
 23         
 24         /*方法一*/
 25         for (int i = 0; i < nums1.size(); i++) {
 26             nums.remove(nums1.get(i));
 27         }
 28         
 29         /*方法二*/
 30         //【★★★如果remove为false,那么删除不成功!!!!★★★】
 31         nums.removeAll(nums1);
 32         System.out.println(nums);
 33 }
 34 
 35 范例2:
 36 /**
 37  * 把车牌号中所有包含8的剔除,并打印剩下的数
 38  */
 39 import java.util.*;
 40 public class PlateNum {        
 41         List<String> No = new ArrayList<String>();
 42         List<String> No1 = new ArrayList<String>();
 43         Random rd = new Random();
 44         for (int i = 0; i < 10; i++) {
 45             //随机一些车牌
 46             String carNo = "川A" + (rd.nextInt(100000) + 1);
 47             No.add(carNo);
 48         }
 49         System.out.println(No);
 50         
 51         /**
 52          * 第一种方法
 53          */
 54         for (int i = 0; i < No.size(); i++) {
 55             if (No.get(i).toString().contains("8")) {
 56                 No1.add(No.get(i));
 57             }
 58         }
 59         //【★★★如果remove为false,那么删除不成功!!!!★★★】
 60         No.removeAll(No1);
 61         System.out.println(No);
 62         
 63         /**
 64          * 第二种方法
 65          */
 66         for (int i = 0; i < No.size(); i++) {
 67             char[] ch = No.get(i).toString().toCharArray();
 68             for (int j = 2; j < ch.length; j++) {
 69                 if (ch[j] == '8') {
 70                     No.remove(i);
 71                     break;
 72                 }
 73             }
 74         }
 75         System.out.println(No);
 76     }
 77 }
 78 
 79 ArrayList:add,remove,toString,subString,foreach,随机名字
 80 范例1:
 81 /**
 82  * 100个姓名存在集合中,把姓王的找出来
 83  */
 84 public class TestDemo2 {
 85     public static void main(String[] args) {
 86         List al = new ArrayList();
 87         //构建姓的数组
 88         String[] xin = new String[]{"丁","王","徐","汤","独孤","李"};
 89         String[] ming = new String[]{"强","求败","建国","援朝","翠花","狗蛋","嫚","凯","程琳"};
 90         
 91         /*存数据*/
 92         Random rd = new Random();
 93         for (int i = 0; i < 100; i++) {
 94             al.add(xin[rd.nextInt(xin.length)] + ming[rd.nextInt(ming.length)]);
 95         }
 96         //测试
 97         System.out.println(al);
 98         
 99         /*取数据*/
100         //姓王的名字剔除
101         /*第一种方法*/
102         System.out.println("***************************************************");
103         List al2 = new ArrayList();
104         for (int i = 0;i < al.size();i++) {
105             /*charAt,首字母*/
106 //            if (al.get(i).toString().charAt(0)=='王') {
107             /*subString,截取*/
108             if (al.get(i).toString().substring(0, 1).equals("王")) {
109                 al2.add(al.get(i));
110             }
111         }
112         al.removeAll(al2);
113         System.out.println(al);
114         
115         /*第二种方法*/
116         List al2 = new ArrayList();
117         //增强型for循环,可以删除对象。
118         ★★★对某对象进行foreach操作,不能对该对象进行remove,add操作,分两步★★★
119         for (Object o : al) {
120             if (o.toString().substring(0,1).equals("王")) {
121                 al2.add(o);
122             }
123         }
124         al.removeAll(al2);
125         System.out.println(al);
126     }
127 }
128 
129 范例2:
130 /**
131  * 1.把1到100中所有被3整除的数放在集合中
132  * 2.求这些数字的和
133  */
134 public class TestDemo {
135     public static void main(String[] args) {
136         ArrayList al = new ArrayList();
137         int sum = 0;
138         /**
139          * 存数据
140          */
141         for (int i = 1; i <= 100; i++) {
142             if (i % 3 == 0) {
143                 al.add(i);
144             }
145         }
146         System.out.println(al);
147         
148         /**
149          * 求和
150          */
151         for (int i = 0; i < al.size(); i++) {
152             sum += (Integer)al.get(i);
153         }
154         System.out.println(sum);
155     }
156 }
157 
158 范例3:
159 /**
160  * 剔除王姓和年龄小于20的学员
161  */
162 import java.util.Random;
163 public class Student {
164     private String name;
165     private int age;
166     public static final String[] xin = new String[]{"丁","王","徐","汤","独孤","李"};
167     public static final String[] ming = new String[]{"强","求败","建国","援朝","翠花","狗蛋","嫚","凯","程琳"};
168     
169     public Student() {
170         //随机姓名和年龄
171         Random rd = new Random();
172         this.name = xin[rd.nextInt(xin.length)] + ming[rd.nextInt(ming.length)];
173         this.age = rd.nextInt(20) + 10;
174     }
175     ★★集合中,toString一定要重写★★
176     /*toString本身是【对象@hashcode码】*/
177     @Override
178     public String toString() {
179         return "Student [name=" + name + ", age=" + age + "]";
180     }
181     
182     public String getName() {
183         return name;
184     }
185     public void setName(String name) {
186         this.name = name;
187     }
188     public int getAge() {
189         return age;
190     }
191     public void setAge(int age) {
192         this.age = age;
193     }
194 }
195 
196 【import有先后次序】
197 import java.util.*;
198 public class TestDemo3 {
199     public static void main(String[] args) {
200         List<Student> stuArr = new ArrayList<Student>();
201         for (int i = 0; i < 10; i++) {
202             stuArr.add(new Student());
203         }
204         System.out.println(stuArr);
205         
206         System.out.println("**********************************");
207         ★★★对某对象进行foreach操作,不能对该对象进行remove,add操作,分两步★★★
208         List<Student> delStu = new ArrayList<Student>();
209         try {
210             /*方法1*/
211             for (Student student : stuArr) {
212                 if ("王".equals(student.getName().substring(0, 1)) || student.getAge()>= 20) {
213                     delStu.add(student);
214                 }
215             }
216             stuArr.removeAll(delStu);
217             System.out.println(stuArr);
218             
219         } catch (Exception e) {
220             System.out.println("删除有误");
221             e.printStackTrace();
222         }
223     }
224 }
225 
226 -----------------------Set----------------------
227 set转换成ArrayList
228 用到对象,就要重写hashCode和equals方法,实现compareTo接口
229 1、HashSet
230 范例:
231 具体类
232 Collection(没有对象):对集合进行排序,遍历
233                         shuffle 打乱顺序
234 public class SetDemo {
235     public static void main(String[] args) {
236         Set hs = new HashSet();
237         Random rd = new Random();
238         for (int i = 0; i < 100; i++) {
239             if (hs.size() == 10) {
240                 break;
241             }
242             hs.add(String.valueOf(rd.nextInt(100)));
243         }
244         System.out.println(hs);
245         
246         ArrayList al = new ArrayList(hs);
247         /*Collections.sort 升序排列*/
248         Collections.sort(al);
249         
250         /*Collections.shuffle 乱序排列*/
251         Collections.shuffle(al);
252         System.out.println(al);
253     }
254 }
255 
256 public class SetDemo {
257     public static void main(String[] args) {
258         Set hs = new HashSet();
259         hs.add("1");
260         hs.add("1");
261         hs.add("2");
262         hs.add("1");
263         System.out.println(hs);
264         /*方法1*/
265         ArrayList al = new ArrayList(hs);
266         System.out.println(al.get(0));
267         /*方法2*/
268         Object[] hs1 = hs.toArray();
269         System.out.println(hs1[0]);
270     }
271 }
272 
273 2、TreeSet 自动升序排列
274 public class SetDemo {
275     public static void main(String[] args) {
276         Set ts = new TreeSet();
277         ts.add(2);
278         ts.add(9);
279         ts.add(9);
280         ts.add(5);
281         ts.add(2);
282         
283         //字母挨个比
284         ts.add("apple");
285         ts.add("hello");
286         ts.add("very");
287         ts.add("apple");
288         ts.add("happy");
289         
290         /*Double.doubleToRawLongBits(price) != Double.doubleToRawLongBits(price1) 比double*/
291         ts.add(2.23);
292         ts.add(2.236);
293         ts.add(2.230);
294         ts.add(2.6);
295         ts.add(2.6);
296         
297         //前面false,后面true
298         ts.add(true);
299         ts.add(false);
300         ts.add(true);
301         ts.add(true);
302         ts.add(false);
303         
304         System.out.println(ts);
305     }
306 }
307 
308 -------------------泛型----------------
309 JDK1.5以后的版本
310 限制参数类型
311     
312 ---------------------Iteator----------------
313 一看到集合,不需要经过思考,就用Iteator遍历
314 Collecton继承Iteator,所以Collecton都可以用Iteator
315 
316 -----------------------Map-------------------
317 范例:
318 public class MapDemo {
319     public static void main(String[] args) {
320         //Map接口实现的类HashMap
321         Map hm = new HashMap();
322         /**
323          * K,V都是对象
324          */
325         hm.put(1, "张三");
326         hm.put(2, "张三");
327         hm.put(3, "张学友");
328         System.out.println(hm);
329         
330         hm.remove(1);
331         
332         //根据key得到value
333         String a = hm.get(2).toString();
334         System.out.println(a);
335         //键的集合
336         Set b = hm.keySet();
337         System.out.println(b);
338         //值的集合
339         Collection c = hm.values();
340         System.out.println(c);
341     }
342 }
343 
344 Properties    创建txt文档
345 ★★★类名不能和关键字,或与系统默认的名重名★★★
346 范例:
347 public class PropertiesDemo {
348     public static void main(String[] args) throws FileNotFoundException, IOException {
349         Properties pro = new Properties();
350         pro.setProperty("1", "王寅");
351         pro.setProperty("2", "李四");
352         pro.setProperty("3", "张三");
353         
354         /*方法1*/
355 //        pro.store(new FileOutputStream(new File("D:\a.txt")), "存姓名");
356 
357         /*方法2*/
358 //        pro.store(new FileOutputStream(new File("D:" + File.separator +"\c.txt")), "存姓名");
359         
360         /*方法3*/
361         pro.load(new FileInputStream(new File("D:" + File.separator +"\c.txt")));
362         System.out.println(pro.getProperty("2"));
363     }
364 }
原文地址:https://www.cnblogs.com/ivy-xu/p/5330561.html