Java集合按照其存储结构可以分为 两大类,即单列集合Collection和双列结合Map
Collection 单列集合类的根接口,其有两个重要子接口 List 和Set 。List 相当于OC的NSArray 。Set相当于OC的NSSet.
List 接口的主要类有 ArrayList 和 LinkedList
Set 接口的主要类有 HashMap 和 TreeMap
Map 双列集合的根接口 相当于OC中的字典。
主要实现类有HashMap 和 TreeMap
1.ArrayList
public static void testArrayList()
{
ArrayList<String> list = new ArrayList<String>();
list.add("stu1");
list.add("stu2");
list.add("stu3");
list.add("stu4");
System.out.println("集合的长度"+ list.size());
System.out.println("第二个元素"+ list.get(1));
}
集合的长度4
第二个元素stu2
2 LinkedList
public static void testLinkedList()
{
LinkedList<String> list = new LinkedList<String>();
list.add("stu1");
list.add("stu2");
list.add("stu3");
list.add("stu4");
System.out.println(list.toString());
list.addFirst("first");
System.out.println(list);
System.out.println(list.getFirst());
list.remove(3);
}
3. Iterator 接口
public static void testIterator()
{
ArrayList<String> list = new ArrayList<String>();
list.add("stu1");
list.add("stu2");
list.add("stu3");
list.add("stu4");
Iterator<String> it =list.iterator();
while(it.hasNext())
{
Object obj = it.next();
System.out.println(obj);
}
}
4 foreach 循环
public static void testForeach()
{
ArrayList<String> list = new ArrayList<String>();
list.add("stu1");
list.add("stu2");
list.add("stu3");
list.add("stu4");
for (Object obj:list){
System.out.println(obj);
}
}
5 HashSet
class Student{
String id;
String name;
public Student(String id ,String name)
{
this.id = id;
this.name = name;
}
public String toString()
{
return id +":"+name;
}
}
public static void testHashSet()
{
HashSet<Student> hs = new HashSet<Student>();
Student stu1 = new Student("1","kangxg");
Student stu2 = new Student("2","kangxl");
Student stu3 = new Student("2","kangxl");
hs.add(stu1);
hs.add(stu2);
hs.add(stu3);
System.out.println(hs);
}
这时候 数据会有重复,为了要数据不存在重复,需要重写 hashCode()和equals()两个方法。
class Student{
String id;
String name;
public Student(String id ,String name)
{
this.id = id;
this.name = name;
}
public String toString()
{
return id +":"+name;
}
public int hashCode()
{
return id.hashCode();
}
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if( !(obj instanceof Student))
{
return false;
}
Student stu = (Student)obj;
boolean b = this.id.equals(stu.id);
return b;
}
}
6 Map
6.1 HashMap
public static void testHashMap()
{
Map<String, String> map = new HashMap<String, String>();
map.put("1", "Jack");
map.put("2", "lucy");
map.put("3", "tom");
System.out.println("1"+ map.get("1"));
System.out.println("2"+ map.get("2"));
System.out.println("3"+ map.get("3"));
Set<String> keySet = map.keySet();
Iterator<String> it = keySet.iterator();
while(it.hasNext())
{
Object key = it.next();
Object value = map.get(key);
System.out.println(key+":"+ value);
}
}
HashMap 存放是无序的,如果要让有序存放 可使用 LinkedHashMap 类
6.2 Properties 主要用来存储字符串类型的键和值,实际开发中经常用来存取应用的配置项
public static void testProperties()
{
Properties p = new Properties();
p.setProperty("bg", "red");
p.setProperty("font", "14px");
p.setProperty("lg", "chinese");
Enumeration<?> names = p.propertyNames();
while(names.hasMoreElements())
{
String key = (String) names.nextElement();
String value = p.getProperty(key);
System.out.println(key +"="+value);
}
}