HJ8 合并表记录

该博客介绍了如何利用Java中的TreeMap数据结构,对具有相同索引的数值进行合并和求和操作。通过创建一个TreeMap,遍历输入的键值对,将相同键的值进行累加,最后按照键值升序输出结果。内容涵盖了TreeMap的基本用法,包括创建、插入、搜索和遍历等操作。
摘要由CSDN通过智能技术生成

描述

数据表记录包含表索引和数值(int范围的正整数),请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出。

输入描述:

先输入键值对的个数
然后输入成对的index和value值,以空格隔开

输出描述:

输出合并后的键值对(多行)

分析(转载)

这题的重点在于查找时间和存储空间的处理即数据结构的选取。
到底是map还是数组还是哈希表可以根据key的范围以及key-value对的个数选取。
1:如果key取值范围小,那么数组是最合适的。但本题不适合,因为并没有给出key的范围,如果key很大如Integer.MAX-1,那么直接开一个这么大的数组会崩的的
2:如果key的取值范围很大,但是key的个数少,那么哈希表比较合适
3:如果key的取值范围很大而且key的个数很多,那么tree是比较合适的。本题目适合tree。

 Scanner sc = new Scanner(System.in);
 while (sc.hasNext()){
     int next = sc.nextInt();
     TreeMap<Integer,Integer> map = new TreeMap<>();
     for (int i = 0; i < next; i++) {
         int key = sc.nextInt();
         int value = sc.nextInt();
         if (map.containsKey(key)){
              map.put(key,map.get(key)+value);
         }else {
             map.put(key,value);
         }
     }
     for (Map.Entry<Integer, Integer> integerIntegerEntry : map.entrySet()) {
         System.out.println(integerIntegerEntry.getKey()+" "+integerIntegerEntry.getValue());
     }
 }

关于Treemap的介绍

// Java code to show creation, insertion,
// searching, and traversal in a TreeMap

import java.util.*;
import java.util.concurrent.*;

public class TreeMapImplementation {

	// Declaring a TreeMap
	static TreeMap<Integer, String> tree_map;

	// Function to create TreeMap
	static void create()
	{
		// Creating an empty TreeMap
		tree_map
			= new TreeMap<Integer, String>();

		System.out.println(
			"TreeMap successfully"
			+ " created");
	}

	// Function to Insert values in
	// the TreeMap
	static void insert()
	{
		// Mapping string values to int keys
		tree_map.put(10, "Geeks");
		tree_map.put(15, "4");
		tree_map.put(20, "Geeks");
		tree_map.put(25, "Welcomes");
		tree_map.put(30, "You");

		System.out.println(
			"\nElements successfully"
			+ " inserted in the TreeMap");
	}

	// Function to search a key in TreeMap
	static void search(int key)
	{

		// Checking for the key
		System.out.println(
			"\nIs key \""
			+ key + "\" present? "
			+ tree_map.containsKey(key));
	}

	// Function to search a value in TreeMap
	static void search(String value)
	{

		// Checking for the value
		System.out.println(
			"\nIs value \""
			+ value + "\" present? "
			+ tree_map.containsValue(value));
	}

	// Function to display the elements in TreeMap
	static void display()
	{
		// Displaying the TreeMap
		System.out.println(
			"\nDisplaying the TreeMap:");

		System.out.println(
			"TreeMap: " + tree_map);
	}

	// Function to traverse TreeMap
	static void traverse()
	{
		System.out.println("\nTraversing the TreeMap:");
		for (Map.Entry<Integer, String> e : tree_map.entrySet())
			System.out.println(e.getKey()
							+ " "
							+ e.getValue());
	}

	// Driver code
	public static void main(String[] args)
	{

		// Creating the TreeMap
		create();

		// Inserting values in the TreeMap
		insert();

		// Search key "50" in the TreeMap
		search(50);

		// Search value "Geeks" in the TreeMap
		search("Geeks");

		// Display the elements in TreeMap
		display();

		// Traverse the TreeMap
		traverse();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值