Home Java javaTutorial Traversal methods of Java collections Set, List, and Map

Traversal methods of Java collections Set, List, and Map

Jan 22, 2017 pm 03:14 PM

The examples in this article describe the traversal methods of Java collections Set, List, and Map, and share them with you for your reference.

The specific methods are as follows:

package com.shellway.javase;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
 
import org.junit.Test;
 
public class TestCollection {
   
  public static void print(Collection<? extends Object> c){
    Iterator<? extends Object> it = c.iterator();
    while (it.hasNext()) {
      Object object = (Object) it.next();
      System.out.println(object);
    }
  }
   
  @Test
  public void demo1(){
    Set<String> set = new HashSet<String>();
    set.add("AAA");
    set.add("BBB");
    set.add("CCC");
    print(set);
     
    //Set的第一种遍历方式:利用Iterator
    Iterator<String> it1 = set.iterator();
    for (String ss : set) {
      System.out.println(ss);
    }
    //Set的第一种遍历方式:利用foreach
    for (String sss : set) {
      System.out.println(sss);
    }
     
    List<String> list = new ArrayList<String>();
    list.add("DDDDD");
    list.add("EEEEE");
    list.add("FFFFF");
    print(list);
     
    //List的第一种遍历方式:因为list有顺序,利用size()和get()方法获取
    for (int i = 0; i < list.size(); i++) {
      System.out.println(list.get(i));
    }
    //List的第二种遍历方式:利用Iterator
    Iterator<String> it = list.iterator();
    while (it.hasNext()) {
      System.out.println(it.next());
    }
    //List的第三种遍历方式:利用foreach
    for (String s2 : list) {
      System.out.println(s2);
    }
     
    Map<String,String> map = new TreeMap<String, String>();
    map.put("Jerry", "10000");
    map.put("shellway", "20000");
    map.put("Kizi", "30000");
    print(map.entrySet());
    //Map的第一种遍历方式:先获得key,再获得值value
    Set<String> sett = map.keySet();
    for (String s : sett) {
      System.out.println(s+":"+map.get(s));
    }
    //Map的第二种遍历方式:获得键值对
    for (Map.Entry<String, String> entry : map.entrySet()) {
      System.out.println(entry.getKey()+" : "+entry.getValue());
    }
  }
}
Copy after login

For more articles related to the traversal methods of Java collection Set, List, and Map, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)