Java Map交集 并集 差集(可直接运行)

求Map的交集、并集、差集

如果两个Map有相同的键,交集求最小值,并集求最大值。

public class HashMapTest {
    // 求交集
    private static Map<String,Integer> Intersection(Map<String,Integer> mp1, Map<String,Integer> mp2) {
        List<String> strRemove = new ArrayList<String>();
        Map<String,Integer> res = new HashMap<String,Integer>(mp1);
        for (String s: res.keySet()){
            if (!mp2.containsKey(s)) {//mp1中存在但mp2中不存在
                strRemove.add(s);
            }else{  //mp1中存在mp2中存在
                res.put(s,Math.min(res.get(s),mp2.get(s)));
            }
        }
        for (String s: strRemove) {
            res.remove(s);
        }
        return res;
    }
    // 求并集
    private static Map<String,Integer> Union(Map<String,Integer> mp1, Map<String,Integer> mp2) {
        Map<String,Integer> res = new HashMap<String,Integer>(mp1);
        for (String s: mp2.keySet()){
            if (mp1.containsKey(s)) {//mp2中存在 mp1中存在
                res.put(s,Math.max(mp1.get(s),mp2.get(s)));
            }else{    //mp2中存在 mp1中不存在
                res.put(s,mp2.get(s));
            }
        }
        return res;
    }
    // 求差集
    private static Map<String,Integer> Difference(Map<String,Integer> mp1, Map<String,Integer> mp2) {
        List<String> strRemove = new ArrayList<String>();
        Map<String,Integer> res = new HashMap<String,Integer>(mp1);
        for (String s: res.keySet()) {
            if (mp2.containsKey(s)){
                strRemove.add(s);
            }
        }
        for (String s: strRemove) {
            res.remove(s);
        }
        return res;
    }
    // 计数 生成两个Map
    private static Map<String,Integer> Counter(String string){
        Map<String,Integer> mp = new HashMap<String,Integer>();
        String[] strArray = string.split("");
        int cnt;
        for (String s:strArray){
            cnt = mp.getOrDefault(s,0);
            mp.put(s,cnt+1);
        }       
        return mp;
    }
    // 调用
	public static void main(String[] args){
        //求两个HashMap的交集、差集、并集       
        String str1 = "queue";
        String str2 = "upper";
        // 统计每个元素的出现次数
        Map<String,Integer> mp1 = new HashMap<String,Integer>();
        Map<String,Integer> mp2 = new HashMap<String,Integer>();
        
        mp1 = Counter(str1);
        mp2 = Counter(str2);
        System.out.println("mp1"+mp1);
        System.out.println("mp2"+mp2);
        // 求交集
        Map<String,Integer> resultIntersection = new HashMap<String,Integer>();
        resultIntersection = Intersection(mp1, mp2);
        System.out.println("交集:"+resultIntersection);
        
        //求并集
        Map<String,Integer> resultUnion = new HashMap<String,Integer>();
        resultUnion = Union(mp1, mp2);
        System.out.println("并集:"+resultUnion);
        
        //求差集
        Map<String,Integer> resultDifference = new HashMap<String,Integer>();
        resultDifference = Difference(mp1, mp2);
        System.out.println("差集mp1-mp2:"+resultDifference);
    }
}

在这里插入图片描述

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是示例代码,实现了在 ArcGIS Engine 上任意画两个多边形,并对它们进行交集并集差集运算: ```csharp using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.SystemUI; using System; using System.Drawing; using System.Windows.Forms; namespace PolygonOperations { public partial class MainForm : Form { private IToolbarMenu m_menu; private IGraphicsContainer m_graphicsContainer; private IActiveView m_activeView; private IPolygon m_polygon1; private IPolygon m_polygon2; public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { // 初始化 ArcGIS Engine 控件 axMapControl1.LoadMxFile(@"C:\data\map.mxd"); m_graphicsContainer = axMapControl1.ActiveView.GraphicsContainer; m_activeView = axMapControl1.ActiveView; // 创建一个右键菜单,包含三个子菜单:交集并集差集 m_menu = new ToolbarMenuClass(); m_menu.AddItem(new IntersectMenuItem(this)); m_menu.AddItem(new UnionMenuItem(this)); m_menu.AddItem(new DifferenceMenuItem(this)); } private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) { // 鼠标左键单击画多边形 if (e.button == 1) { if (m_polygon1 == null) { m_polygon1 = DrawPolygon(); } else if (m_polygon2 == null) { m_polygon2 = DrawPolygon(); } } // 鼠标右键单击打开右键菜单 else if (e.button == 2) { m_menu.PopupMenu(e.x, e.y, axMapControl1.hWnd); } } private IPolygon DrawPolygon() { // 在地图上画多边形 IGeometry geometry = axMapControl1.TrackPolygon(); if (geometry == null) return null; // 设置多边形的符号 ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass(); fillSymbol.Color = GetRandomColor(); ISymbol symbol = fillSymbol as ISymbol; symbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen; // 把多边形添加到图形容器中 IElement element = new PolygonElementClass(); element.Geometry = geometry; element.Symbol = symbol; m_graphicsContainer.AddElement(element, 0); // 刷新地图显示 m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); return geometry as IPolygon; } private Color GetRandomColor() { // 获取随机颜色 Random rand = new Random(); return Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255)); } public void Intersect() { // 计算两个多边形的交集 if (m_polygon1 == null || m_polygon2 == null) return; ITopologicalOperator topoOp = m_polygon1 as ITopologicalOperator; IGeometry result = topoOp.Intersect(m_polygon2, esriGeometryDimension.esriGeometry2Dimension); ShowResult(result); } public void Union() { // 计算两个多边形的并集 if (m_polygon1 == null || m_polygon2 == null) return; ITopologicalOperator topoOp = m_polygon1 as ITopologicalOperator; IGeometry result = topoOp.Union(m_polygon2); ShowResult(result); } public void Difference() { // 计算两个多边形的差集 if (m_polygon1 == null || m_polygon2 == null) return; ITopologicalOperator topoOp = m_polygon1 as ITopologicalOperator; IGeometry result = topoOp.Difference(m_polygon2); ShowResult(result); } private void ShowResult(IGeometry geometry) { // 显示运算结果 if (geometry == null) return; IElement element = new PolygonElementClass(); element.Geometry = geometry; ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass(); fillSymbol.Color = Color.Yellow; ISymbol symbol = fillSymbol as ISymbol; symbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen; element.Symbol = symbol; m_graphicsContainer.AddElement(element, 0); m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } } public class IntersectMenuItem : IToolbarMenuEvent { private MainForm m_form; public IntersectMenuItem(MainForm form) { m_form = form; } public void OnItemClick() { m_form.Intersect(); } } public class UnionMenuItem : IToolbarMenuEvent { private MainForm m_form; public UnionMenuItem(MainForm form) { m_form = form; } public void OnItemClick() { m_form.Union(); } } public class DifferenceMenuItem : IToolbarMenuEvent { private MainForm m_form; public DifferenceMenuItem(MainForm form) { m_form = form; } public void OnItemClick() { m_form.Difference(); } } } ``` 代码中定义了一个 MainForm 类,包含一个 ArcGIS Engine 控件和一个右键菜单,用于实现多边形运算。在 MainForm_Load 方法中初始化 ArcGIS Engine 控件和右键菜单,在 axMapControl1_OnMouseDown 方法中实现画多边形的功能,在 Intersect、Union、Difference 方法中分别计算多边形的交集并集差集,在 ShowResult 方法中显示运算结果。 在 IntersectMenuItem、UnionMenuItem、DifferenceMenuItem 类中分别实现右键菜单中的子菜单,用于调用 MainForm 中的 Intersect、Union、Difference 方法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值