java两map取交集_Java Map 求交集 并集 差集

import com.google.common.collect.MapDifference;

import com.google.common.collect.Maps;

import java.util.Map;

public class MapUtil {

/**

* difference

* Maps.difference(Map, Map)用来比较两个Map以获取所有不同点。该方法返回MapDifference对象

*/

public static void u(Map map1, Map map2) {

MapDifference difference = Maps.difference(map1, map2);

// 是否有差异,返回boolean

boolean areEqual = difference.areEqual();

System.out.println("比较两个Map是否有差异:" + areEqual);

// 两个map的交集

Map entriesInCommon = difference.entriesInCommon();

System.out.println("两个map都有的部分(交集)===:" + entriesInCommon);

// 键相同但是值不同值映射项。返回的Map的值类型为MapDifference.ValueDifference,以表示左右两个不同的值

Map> entriesDiffering = difference.entriesDiffering();

System.out.println("键相同但是值不同值映射项===:" + entriesDiffering);

// 键只存在于左边Map的映射项

Map onlyOnLeft = difference.entriesOnlyOnLeft();

System.out.println("键只存在于左边Map的映射项:" + onlyOnLeft);

// 键只存在于右边Map的映射项

Map entriesOnlyOnRight = difference.entriesOnlyOnRight();

System.out.println("键只存在于右边Map的映射项:" + entriesOnlyOnRight);

}

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是示例代码,实现了在 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 方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值