LIst和map的遍历

1.

public static void main(String[] args) {
    // ArrayList类实现一个可增长的动态数组
    List<String> list = new ArrayList<String>();
    // 插入元素
    list.add("list1");
    list.add("list2");
    // 打印list的大小
    System.out.println(list.size());
    // 按索引移除元素
    list.remove(0);
    // 按对象移除元素
    list.remove("list2");
    // 打印list的大小
    System.out.println(list.size());
    // 清空list
    list.clear();
}
View Code

2.linkedList

public static void main(String[] args) {
    // LinkedList类实现了链表,可初始化化为空或者已存在的集合
    LinkedList<String> list = new LinkedList<String>();
    // 插入元素
    list.add("list2");
    list.add("list3");
    // 向链表头插入数据
    list.addFirst("list1");
    // 向链表尾插入数据
    list.addLast("list4");
    for (String str : list) {
        System.out.println(str);
    }
    // 获取链表头数据
    System.out.println("链表头数据:" + list.getFirst());
    // 获取链表尾数据
    System.out.println("链表尾数据:" + list.getLast());
}
View Code

排序

public static void main(String[] args) {
    // 创建list
    List<Integer> list = new ArrayList<Integer>();
    // 插入元素
    list.add(2);
    list.add(0);
    list.add(3);
    list.add(4);
    list.add(1);
    Collections.sort(list);
    for (int i : list) {
        System.out.println(i);
    }
}
View Code
public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<String>();
    list.add("一鸣惊人-Y");
    list.add("人山人海-R");
    list.add("海阔天空-H");
    list.add("空前绝后-K");
    list.add("后来居上-H");
    Comparator<Object> cmp = Collator.getInstance(java.util.Locale.CHINA);
    Collections.sort(list, cmp);
    for (String str : list) {
        System.out.println(str);
    }
}
View Code
package net.xsoftlab.baike;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TextList {
    public static void main(String[] args) {
        List<User> userlist = new ArrayList<User>();
        userlist.add(new User("Y - 易小星 ", 33));
        userlist.add(new User("W - 王大锤", 33));
        Comparator<User> cmp = new ComparatorUser();
        Collections.sort(userlist, cmp);
        for (User user : userlist) {
            System.out.println(user.getName());
        }
    }
}
class ComparatorUser implements Comparator<User> {
    @Override
    public int compare(User u1, User u2) {
        // 先按年龄排序
        int flag = u1.getAge().compareTo(u2.getAge());
        // 年龄相等比较姓名
        if (flag == 0) {
            return u1.getName().compareTo(u2.getName());
        } else {
            return flag;
        }
    }
}
class User {
    private String name;
    private Integer age;
    public User() {
        super();
    }
    public User(String name, Integer age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}
View Code

遍历

public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    // 插入元素
    list.add("list1");
    list.add("list2");
    list.add("list3");
    System.out.println("第一种遍历方法 - >");
    for (String str : list) {
        System.out.println(str);
    }
    System.out.println("第二种遍历方法 - >");
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }
    System.out.println("第三种遍历方法 - >");
    Iterator<String> iter = list.iterator();
    while (iter.hasNext()) {
        System.out.println(iter.next());
    }
}
View Code

遍历时移除元素

package net.xsoftlab.baike;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class RemoveItemFromList {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        // 插入元素
        list.add("list1");
        list.add("list2");
        list.add("list2");
        list.add("list3");
        // 实例化新的list防止因传递地址而达不到测试效果。
        remove1(new ArrayList<String>(list));
        remove2(new ArrayList<String>(list));
        remove2_1(new ArrayList<String>(list));
        remove3(new ArrayList<String>(list));
    }
    public static void remove1(List<String> list) {
        System.out.print("第一种方法 - > ");
        try {
            for (String str : list) {
                if (str.equals("list2"))
                    list.remove(str);
            }
        } catch (Exception e) {
            System.out.println("移除失败!");
        }
    }
    public static void remove2(List<String> list) {
        System.out.print("第二种方法 - > ");
        for (int i = 0; i < list.size(); i++) {
            String str = list.get(i);
            if (str.equals("list2"))
                list.remove(str);
        }
        System.out.println(list);
        System.out.println("也有异常,可以用下面的方法避免。");
    }
    public static void remove2_1(List<String> list) {
        System.out.print("第二种方法修正 - > ");
        for (int i = 0; i < list.size(); i++) {
            String str = list.get(i);
            if (str.equals("list2")) {
                list.remove(str);
                // 因移除了元素,位置发生偏移,需要重新对当前位置的元素进行判断。
                i--;
            }
        }
        System.out.println(list);
    }
    public static void remove3(List<String> list) {
        System.out.print("第三种方法 - > ");
        Iterator<String> iter = list.iterator();
        while (iter.hasNext()) {
            String str = iter.next();
            if (str.equals("list2"))
                iter.remove();
        }
        System.out.println(list);
    }
}
View Code

map4中遍历

 

public static void main(String[] args) {


  Map<String, String> map = new HashMap<String, String>();
  map.put("1", "value1");
  map.put("2", "value2");
  map.put("3", "value3");
  
  //第一种:普遍使用,二次取值
  System.out.println("通过Map.keySet遍历key和value:");
  for (String key : map.keySet()) {
   System.out.println("key= "+ key + " and value= " + map.get(key));
  }
  
  //第二种
  System.out.println("通过Map.entrySet使用iterator遍历key和value:");
  Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
  while (it.hasNext()) {
   Map.Entry<String, String> entry = it.next();
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }
  
  //第三种:推荐,尤其是容量大时
  System.out.println("通过Map.entrySet遍历key和value");
  for (Map.Entry<String, String> entry : map.entrySet()) {
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }

  //第四种
  System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
  for (String v : map.values()) {
   System.out.println("value= " + v);
  }
 }
View Code

 

posted @ 2016-07-27 22:35  咖喱不见不散啊  阅读(192)  评论(0编辑  收藏  举报