Excel列比较和匹配

时间:2017-11-09 17:45:08

标签: excel

我有一个报告,显示ID#和与这些ID对应的名称。

我正在尝试编写一个检查ID#的公式,看看相同的ID#是否具有相同的名称;

如果多次列出相同的ID#并且它们具有不同的名称,那么它应该显示ID和名称不匹配。

这个想法是相同的ID#应该具有相同的名称;如果相同的ID具有不同的名称,则应显示该ID具有不同的名称。

Col A     Col B       Col C

ID        Name         Desired Output
3         Peter        ID with multiple names
3         Ken          ID with multiple names
5         Chris        match
5         Chris        match
5         Chris        match
6         Dave         match
6         Dave         match
7         Lisa         match
8         Mark         match
10        Ken          match 
12        Frank        ID with Multiple names
12        Randy        ID with Multiple names  
12        Frank        ID with Multiple names 
12        Mike         ID with Multiple names  

enter image description here

1 个答案:

答案 0 :(得分:2)

使用COUNTIFS():

public static void main(String... args){
        try{
            new ClassA().method1();

            TrackingThreadLocal.tracker.get().entrySet().stream().forEach( (e) -> System.out.println( 
            "the return of " + e.getKey() + " is generated from the return of " + e.getValue().stream().collect( Collectors.joining(", ") ) ) );

        }finally{
            //make sut to clean up to avoid ThreadLocal memoty leak
            TrackingThreadLocal.tracker.remove();
        }
    }

    public class TrackingThreadLocal{

        public static ThreadLocal< Map<String, List<String> > > tracker = new ThreadLocal< Map< String, List<String> > >(){
            @Override 
            public Map< String, List<String> > initialValue() {
                return new HashMap<>();
            }
        };
    }

    public class ClassA {
        ClassB classB= new ClassB();
        public Integer method1(){
           TrackingThreadLocal.tracker.get().put("method1", Arrays.asList("method2") );
           return classB.method2();
        }
    }

    public class ClassB {
        ClassC classC = new ClassC();
        public Integer method2() {
            TrackingThreadLocal.tracker.get().put( "method2",  Arrays.asList("method3", "method4") );
            return this.classC.method3() + this.classC.method4();
        }
    }

    public class ClassC {
        public Integer method3() {
            return 3;
        }
        public Integer method4() {
            return 4;
        }
    }

enter image description here