- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
- /*定义集合A:["2007","2008"] , B["20071555","20089087","20081234","20200801"],
- 然后在B中找出前4位与集合A中相同的字符串
- 将找出的字符串以键值对(A:key,B:value)的方式保存.*/
- public class MapTest0 {
- public static void main(String[] args){
- HashMap<String, String> m = new HashMap<String, String>();
- ArrayList<String> a = new ArrayList<String>();
- a.add("2007");
- a.add("2008");
- ArrayList<String> b = new ArrayList<String>();
- b.add("20071555");
- b.add("20089087");
- b.add("20081234");
- b.add("20200801");
- for (int i = 0; i < 2; i++) {
- for (int j = 0; j < 4; j++) {
- if(compareTo(a.get(i), b.get(j))){
- m.put(a.get(i), b.get(j));
- }
- }
- }
- Set entryset = m.entrySet();
- Iterator iterator = entryset.iterator();
- for (Object object : entryset) {
- Map.Entry entry = (Entry)object;
- System.out.println(entry.getKey()+"="+entry.getValue());
- }
-
- }
- static boolean compareTo(String s1,String s2){
- boolean bl = false ;
- if (s1.charAt(0)==s2.charAt(0)&&s1.charAt(1)==s2.charAt(1)&&s1.charAt(2)==s2.charAt(2)&&s1.charAt(3)==s2.charAt(3)) {
- bl = true;
- }
- return bl;
- }
-
- }
复制代码
结果:
2008=20081234
2007=20071555
|