好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

判断List和Map是否相等并合并List中相同的Map

list、set、map判断两个对象相等的标准

list:通过equals()方法比较返回true即可。 hashset:先比较两个对象hashcode()方法返回的值是否相等,如果不相等就认为两个对象是不相等的,如果两个对象的hashcode相等就继续调用equals()方法进一步判断两个对象是否相等,如果equals()方法返回true认为两个对象相等,返回false认为两个对象不相等。 treeset:两个对象通过compareto(object obj)方法比较是否返回0:如果返回0,则认为相等,否则不相等。 hashmap、hashtable:(1)两个key通过equals()方法比较返回true,两个key的hashcode值也相等;(2)value与另外一个对象通过equals()方法比较返回true即可。 treemap:两个key值通过compareto()方法返回0,treemap即认为这两个key是相等的。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

/**

    * 根据特定规格,判断两个map是否相等

    */

   private static boolean isequals(map<string, string> src, map<string, string> dest, string[] samekey) {

     boolean equals = true ;

     stringbuffer sbf_src = new stringbuffer();

     stringbuffer sbf_dest = new stringbuffer();

     for ( int i = 0 ; i < samekey.length; i++) {

       sbf_src.append(src.get(samekey[i]));

       sbf_dest.append(dest.get(samekey[i]));

     }

     if (sbf_src.tostring().equals(sbf_dest.tostring())) {

       equals = true ;

     } else {

       equals = false ;

     }

     return equals;

   }

   /**

    * 获得list中有没有相同的keymap(待需找的map)<br>

    * 如果找到则返回这个list和keymap相同map的下标,否则返回-1

    */

   private static int getequalsmap(list<map<string, string>> list, map<string, string> keymap, string[] samekey) {

     int equalsindex = - 1 ;

     for ( int i = 0 ; i < list.size(); i++) {

       map<string, string> tempmap = list.get(i);

       if (isequals(tempmap, keymap, samekey)) {

         equalsindex = i;

       }

     }

     return equalsindex;

   }

   /**

    * 合并list中相同的map

    * @param list

    * @return

    */

   public static list<map<string, string>> combinelist(list<map<string, string>> list, string[] samekey,string combinekey) {

     list<map<string, string>> retlist = new arraylist<map<string, string>>();

     for ( int i = 0 ; i < list.size(); i++) {

       map<string, string> tempmap = list.get(i);

       int equalsindex = getequalsmap(retlist, tempmap, samekey);

       if (- 1 == equalsindex) {

         retlist.add(tempmap);

       } else {

         string custsrc = retlist.get(equalsindex).get(combinekey);

         int custsrcint = integer.parseint(custsrc.substring( 0 , custsrc.length() - 1 ));

         string custtemp = tempmap.get(combinekey);

         int custtempint = integer.parseint(custtemp.substring( 0 , custtemp.length() - 1 ));

         string destcust = (custsrcint + custtempint) + custsrc.substring(custsrc.length() - 1 );

         retlist.get(equalsindex).put(combinekey, destcust);

       }

     }

     return retlist;

   }

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/moneyshi/article/details/41081313

查看更多关于判断List和Map是否相等并合并List中相同的Map的详细内容...

  阅读:20次