Java中怎样遍历Map的所有的元素

Java中怎样遍历Map的所有的元素,第1张

JDK 中

view plaincopy to clipboardprint    <FONT color=# ff>Map map = new HashMap();

Iterator it = map entrySet(erator();

while (it hasNext()) {

Map Entry entry = (Map Entry) it next();

Object key = entry getKey();

Object value = entry getValue();

}</FONT>

Map map = new HashMap();

Iterator it = map entrySet(erator();

while (it hasNext()) {

Map Entry entry = (Map Entry) it next();

Object key = entry getKey();

Object value = entry getValue();

}JDK 中 应用新特性For Each循环

view plaincopy to clipboardprint    Map m = new HashMap();

for(Object o : map keySet()){

map get(o);

}

Map m = new HashMap();

for(Object o : map keySet()){

map get(o);

}返回的 set 中的每个元素都是一个 Map Entry 类型

view plaincopy to clipboardprint    <FONT color=# ff>private Hashtable<String String> emails = new Hashtable<String String>();</FONT>

private Hashtable<String String> emails = new Hashtable<String String>();  另外 我们可以先把hashMap 转为集合Collection 再迭代输出 不过得到的对象

view plaincopy to clipboardprint     <FONT color=# ff>//方法一: 用entrySet()

Iterator it = emails entrySet(erator();

while(it hasNext()){

Map Entry m=(Map Entry)it next();

( email + m getKey() + : + m getValue());

}

// 方法二 jdk 支持 用entrySet()和For Each循环()

for (Map Entry<String String> m : emails entrySet()) {

( email + m getKey() + : + m getValue());

}

// 方法三 用keySet()

Iterator it = emails keySet(erator();

while (it hasNext()){

String key;

key=(String)it next();

( email + key + : + emails get(key));

}

// 方法五 jdk 支持 用keySEt()和For Each循环

for(Object m: emails keySet()){

( email + m+ : + emails get(m));

}    </FONT>

//方法一: 用entrySet()

Iterator it = emails entrySet(erator();

while(it hasNext()){

Map Entry m=(Map Entry)it next();

( email + m getKey() + : + m getValue());

}

// 方法二 jdk 支持 用entrySet()和For Each循环()

for (Map Entry<String String> m : emails entrySet()) {

( email + m getKey() + : + m getValue());

}

// 方法三 用keySet()

Iterator it = emails keySet(erator();

while (it hasNext()){

String key;

key=(String)it next();

( email + key + : + emails get(key));

}

// 方法五 jdk 支持 用keySEt()和For Each循环

for(Object m: emails keySet()){

( email + m+ : + emails get(m));

}

Map    aa    =    new    HashMap();      aa put( tmp     new    Object());      //追加      替换用同样的函数       aa remove( temp );                        //删除      for    (Iterator    i    =    aa values(erator();    i hasNext();    )    {              Object    temp    =    i next();      }          //遍历    来个完整的 包含TreeSet的元素内部排序的

view plaincopy to clipboardprint    public static void main(String[] args) {

ArrayList<String> list = new ArrayList<String>();

HashMap<Object Object> hash = new HashMap<Object Object>();

TreeMap<Object Object> treeMap = new TreeMap<Object Object>();

list add( a );

list add( b );

list add( c );

hash put( );

hash put( );

hash put( );

hash put( );

hash put( );

hash put( );

treeMap put( );

treeMap put( );

treeMap put( );

treeMap put( );

treeMap put( );

treeMap put( );

//list遍历

for(String m: list){

System out println(m);

}

// hashmap entrySet() 遍历

for(Map Entry<Object Object> m: hash entrySet()){

System out println(m getKey()+ +m getValue());

}

//hashmap keySet() 遍历

for(Object m: hash keySet()){

System out println(m+ +hash get(m));

}

// treemap keySet()遍历

for(Object m: treeMap keySet()){

System out println(m+ +treeMap get(m));

}

lishixinzhi/Article/program/Java/hx/201311/25783

给你一段代码你就会用了!
package customdao;
import javasqlConnection;
import javasqlResultSet;
import javasqlStatement;
import javautilArrayList;
import javautilList;
import customstrutsformSellerForm;
import customstrutsformCustomForm;
import customdaoConnectionManager;
public class SellerDao {
/针对seller表/
//1,查询
public static List findAll(Connection conn){
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try{
conn = ConnectionManagergetConnection();
stmt = conncreateStatement();
rs = stmtexecuteQuery("select from seller where seller_power='2'");
while(rsnext()){
SellerForm sf = new SellerForm();
sfsetSeller_name(rsgetString("seller_name"));
sfsetSeller_psw(rsgetString("seller_psw"));
sfsetSeller_power(rsgetString("seller_power"));
sfsetSeller_id(rsgetString("seller_id"));
listadd(sf);
}
}catch(Exception e){
eprintStackTrace();
}finally{
ConnectionManagercloseConnection(conn);
}
return list;
}
public static List findId(String id){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try{
stmt = conncreateStatement();
rs = stmtexecuteQuery("select from seller where seller_id='"+id+"'");
while(rsnext()){
SellerForm sf = new SellerForm();
sfsetSeller_id(rsgetString("seller_id"));
sfsetSeller_name(rsgetString("seller_name"));
sfsetSeller_power(rsgetString("seller_power"));
sfsetSeller_psw(rsgetString("seller_psw"));
listadd(sf);
}
}catch(Exception e){
eprintStackTrace();
}finally{
ConnectionManagergetConnection();
}
return list;
}
//2,删除
public static int delete(String id){
Connection conn = null;
Statement stmt = null;
int flag = 0;
try{
conn = ConnectionManagergetConnection();
stmt = conncreateStatement();
flag = stmtexecuteUpdate("delete seller where seller_id='"+id+"'");
}catch(Exception e){
eprintStackTrace();
}finally{
ConnectionManagergetConnection();
}
return flag;
}
//3,修改
public static int update(String id,String name,String psw){
Connection conn = null;
Statement stmt = null;
int n = 0;
try{
stmt = conncreateStatement();
n = stmtexecuteUpdate("update seller set seller_name='"+name+"',seller_psw='"+psw+"' where seller_id="+id);
}catch(Exception e){
eprintStackTrace();
}finally{
ConnectionManagergetConnection();
}
return n;
}
//4,插入
public static int insert(String name,String psw){
Connection conn = null;
Statement stmt = null;
int i = 0;
try{
stmt = conncreateStatement();
i = stmtexecuteUpdate("insert into seller (select max(seller_id)+1 ,"+name+","+psw+",'2' from seller)");
}catch(Exception e){
eprintStackTrace();
}finally{
ConnectionManagergetConnection();
}
return i;
}

//处理登录这个模块
public static String login(String name,String psw,Connection conn){
Statement stmt = null;
ResultSet rs = null;
String power = "";
try{
stmt = conncreateStatement();
rs = stmtexecuteQuery("select from seller where seller_name='"+name+"' and seller_psw='"+psw+"'");
//如果查询出姓名密码来了就获取权限值
if(rsnext()){
power = rsgetString("seller_power");
}
}catch(Exception e){
eprintStackTrace();
}finally{
ConnectionManagergetConnection();
}
return power;
}
/针对custom表/
//1,查询
public static List findAllCustom(Connection conn){
Statement stmt = null;
ResultSet rs= null;
String sql1 = "";
String sql2 = "";
List list = new ArrayList();
try{
stmt=conncreateStatement();
sql1 = "select seller_name,customer_name,customer_sex,customer_email,customer_phone ";
sql2 = "from customer,seller where customerseller_id=sellerseller_id and customer_state='1'";
rs = stmtexecuteQuery(sql1+sql2);
while(rsnext()){
CustomForm c = new CustomForm();
csetSeller_name(rsgetString("seller_name"));
csetCustomer_name(rsgetString("customer_name"));
csetCustomer_sex(rsgetString("customer_sex"));
csetCustomer_email(rsgetString("customer_email"));
csetCustomer_phone(rsgetString("customer_phone"));
listadd(c);
}
}catch(Exception e){
eprintStackTrace();
}finally{
ConnectionManagergetConnection();
}
return list;
}
public static List dispaterSeller(Connection conn){
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try{
stmt = conncreateStatement();
String sql = "select from seller where seller_power='2'";
rs = stmtexecuteQuery(sql);
while(rsnext()){
SellerForm sf = new SellerForm();
sfsetSeller_name(rsgetString("seller_name"));
sfsetSeller_id(rsgetString("seller_id"));
listadd(sf);
}
}catch(Exception e){
eprintStackTrace();
}finally{
ConnectionManagergetConnection();
}
return list;
}
public static List dispaterCustom(Connection conn){
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try{
stmt = conncreateStatement();
String sql = "select from customer where customer_state='0'";
rs = stmtexecuteQuery(sql);
while(rsnext()){
CustomForm cf = new CustomForm();
cfsetCustomer_name(rsgetString("customer_name"));
cfsetCustomer_id(rsgetString("customer_id"));
listadd(cf);
}
}catch(Exception e){
eprintStackTrace();
}finally{
ConnectionManagergetConnection();
}
return list;
}
public static void dispater(String sid,String cid,Connection conn,int count,int i){
Statement stmt = null;
try{
stmt = conncreateStatement();
stmtexecuteUpdate("update customer set seller_id='"+sid+"',customer_state='1' where customer_id ="+cid);
}catch(Exception e){
eprintStackTrace();
}finally{
if(count==i){
ConnectionManagergetConnection();
}
}
}
}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13398285.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-28
下一篇 2023-07-28

发表评论

登录后才能评论

评论列表(0条)

保存