Java类集 List, Set, Map, Stack, Properties基本使用

时间:2023-11-17 15:37:56

首先看下继承结构:

Java类集  List, Set, Map, Stack, Properties基本使用

ArrayList(常用):

  /**
* List接口继承Collection接口
* ArrayList, Vector为List接口的实现类
* add()添加新元素,remove()删除指定位置元素,get()通过索引获取对应位置元素,set()设置索引位置元素
* Iterator(最常用)接口实现集合遍历
*/
List list = new ArrayList<String>();
list.add("Hello aa");
list.add("Hello bb");
list.add("Hello cc");
list.add("Hello dd");
list.add("Hello dd");
list.add("Hello dd");
list.remove(0);
System.out.println(list);
System.out.println(list.get(0));
list.set(0, "nihao");
System.out.println(list.get(0));
Iterator iter = list.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}

Vector(旧版):

 /**
* List接口继承Collection接口
* ArrayList, Vector为List接口的实现类
* add()添加新元素,remove()删除指定位置元素,get()通过索引获取对应位置元素,set()设置索引位置元素
* Iterator(最常用)接口实现集合遍历
*/
List list = new Vector<String>();
list.add("Hello aa");
list.add("Hello bb");
list.add("Hello cc");
list.add("Hello dd");
list.add("Hello dd");
list.add("Hello dd");
list.remove(0);
System.out.println(list);
System.out.println(list.get(0));
list.set(0, "nihao");
System.out.println(list.get(0));
Iterator iter = list.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}

两者的主要区别:

ArrayList是JDK1.2新加入的, Vector在JDK1.0中就已经出现, Vector是同步的, 所以是线程安全的(当然性能会低),ArrayList是异步的,所以线程不安全,日常开发中Vector已经很少用了,ArrayList更常用一些,两者的用法基本一致。

HashSet, TreeSet:

 /**
* Set(集合)是不重复的, Collection集合的子接口
*/
Set hashSet = new HashSet<String>();
hashSet.add("1111");
hashSet.add("1111");
hashSet.add("2222");
hashSet.add("3333");
hashSet.add("X");
hashSet.add("C");
hashSet.add("E");
hashSet.add("A");
System.out.println(hashSet); // 发现HashSet是无序的
Set treeSet = new TreeSet<String>();
treeSet.add("1111");
treeSet.add("1111");
treeSet.add("2222");
treeSet.add("3333");
treeSet.add("X");
treeSet.add("C");
treeSet.add("E");
treeSet.add("A");
System.out.println(treeSet); // 发现TreeSet是有序的

用HashSet, TreeSet存储自定义类Book:

Book类:

 public class Book {
private String title;
private double price;
public Book(){
this("", 0.0);
}
public Book(String title, double price){
this.title = title;
this.price = price;
}
}

执行如下代码:

 public class Ph {
public static void main(String[] args) {
Set treeSet = new TreeSet<String>();
treeSet.add(new Book("Java开发", 29.8));
treeSet.add(new Book("Java开发", 29.8));
treeSet.add(new Book("JSP开发", 39.8));
treeSet.add(new Book("Oracle开发", 79.8));
System.out.println(treeSet);
}
}

运行时异常:

Exception in thread "main" java.lang.ClassCastException: MyPackageOne.Book cannot be cast to java.lang.Comparable
看到Comparable明白TreeSet通过Comparable接口实现让元素不重复和排序,所以运用TreeSet存储自定义类时应实现Comparable接口并实现CompareTo方法

故Book类此时应为:

 public class Book implements Comparable<Book>{
private String title;
private double price;
public Book(){
this("", 0.0);
}
public Book(String title, double price){
this.title = title;
this.price = price;
} @Override
public int compareTo(Book o) {
if(price > o.price){
return 1;
}else if(price < o.price){
return -1;
}else{
// 注意应该把所有元素的比较填入, 不然有一个属性相同可能就会误以为相同元素
return title.compareTo(o.title);
}
}
}

而此时换成HashSet,发现会有重复元素,因为HashSet通过HashCode()和equals()方法实现去重,故此时Book类应为:

 import java.util.Objects;

 public class Book {
private String title;
private double price; public Book() {
this("", 0.0);
} public Book(String title, double price) {
this.title = title;
this.price = price;
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return Double.compare(book.price, price) == 0 &&
Objects.equals(title, book.title);
} @Override
public int hashCode() {
return Objects.hash(title, price);
} @Override
public String toString() {
return title + price;
}
}

HashMap,Hashtable:

 Map map = new HashMap<String, Integer>();
map.put("Hello", 1);
map.put("Hello", 100);
map.put("world", 100);
map.put("Java", 100);
map.put(null, 100);
System.out.println(map.get("Hello"));
System.out.println(map.get("Jav"));
System.out.println(map.get(null));

运行结果:

100
null
100

 Map map = new Hashtable<String, Integer>();
map.put("Hello", 1);
map.put("Hello", 100);
map.put("world", 100);
map.put("Java", 100);
map.put(null, 100);
System.out.println(map.get("Hello"));
System.out.println(map.get("Jav"));
System.out.println(map.get(null));

运行结果:

Exception in thread "main" java.lang.NullPointerException
at java.util.Hashtable.put(Hashtable.java:465)
at MyPackageOne.Ph.main(Ph.java:12)

将null去掉:

 map.put("Hello", 1);
map.put("Hello", 100);
map.put("world", 100);
map.put("Java", 100);
System.out.println(map.get("Jav"));

运行结果:

null

总结:HashMap和Hashtable区别:

Hashtable为JDK1.0时存在的Map实现类, HashMap为JDK1.2时新加入的Map实现类,HashMap可以设置null而Hashtable不能,Hashtable为同步的线程安全,不推荐使用,HashMap为异步线程非安全,两者用法基本一致。

遍历HashMap(Hashtable一样):

 Map map = new Hashtable<String, Integer>();
map.put("Hello", 1);
map.put("world", 100);
map.put("Java", 100);
/**
* 方法一
* 通过keySet()方法获取key集合
*/
Set keySet = map.keySet();
Iterator iter = keySet.iterator();
while (iter.hasNext()){
System.out.println(iter.next());
}
/**
* 方法二
* 通过Map.Entry接口
*/
Set<Map.Entry<String, Integer>> set = map.entrySet();
Iterator<Map.Entry<String, Integer>> iterator = set.iterator();
while (iterator.hasNext()){
Map.Entry<String, Integer> mapEntry = iterator.next();
System.out.println(mapEntry.getKey() + mapEntry.getValue());
}

特别的, Map可以以自定义类为key或value,当作key时,比如之前的Book类(注释掉hashCode和equals方法)

 Map map = new HashMap<Book, String>();
map.put(new Book("Java", 10.9), "1");
map.put(new Book("Java", 10.9), "1");
map.put(new Book("Java", 11.9), "1");
System.out.println(map);

运行结果为:{Java10.9=1, Java10.9=1, Java11.9=1}

发现有重复元素, 去掉hashCode和equals方法注释:

此时运行结果:{Java10.9=1, Java11.9=1}

总结:用HashMap,Hashtable时如果自定义类作为key需要加上hashCode和equals方法

Stack:

  /**
* Stack为Vector子类,但一般用法与Vector无关
* 常用push,pop,size方法
*/
Stack stack = new Stack<String>();
stack.push("11");
stack.push("22");
stack.push("33");
System.out.println(stack.size());
System.out.println(stack.get(stack.size() - 1));
stack.pop();
System.out.println(stack.get(stack.size() - 1));

Properties:

 /**
* Properties类为Hashtable子类
* 不用设置泛型因为只能用String,String
* 常用setProperty方法设置属性, getProperty方法获得属性
* store方法存入文件中,load方法从文件中取出
*/
Properties properties = new Properties();
properties.setProperty("123", "456");
properties.setProperty("1", "4");
properties.setProperty("12", "45");
properties.setProperty("123", "456");
System.out.println(properties.getProperty("123"));
properties.store(new FileOutputStream(new File("key.txt")), "key-info");
properties.load(new FileInputStream(new File("key.txt")));

Collections:

 /**
* Collection和Collections的区别:
* Collection是集合的接口,Collections是接口的工具类,提供一些方法
* 如:addAll, reverse, replaceAll等方法
*/
Collections.addAll(list, "1", "2", "3");
Collections.reverse(list);
System.out.println(list);
Collections.replaceAll(list, "1", "2");
System.out.println(list);